需要下载ICSharpCode.SharpZipLib.dll
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 using System.IO; 8 using ICSharpCode.SharpZipLib.Checksums; 9 using ICSharpCode.SharpZipLib.Zip; 10 using ICSharpCode.SharpZipLib.GZip; 11 12 namespace ZYOfficeWrap 13 { 14 15 /// <summary> 16 /// 压缩文件格式 17 /// </summary> 18 19 public enum fileType 20 { 21 Zip = 1, 22 RAR = 2, 23 Tar =3 24 } 25 /// <summary> 26 /// 文件解压缩操作类库 27 /// </summary> 28 public class FileZipOpr 29 { 30 /// <summary> 31 /// 构造函数 32 /// </summary> 33 public FileZipOpr() 34 { 35 36 } 37 38 /// <summary> 39 /// 压缩单个文件 40 /// </summary> 41 /// <param name="fileToZip">要压缩的文件</param> 42 /// <param name="zipedFile">压缩后的文件</param> 43 /// <param name="compressionLevel">压缩等级</param> 44 /// <param name="blockSize">每次写入大小</param> 45 public void ZipFile(string fileToZip, string zipedFile, int compressionLevel, int blockSize) 46 { 47 //如果文件没有找到,则报错 48 if (!File.Exists(fileToZip)) 49 { 50 throw new System.IO.FileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!"); 51 } 52 53 using (FileStream ZipFile =