压缩文件
public void YaSuo() { using (FileStream fsRead = File.OpenRead(@"F:\MVC5_Demo\Project4YaSuo\Project4YaSuo\Files\笔记.txt")) { //创建写入文件的流 using (FileStream fsWrite = File.OpenWrite(@"F:\MVC5_Demo\Project4YaSuo\Project4YaSuo\Files\yasuo.rar")) { //创建压缩流 using (GZipStream zipStream = new GZipStream(fsWrite, CompressionMode.Compress)) { //每次读取1024byte byte[] byts = new byte[1024 * 10]; int len = 0; while ((len = fsRead.Read(byts, 0, byts.Length)) > 0) { zipStream.Write(byts, 0, len);//通过压缩流写入文件 } } } } }
解压文件
public void JieYa() { //读取压缩文件 using (FileStream fsRead = File.OpenRead(@"F:\MVC5_Demo\Project4YaSuo\Project4YaSuo\Files\yasuo.rar")) { //创建压缩流 using (GZipStream gzipStream = new GZipStream(fsRead, CompressionMode.Decompress)) { using (FileStream fsWrite = File.OpenWrite(@"F:\MVC5_Demo\Project4YaSuo\Project4YaSuo\Files\笔记.txt")) { byte[] byts = new byte[1024 * 10]; int len = 0; //写入新文件 while ((len = gzipStream.Read(byts, 0, byts.Length)) > 0) { fsWrite.Write(byts, 0, len); } } } } }