2016-12-01 2 views
5

바이트 배열을 압축해야합니다. 압축 된 배열의 크기가 압축 해제 1보다 큰 이유Gzip을 사용하여 바이트 배열 압축/압축 해제

output

가 이해가 안 :

class Program 
    { 
     static void Main() 
     { 
      var test = "foo bar baz"; 

      var compressed = Compress(Encoding.UTF8.GetBytes(test)); 
      var decompressed = Decompress(compressed); 
      Console.WriteLine("size of initial table = " + test.Length); 
      Console.WriteLine("size of compressed table = " + compressed.Length); 
      Console.WriteLine("size of decompressed table = " + decompressed.Length); 
      Console.WriteLine(Encoding.UTF8.GetString(decompressed)); 
      Console.ReadKey(); 
     } 

     static byte[] Compress(byte[] data) 
     { 
      using (var compressedStream = new MemoryStream()) 
      using (var zipStream = new GZipStream(compressedStream, CompressionMode.Compress)) 
      { 
       zipStream.Write(data, 0, data.Length); 
       zipStream.Close(); 
       return compressedStream.ToArray(); 
      } 
     } 

     static byte[] Decompress(byte[] data) 
     { 
      using (var compressedStream = new MemoryStream(data)) 
      using (var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress)) 
      using (var resultStream = new MemoryStream()) 
      { 
       zipStream.CopyTo(resultStream); 
       return resultStream.ToArray(); 
      } 
     } 
    } 

문제는 내가이 출력을 얻을 수 있습니다 : 그래서 나는이 조각을 썼다!

아이디어가 있으십니까? 내가 예를 들어 test 문자열을 변경하는 경우 : @의 스펜더의 발언 후

편집

var test = "foo bar baz very long string for example hdgfgfhfghfghfghfghfghfghfghfghfghfghfhg"; 

를 나는 다른 결과를 얻을. 그렇다면 초기 배열의 최소 크기는 얼마입니까?

+2

데이터를 압축 포맷의 압축 오버 헤드의 증가를 능가 정도로 작기 때문에 :

GZIP 여기에 상기 프로브 흥미로운 질문이다. 더 많은 데이터를보십시오. 참고 : 완전히 임의의 데이터는 압축되지 않습니다. – spender

+0

@spender plz 내 편집을보고 답변을 작성해주세요. 감사합니다. –

답변

1

압축 파일에는 헤더가 있으며 파일 크기가 커지므로 입력 크기가 매우 작 으면 출력이 훨씬 커질 수 있습니다. 더 큰 크기의 파일로 시도해보십시오.

1

데이터 양이 너무 적어 압축 형식의 오버 헤드가 압축 이득을 능가하지 않기 때문입니다.

더 많은 데이터를보십시오.

완전히 임의의 데이터 (또는 jpeg와 같은 이미 압축 된 데이터)를 압축하면 결코 큰 이익을 얻지 못할 것입니다. 그러나 문자열 new String('*',1000000)은 정말 멋지게 압축 할 것입니다.

GZIP는 최소 18 바이트를 추가하므로 쉽게 압축 할 수있는이 크기보다 약간 아래 또는 약간 위에 이득이 없습니다. What's the most that GZIP or DEFLATE can increase a file size?