在抓取一些網站時,不論怎麼改編碼數據流都是亂碼,這是就應該查看該網站是否用了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();
}