using ICSharpCode.SharpZipLib.Checksums; using ICSharpCode.SharpZipLib.Zip; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace MacCheck { /// <summary> /// 适用与ZIP压缩 /// </summary> public static class ZipHelper { /// <summary> /// 递归压缩文件夹的内部方法 /// </summary> /// <param name="folderToZip">要压缩的文件夹路径</param> /// <param name="zipStream">压缩输出流</param> /// <param name="parentFolderName">此文件夹的上级文件夹</param> /// <returns></returns> private static bool ZipDirectory(string folderToZip, ZipOutputStream zipStream, string parentFolderName) { bool result = false; if (Directory.Exists(folderToZip)) { try { string FolderName = Path.GetFileName(folderToZip); ZipEntry theEntry = new ZipEntry(Path.Combine(parentFolderName, FolderName + "/")); zipStream.PutNextEntry(theEntry); zipStream.Flush(); foreach (string file in Directory.GetFiles(folderToZip)) { using (FileStream fileStream = File.OpenRead(file)) { theEntry = new ZipEntry(Path.Combine(parentFolderName, FolderName + "/" + Path.GetFileName(file))); theEntry.DateTime = DateTime.Now; theEntry.Size = fileStream.Length; byte[] buffer = new byte[fileStream.Length]; fileStream.Read(buffer, 0, buffer.Length); Crc32 crc = new Crc32(); crc.Update(buffer); theEntry.Crc = crc.Value; zipStream.PutNextEntry(theEntry); zipStream.Write(buffer, 0, buffer.Length); } } result = true; foreach (string folder in Directory.GetDirectories(folderToZip)) { if (!ZipDirectory(folder, zipStream, FolderName)) { return false; } } } catch { result = false; } } return result; } /// <summary> /// 压缩文件夹 /// </summary> /// <param name="folderToZip">要压缩的文件夹路径</param> /// <param name="zipedFile">压缩文件完整路径</param> /// <param name="password">密码</param> /// <returns>是否压缩成功</returns> public static bool ZipDirectory(string folderToZip, string zipedFile, string password) { bool result = false; if (Directory.Exists(folderToZip)) { using (ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipedFile))) { if (!string.IsNullOrEmpty(password)) { zipStream.Password = password; } zipStream.SetLevel(6); result = ZipDirectory(folderToZip, zipStream, ""); } } return result; } /// <summary> /// 压缩文件夹 /// </summary> /// <param name="folderToZip">要压缩的文件夹路径</param> /// <param name="zipedFile">压缩文件完整路径</param> /// <returns>是否压缩成功</returns> public static bool ZipDirectory(string folderToZip, string zipedFile) { return ZipDirectory(folderToZip, zipedFile, string.Empty); } /// <summary> /// 压缩文件 /// </summary> /// <param name="fileToZip">要压缩的文件全名</param> /// <param name="zipedFile">压缩后的文件名</param> /// <param name="password">密码</param> /// <returns>压缩结果</returns> public static bool ZipFile(string fileToZip, string zipedFile, string password) { bool successed = false; if (File.Exists(fileToZip)) { try { using (ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipedFile))) { if (!string.IsNullOrEmpty(password)) { zipStream.Password = password; } zipStream.SetLevel(6); ZipEntry theEntry = new ZipEntry(Path.GetFileName(fileToZip)); zipStream.PutNextEntry(theEntry); using (FileStream fileStream = File.OpenRead(fileToZip)) { int size = 2048; byte[] data = new byte[size]; while ((size = fileStream.Read(data, 0, data.Length)) > 0) { zipStream.Write(data, 0, size); } } } successed = true; } catch { successed = false; } } return successed; } /// <summary> /// 压缩文件 /// </summary> /// <param name="fileToZip">要压缩的文件全名</param> /// <param name="zipedFile">压缩后的文件名</param> /// <returns>压缩结果</returns> public static bool ZipFile(string fileToZip, string zipedFile) { return ZipFile(fileToZip, zipedFile, string.Empty); } /// <summary> /// 压缩文件或文件夹 /// </summary> /// <param name="fileToZip">要压缩的路径</param> /// <param name="zipedFile">压缩后的文件名</param> /// <param name="password">密码</param> /// <returns>压缩结果</returns> 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; } /// <summary> /// 压缩文件或文件夹 /// </summary> /// <param name="fileToZip">要压缩的路径</param> /// <param name="zipedFile">压缩后的文件名</param> /// <returns>压缩结果</returns> public static bool Zip(string fileToZip, string zipedFile) { return Zip(fileToZip, zipedFile, string.Empty); } /// <summary> /// 解压功能(解压压缩文件到指定目录) /// </summary> /// <param name="zipFilePath">待解压的文件</param> /// <param name="zipedFolder">指定解压目标目录</param> /// <param name="password">密码</param> /// <returns>解压结果</returns> public static bool UnZip(string zipFilePath, string zipedFolder, string password) { bool successed = false; if (File.Exists(zipFilePath)) { try { using (ZipInputStream zipStream = new ZipInputStream(File.OpenRead(zipFilePath))) { if (!string.IsNullOrEmpty(password)) { zipStream.Password = password; } ZipEntry theEntry; while ((theEntry = zipStream.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(theEntry.Name); string fileName = Path.GetFileName(theEntry.Name); if (directoryName.Length > 0 && !Directory.Exists(Path.Combine(zipedFolder, directoryName))) { Directory.CreateDirectory(Path.Combine(zipedFolder, directoryName)); } if (fileName != string.Empty) { using (FileStream fileStreamm = File.Create(Path.Combine(zipedFolder, theEntry.Name))) { int size = 2048; byte[] data = new byte[size]; while ((size = zipStream.Read(data, 0, data.Length)) > 0) { fileStreamm.Write(data, 0, size); } } } } } successed = true; } catch { successed = false; } } return successed; } /// <summary> /// 解压功能(解压压缩文件到指定目录) /// </summary> /// <param name="zipFilePath">待解压的文件</param> /// <param name="zipedFolder">指定解压目标目录</param> /// <returns>解压结果</returns> public static bool UnZip(string zipFilePath, string zipedFolder) { return UnZip(zipFilePath, zipedFolder, string.Empty); } } }