Created
December 6, 2016 13:22
-
-
Save zhenlinyang/803ef2ee2f54648d4680d22326106f9d to your computer and use it in GitHub Desktop.
CSharp GZip
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class ZipMng | |
{ | |
public static byte[] Compress(byte[] originalData) | |
{ | |
using (MemoryStream outms = new MemoryStream()) | |
{ | |
using (MemoryStream inms = new MemoryStream(originalData)) | |
{ | |
using (GZipStream gzs = new GZipStream(outms, CompressionMode.Compress)) | |
{ | |
byte[] buffer = new byte[81920]; | |
int read = 0; | |
while ((read = inms.Read(buffer, 0, buffer.Length)) != 0) | |
{ | |
gzs.Write(buffer, 0, read); | |
} | |
} | |
} | |
return outms.ToArray(); | |
} | |
} | |
public static byte[] Decompress(byte[] compressedData) | |
{ | |
using (MemoryStream outms = new MemoryStream()) | |
{ | |
using (MemoryStream inms = new MemoryStream(compressedData)) | |
{ | |
using (GZipStream gzs = new GZipStream(inms, CompressionMode.Decompress)) | |
{ | |
byte[] buffer = new byte[81920]; | |
int read = 0; | |
while ((read = gzs.Read(buffer, 0, buffer.Length)) != 0) | |
{ | |
outms.Write(buffer, 0, read); | |
} | |
} | |
} | |
return outms.ToArray(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment