一,代码
using System; using System.IO; using ICSharpCode.SharpZipLib.Checksums; using ICSharpCode.SharpZipLib.Zip; namespace Helper { public class ZipHelper { public static bool ZipDirectory(string folderToZip, ZipOutputStream zipStream, string parentFolderName) { bool result = true; ZipEntry zipEntry = null; FileStream fileStream = null; Crc32 crc = new Crc32(); try { zipEntry = new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/")); zipStream.PutNextEntry(zipEntry); zipStream.Flush(); string[] files = Directory.GetFiles(folderToZip); string[] array = files; foreach (string path in array) { fileStream = File.OpenRead(path); byte[] array2 = new byte[fileStream.Length]; fileStream.Read(array2, 0, array2.Length); zipEntry = new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/" + Path.GetFileName(path))); zipEntry.DateTime = DateTime.Now; zipEntry.Size = fileStream.Length; fileStream.Close(); crc.Reset(); crc.Update(array2); zipEntry.Crc = crc.Value; zipStream.PutNextEntry(zipEntry); zipStream.Write(array2, 0, array2.Length); } } catch { result = false; } finally { if (fileStream != null) { fileStream.Close(); fileStream.Dispose(); } if (zipEntry != null) { zipEntry = null; } GC.Collect(); GC.Collect(1); } string[] directories = Directory.GetDirectories(folderToZip); string[] array3 = directories; int num = 0; while (true) { if (num < array3.Length) { string folderToZip2 = array3[num]; if (!ZipDirectory(folderToZip2, zipStream, folderToZip)) { break; } num++; continue; } return result; } return false; } public static bool ZipDirectory(string folderToZip, string zipedFile, string password) { bool result = false; if (!Directory.Exists(folderToZip)) { return result; } ZipOutputStream zipOutputStream = new ZipOutputStream(File.Create(zipedFile)); zipOutputStream.SetLevel(6); if (!string.IsNullOrEmpty(password)) { zipOutputStream.Password = password; } result = ZipDirectory(folderToZip, zipOutputStream, ""); zipOutputStream.Finish(); zipOutputStream.Close(); return result; } public static bool ZipDirectory(string folderToZip, string zipedFile) { return ZipDirectory(folderToZip, zipedFile, null); } public static bool ZipFile(string fileToZip, string zipedFile, string password) { bool result = true; ZipOutputStream zipOutputStream = null; FileStream fileStream = null; ZipEntry zipEntry = null; if (!File.Exists(fileToZip)) { return false; } try { fileStream = File.OpenRead(fileToZip); byte[] array = new byte[fileStream.Length]; fileStream.Read(array, 0, array.Length); fileStream.Close(); fileStream = File.Create(zipedFile); zipOutputStream = new ZipOutputStream(fileStream); if (!string.IsNullOrEmpty(password)) { zipOutputStream.Password = password; } zipEntry = new ZipEntry(Path.GetFileName(fileToZip)); zipOutputStream.PutNextEntry(zipEntry); zipOutputStream.SetLevel(6); zipOutputStream.Write(array, 0, array.Length); } catch { result = false; } finally { if (zipOutputStream != null) { zipOutputStream.Finish(); zipOutputStream.Close(); } if (zipEntry != null) { zipEntry = null; } if (fileStream != null) { fileStream.Close(); fileStream.Dispose(); } } GC.Collect(); GC.Collect(1); return result; } public static bool ZipFile(string fileToZip, string zipedFile) { return ZipFile(fileToZip, zipedFile, null); } public static bool Zip(string fileToZip, string zipedFile, string password) { bool result = false; if (Directory.Exists(fileToZip)) { result = ZipDirectory(fileToZip, zipedFile, password); } else if (File.Exists(fileToZip)) { result = ZipFile(fileToZip, zipedFile, password); } return result; } public static bool Zip(string fileToZip, string zipedFile) { return Zip(fileToZip, zipedFile, null); } public static bool UnZip(string fileToUnZip, string zipedFolder, string password) { bool result = true; FileStream fileStream = null; ZipInputStream zipInputStream = null; ZipEntry zipEntry = null; if (!File.Exists(fileToUnZip)) { return false; } if (!Directory.Exists(zipedFolder)) { Directory.CreateDirectory(zipedFolder); } try { zipInputStream = new ZipInputStream(File.OpenRead(fileToUnZip)); if (!string.IsNullOrEmpty(password)) { zipInputStream.Password = password; } while ((zipEntry = zipInputStream.GetNextEntry()) != null) { if (string.IsNullOrEmpty(zipEntry.Name)) { continue; } string text = Path.Combine(zipedFolder, zipEntry.Name); text = text.Replace('/', '\\'); if (text.EndsWith("\\")) { Directory.CreateDirectory(text); continue; } fileStream = File.Create(text); int num = 2048; byte[] array = new byte[2048]; while (true) { num = fileStream.Read(array, 0, array.Length); if (num <= 0) { break; } fileStream.Write(array, 0, array.Length); } } } catch { result = false; } finally { if (fileStream != null) { fileStream.Close(); fileStream.Dispose(); } if (zipInputStream != null) { zipInputStream.Close(); zipInputStream.Dispose(); } if (zipEntry != null) { zipEntry = null; } GC.Collect(); GC.Collect(1); } return result; } public static bool UnZip(string fileToUnZip, string zipedFolder) { return UnZip(fileToUnZip, zipedFolder, null); } } }