程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> c#.net中使用GZipStream進行Gzip壓縮

c#.net中使用GZipStream進行Gzip壓縮

編輯:關於C#
 

在抓取一些網站時,不論怎麼改編碼數據流都是亂碼,這是就應該查看該網站是否用了gzip壓縮(檢測地址:http://tool.chinaz.com/Gzips/)

再知道了該網址確實用了gzip壓縮,就可以把他解壓了,這樣亂碼的問題就解決了。


code//壓縮
public void Compress(System.IO.Stream orgStream, System.IO.Stream cmpStream)
{
System.IO.Compression.GZipStream zipStream = new System.IO.Compression.GZipStream(cmpStream, System.IO.Compression.CompressionMode.Compress);
BinaryWriter writer = new BinaryWriter(zipStream);
BinaryReader reader = new BinaryReader(orgStream);
while (true)
{
byte[] buffer = reader.ReadBytes(100);
if (buffer == null || buffer.Length < 1)
break;
writer.Write(buffer);
}
}
//解壓縮
public void DeCompress(System.IO.Stream cmpStream, System.IO.Stream orgStream)
{
System.IO.Compression.GZipStream zipStream = new System.IO.Compression.GZipStream(cmpStream, System.IO.Compression.CompressionMode.Decompress);
BinaryReader reader = new BinaryReader(zipStream);
BinaryWriter writer = new BinaryWriter(orgStream);
while (true)
{
byte[] buffer = reader.ReadBytes(100);
if (buffer == null || buffer.Length < 1)
break;
writer.Write(buffer);
}
}
//執行壓縮
private void button1_Click(object sender, EventArgs e)
{
System.IO.FileStream targetstream = new System.IO.FileStream(this.txt_SelFile.Text, System.IO.FileMode.Open);
System.IO.FileStream saveStream = new System.IO.FileStream(this.txt_SaveTo.Text, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.Read);
CompressionHelper.Compress(CompressionHelper.CompressionMode.GZip,targetstream, saveStream);
targetstream.Close();
saveStream.Close();
}
//執行解壓縮
private void button2_Click(object sender, EventArgs e)
{
System.IO.FileStream targetstream = new System.IO.FileStream(this.txt_SelFile.Text, System.IO.FileMode.Open);
System.IO.FileStream saveStream = new System.IO.FileStream(this.txt_SaveTo.Text,System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite, System.IO.FileShare.Read);
CompressionHelper.DeCompress(CompressionHelper.CompressionMode.GZip , targetstream, saveStream);
targetstream.Close();
saveStream.Close();
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved