利用ICSharpCode.SharpZipLib.dll 压缩文件夹
View Code
1 private void zip(string strFile, ZipOutputStream s, string staticFile) 2 { 3 if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar) 4 { 5 strFile = strFile + Path.DirectorySeparatorChar; 6 } 7 Crc32 crc = new Crc32(); 8 string[] fileSystemEntries = Directory.GetFileSystemEntries(strFile); 9 foreach (string str in fileSystemEntries) 10 { 11 if (Directory.Exists(str)) 12 { 13 zip(str, s, staticFile); 14 } 15 else 16 { 17 FileStream stream = File.OpenRead(str); 18 byte[] buffer = new byte[stream.Length]; 19 stream.Read(buffer, 0, buffer.Length); 20 ZipEntry entry = new ZipEntry(str.Substring(staticFile.LastIndexOf(@"\") + 1)); 21 entry.DateTime=DateTime.Now; 22 entry.Size = stream.Length; 23 stream.Close(); 24 crc.Reset(); 25 crc.Update(buffer); 26 entry.Crc=crc.Value; 27 s.PutNextEntry(entry); 28 s.Write(buffer, 0, buffer.Length); 29 } 30 } 31 } 32 /// <summary> 33 /// 34 /// </summary> 35 /// <param name="strFile">文件夹</param> 36 /// <param name="strZip">压缩后文件</param> 37 public void ZipFile(string strFile, string strZip) 38 { 39 if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar) 40 { 41 strFile = strFile + Path.DirectorySeparatorChar; 42 } 43 ZipOutputStream s = new ZipOutputStream(File.Create(strZip)); 44 s.Password = "**";//需要密码可以设置 45 s.SetLevel(6); 46 zip(strFile, s, strFile); 47 s.Finish(); 48 s.Close(); 49 }