2012-05-16 20 views
5

मैं 100 जीबी के क्षेत्र में बड़ी मात्रा में डेटा को संपीड़ित करने का प्रयास कर रहा हूं, जब मैं दिनचर्या चलाता हूं तो मैंने लिखा है कि यह फ़ाइल पिछले आकार के समान आकार के बाहर आती है। क्या किसी और को GZipStream के साथ यह समस्या है?बड़े डेटा पर GZipStream

 byte[] buffer = BitConverter.GetBytes(StreamSize); 
     FileStream LocalUnCompressedFS = File.OpenWrite(ldiFileName); 
     LocalUnCompressedFS.Write(buffer, 0, buffer.Length); 
     GZipStream LocalFS = new GZipStream(LocalUnCompressedFS, CompressionMode.Compress); 
     buffer = new byte[WriteBlock]; 
     UInt64 WrittenBytes = 0; 
     while (WrittenBytes + WriteBlock < StreamSize) 
     { 
      fromStream.Read(buffer, 0, (int)WriteBlock); 
      LocalFS.Write(buffer, 0, (int)WriteBlock); 
      WrittenBytes += WriteBlock; 
      OnLDIFileProgress(WrittenBytes, StreamSize); 
      if (Cancel) 
       break; 
     } 
     if (!Cancel) 
     { 
      double bytesleft = StreamSize - WrittenBytes; 
      fromStream.Read(buffer, 0, (int)bytesleft); 
      LocalFS.Write(buffer, 0, (int)bytesleft); 
      WrittenBytes += (uint)bytesleft; 
      OnLDIFileProgress(WrittenBytes, StreamSize); 
     } 
     LocalFS.Close(); 
     fromStream.Close(); 

StreamSize एक 8 बाइट UInt64 मूल्य कि फ़ाइल के आकार को धारण करता है:

मेरे कोड इस प्रकार है। मैं फ़ाइल की शुरुआत में इन 8 बाइट कच्चे लिखता हूं इसलिए मुझे मूल फ़ाइल आकार पता है। लिखेंब्लॉक में 32kb (32768 बाइट्स) का मान है। सेस्ट्रीम इस उदाहरण में, एक फ़ाइलस्ट्रीम से डेटा लेने के लिए धारा है। संपीड़ित डेटा के सामने 8 बाइट्स किसी समस्या का कारण बन रहा है?

+0

छोटी फ़ाइलों पर अपने कोड काम करता है? –

+1

क्या आप अपने कोड को छोटे डेटासेट को सही ढंग से संपीड़ित कर सकते हैं - उदाहरण के लिए एक टेक्स्ट फ़ाइल जिसे आप सामान्य रूप से अच्छी तरह से संपीड़ित करते हैं ... – Nik

उत्तर

4

मैंने संपीड़न के लिए निम्न कोड का उपयोग करके एक परीक्षण चलाया और यह 7 जीबी और 12 जीबी फ़ाइल (बिना "अच्छी तरह से" संपीड़ित करने के लिए पहले से ज्ञात दोनों) के बिना किसी समस्या के भाग गया। क्या यह संस्करण आपके लिए काम करता है?

const string toCompress = @"input.file"; 
var buffer = new byte[1024*1024*64]; 

using(var compressing = new GZipStream(File.OpenWrite(@"output.gz"), CompressionMode.Compress)) 
using(var file = File.OpenRead(toCompress)) 
{ 
    var bytesRead = 0; 
    while(bytesRead < buffer.Length) 
    { 
     bytesRead = file.Read(buffer, 0, buffer.Length); 
     compressing.Write(buffer, 0, buffer.Length); 
    } 
} 

आप documentation बाहर जाँच की है?

GZipStream वर्ग डेटा कि असंपीड़ित डेटा के 8 जीबी से अधिक में परिणाम को संपीड़ित नहीं कर सकते।

आप शायद एक अलग पुस्तकालय कि अपनी आवश्यकताओं का समर्थन या <=8GB हिस्सा है कि सुरक्षित रूप से "पिरोया" किया जा सकता है वापस एक साथ में अपने डेटा को विभाजित करने का प्रयास करेंगे खोजने की जरूरत है।

+2

हाय ऑस्टिन, उत्तर के लिए धन्यवाद। मेरा कार्यक्रम डिकंप्रेस नहीं करेगा इसलिए मुझे यह बात नहीं है? जब तक यह संपीड़न के लिए 8 जीबी सीमा भी न हो। – Skintkingle

+0

हम्म ... क्या होगा यदि आपको उससे अधिक की आवश्यकता है? क्या अन्य विकल्प उपलब्ध हैं? यह अजीब लगता है कि एक धारा में उस तरह की सीमा होगी। –

+0

यह डिकंप्रेशन के बारे में बात कर रहा है, ओपी संपीड़न के बारे में बात कर रहा है। –

-1

ऑस्टिन सैलोनन का कोड मेरे लिए काम नहीं करता है (छोटी गाड़ी, 4 जीबी त्रुटि)।

यहाँ उचित तरीका है:

using System; 
using System.Collections.Generic; 
using System.Text; 

namespace CompressFile 
{ 
    class Program 
    { 


     static void Main(string[] args) 
     { 
      string FileToCompress = @"D:\Program Files (x86)\msvc\wkhtmltopdf64\bin\wkhtmltox64.dll"; 
      FileToCompress = @"D:\Program Files (x86)\msvc\wkhtmltopdf32\bin\wkhtmltox32.dll"; 
      string CompressedFile = System.IO.Path.Combine(
       System.IO.Path.GetDirectoryName(FileToCompress) 
       ,System.IO.Path.GetFileName(FileToCompress) + ".gz" 
      ); 


      CompressFile(FileToCompress, CompressedFile); 
      // CompressFile_AllInOne(FileToCompress, CompressedFile); 

      Console.WriteLine(Environment.NewLine); 
      Console.WriteLine(" --- Press any key to continue --- "); 
      Console.ReadKey(); 
     } // End Sub Main 


     public static void CompressFile(string FileToCompress, string CompressedFile) 
     { 
      //byte[] buffer = new byte[1024 * 1024 * 64]; 
      byte[] buffer = new byte[1024 * 1024]; // 1MB 

      using (System.IO.FileStream sourceFile = System.IO.File.OpenRead(FileToCompress)) 
      { 

       using (System.IO.FileStream destinationFile = System.IO.File.Create(CompressedFile)) 
       { 

        using (System.IO.Compression.GZipStream output = new System.IO.Compression.GZipStream(destinationFile, 
         System.IO.Compression.CompressionMode.Compress)) 
        { 
         int bytesRead = 0; 
         while (bytesRead < sourceFile.Length) 
         { 
          int ReadLength = sourceFile.Read(buffer, 0, buffer.Length); 
          output.Write(buffer, 0, ReadLength); 
          output.Flush(); 
          bytesRead += ReadLength; 
         } // Whend 

         destinationFile.Flush(); 
        } // End Using System.IO.Compression.GZipStream output 

        destinationFile.Close(); 
       } // End Using System.IO.FileStream destinationFile 

       // Close the files. 
       sourceFile.Close(); 
      } // End Using System.IO.FileStream sourceFile 

     } // End Sub CompressFile 


     public static void CompressFile_AllInOne(string FileToCompress, string CompressedFile) 
     { 
      using (System.IO.FileStream sourceFile = System.IO.File.OpenRead(FileToCompress)) 
      { 
       using (System.IO.FileStream destinationFile = System.IO.File.Create(CompressedFile)) 
       { 

        byte[] buffer = new byte[sourceFile.Length]; 
        sourceFile.Read(buffer, 0, buffer.Length); 

        using (System.IO.Compression.GZipStream output = new System.IO.Compression.GZipStream(destinationFile, 
         System.IO.Compression.CompressionMode.Compress)) 
        { 
         output.Write(buffer, 0, buffer.Length); 
         output.Flush(); 
         destinationFile.Flush(); 
        } // End Using System.IO.Compression.GZipStream output 

        // Close the files.   
        destinationFile.Close(); 
       } // End Using System.IO.FileStream destinationFile 

       sourceFile.Close(); 
      } // End Using System.IO.FileStream sourceFile 

     } // End Sub CompressFile 


    } // End Class Program 


} // End Namespace CompressFile 
+0

कृपया डाउनवोट समझाएं? –

संबंधित मुद्दे