//压缩文件
public static void ZGip(string fileName, string gipFileName)
{FileStream fc = File.OpenRead(fileName);
FileStream fr = File.Create(gipFileName);
GZipStream gzs = new GZipStream(fr, CompressionMode.Compress); //压缩文件类
/*
int thebyte=fc.ReadByte();
while(thebyte!=-1)
{
gzs.WriteByte((byte)thebyte);
thebyte=fc.ReadByte();
}
*/
byte []arr = new byte[fc.Length];
fc.Read(arr, 0, (int)fc.Length);
gzs.Write(arr, 0, (int)fc.Length);
gzs.Close();
fc.Close();
fr.Close();
}
//解压缩文件方法
public static void DeZGip(string fileName, string gipFileName){
//准备输入输出文件
FileStream fc = File.Create(fileName);
FileStream fr = File.OpenRead(gipFileName);
GZipStream gzs = new GZipStream(fr, CompressionMode.Decompress);
byte[] arr = new byte[fr.Length];fr.Read(arr, 0, (int)fr.Length);
fc.Write(arr, 0, (int)fr.Length);
gzs.Close();
fr.Close();
fc.Close();
}
本文介绍了一个使用GZipStream进行文件压缩和解压缩的方法。通过FileStream读取原始文件内容,并利用GZipStream将其压缩到目标文件中;反之亦然,能够从压缩文件还原出原始数据。
3383

被折叠的 条评论
为什么被折叠?



