?關于.net壓縮功能有很多的小伙伴們不知道該如何去實現,那么現在本文就將為大家介紹.net實現壓縮功能的方法,其實.net壓縮功能沒有我們想象中的那么復雜,下面我們就一起去參考參考吧。
復制代碼 代碼如下:
public static class Compressor??? {
??????????? public static byte[] Compress(byte[] data)
??????????? {
??????????????? using (MemoryStream output = new MemoryStream())
??????????????? {
??????????????????? using (GZipStream gzip = new GZipStream(output, CompressionMode.Compress, true))
??????????????????? {
??????????????????????? gzip.Write(data, 0, data.Length);
??????????????????????? gzip.Close();
??????????????????????? return output.ToArray();
??????????????????? }
??????????????? }
??????????? }
??????????? public static byte[] Decompress(byte[] data)
??????????? {
??????????????? using (MemoryStream input = new MemoryStream())
??????????????? {
??????????????????? input.Write(data, 0, data.Length);
??????????????????? input.Position = 0;
??????????????????? using (GZipStream gzip = new GZipStream(input, CompressionMode.Decompress, true))
??????????????????? {
??????????????????????? using (MemoryStream output = new MemoryStream())
??????????????????????? {
??????????????????????????? byte[] buff = new byte[64];
??????????????????????????? int read = -1;
??????????????????????????? read = gzip.Read(buff, 0, buff.Length);
??????????????????????????? while (read > 0)
??????????????????????????? {
??????????????????????????????? output.Write(buff, 0, read);
??????????????????????????????? read = gzip.Read(buff, 0, buff.Length);
??????????????????????????? }
??????????????????????????? gzip.Close();
??????????????????????????? return output.ToArray();
??????????????????????? }
??????????????????? }
??????????????? }
??????????? }
看完本文關于.net實現壓縮功能的方法后你是不是明白了呢?想了解更多的ASP.NET知識就請關注我們錯新技術頻道吧!?