C#
10# 데이터 압축
NaHyungMin
2017. 5. 19. 12:26
유니티 통신용으로 호환될 수 있는 SharpZipLib 사용
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | using ICSharpCode.SharpZipLib.BZip2; /// <summary> /// byte[] compress 압축 /// </summary> public byte[] GenericToByteZip<T>(T generic) { MemoryStream buffer = new MemoryStream(); MemoryStream outBuffer = new MemoryStream(); protobuf.Serialize<T>(buffer, ref generic); buffer.Position = 0; BZip2.Compress(buffer, outBuffer, false, 3); return outBuffer.ToArray(); } /// <summary> /// byte[] Decompress 압축해제 /// </summary> public T ByteZipGeneric<T>(byte[] bytes) { MemoryStream buffer = new MemoryStream(bytes); MemoryStream outBuffer = new MemoryStream(); //buffer.Position = 0; BZip2.Decompress(buffer, outBuffer, false); outBuffer.Position = 0; return protobuf.Deserialize<T>(outBuffer); } | cs |
압축 해제 풀기
1 2 3 | byte[] myTownBytes = serializerSharpZip.GenericToByteZip<타입>(압축 T); serializerSharpZip.ByteZipGeneric<타입>(해제할 byte); | cs |