2012-05-16 12 views
5

가끔은 100GB 영역에서 많은 양의 데이터를 압축하려고합니다. 내가 작성한 루틴을 실행할 때 파일이 이전 크기와 정확히 같은 크기로 나오면 나타납니다. 다른 사람이 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 바이트 원시 파일의 시작 부분에 쓸 수 있습니다. Writeblock의 값은 32kb (32768 바이트)입니다. fromStream은이 인스턴스에서는 FileStream에서 데이터를 가져 오는 스트림입니다. 압축 된 데이터의 8 바이트가 문제의 원인이 될까요?

+0

가 작은 파일에 코드를 사용할 수 있습니까? –

+1

코드가 올바르게 데이터를 압축하는지 확인할 수 있습니까? 예를 들어 일반적으로 잘 압니다. ... – Nik

답변

4

압축을 위해 다음 코드를 사용하여 테스트를 실행했으며 7GB 및 12GB 파일에서 문제없이 실행되었습니다 (둘 다 미리 "잘"압축 됨). 이 버전이 효과가 있습니까?

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 클래스

는 비 압축 데이터 8GB의 이상에서 초래 된 데이터를 압축 해제 할 수 없습니다.

당신은 아마 당신의 요구 사항을 지원하거나 안전하게 다시 함께 "수 놓은"할 수 <=8GB 덩어리들로 데이터를 파괴하려고 시도합니다 다른 라이브러리를 찾을 필요가있다.

+2

안녕하세요, 오스틴, 답변 해 주셔서 감사합니다. 내 프로그램은 감압되지 않을 것이므로 나는이 문제가 중요하다고 생각하지 않습니까? 압축에 8 기가 바이트 제한도 없다면. – Skintkingle

+0

흠 ... 그 이상이 필요한 경우? 다른 옵션을 사용할 수 있습니까? 그것은 스트림이 그런 종류의 제한이있을 것이라고 이상한 것 같습니다. –

+0

그건 압축 해제에 대한 얘기입니다, OP는 압축에 대해 이야기하고 있습니다. –

-1

Austin Salonen의 코드가 작동하지 않습니다 (버그, 4GB 오류).

여기에 적절한 방법 :

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

downvote에 대해 설명해주십시오. –