liudongbei 发表于 2013-7-3 09:19:38

使用GZip压缩和解压缩示例

byte[] data;

//读取文件并压缩
using (FileStream fileStream = new FileStream(filePath1, FileMode.Open, FileAccess.Read))
{
using (MemoryStream ms = new MemoryStream())
{

using (System.IO.Compression.GZipStream gzip = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress))
{
   fileStream.CopyTo(gzip);                                    
}
data = ms.ToArray();
}
}

//解压缩并写本地文件
FileInfo fi = new FileInfo(filePath2);            
using (System.IO.FileStream fs = fi.Create())            
{
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
ms.Write(data, 0, data.File.Length);
ms.Seek(0, System.IO.SeekOrigin.Begin);
using (System.IO.Compression.GZipStream gzip = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress))
{
   gzip.CopyTo(fs);
}
}   
}

眯眼笑 发表于 2013-7-3 10:12:34

谢谢分享
页: [1]
查看完整版本: 使用GZip压缩和解压缩示例