바이트 배열을 압축해야합니다. 압축 된 배열의 크기가 압축 해제 1보다 큰 이유Gzip을 사용하여 바이트 배열 압축/압축 해제
가 이해가 안 :
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";
를 나는 다른 결과를 얻을. 그렇다면 초기 배열의 최소 크기는 얼마입니까?
데이터를 압축 포맷의 압축 오버 헤드의 증가를 능가 정도로 작기 때문에 :
GZIP 여기에 상기 프로브 흥미로운 질문이다. 더 많은 데이터를보십시오. 참고 : 완전히 임의의 데이터는 압축되지 않습니다. – spender
@spender plz 내 편집을보고 답변을 작성해주세요. 감사합니다. –