using System.Linq; using System.Text; using System.IO; using ICSharpCode.SharpZipLib.Zip; using System; namespace Estsh.Core.Util { public class ZipHelper { /// /// 压缩文件和文件夹 /// /// 待压缩的文件或文件夹,全路径格式 /// 压缩后生成的压缩文件名,全路径格式 /// 压缩文件的密码 /// public static bool Zip(String FileToZip, String ZipedFile, String Password) { if (Directory.Exists(FileToZip)) { return ZipFileDictory(FileToZip, ZipedFile, Password); } else if (File.Exists(FileToZip)) { return ZipFile(FileToZip, ZipedFile, Password); } else { return false; } } /// /// 压缩目录 /// /// 待压缩的文件夹,全路径格式 /// 压缩后的文件名,全路径格式 /// 压缩文件的密码 /// private static bool ZipFileDictory(string FolderToZip, string ZipedFile, String Password) { bool res = true; if (!Directory.Exists(FolderToZip)) { return false; } ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFile)); s.Password = Password; s.SetLevel(6); _CompressFolder(FolderToZip, s, FolderToZip); s.Finish(); s.Close(); return res; } /// /// 压缩文件 /// /// 要进行压缩的文件名 /// 压缩后生成的压缩文件名 /// private static bool ZipFile(string FileToZip, string ZipedFile, String Password) { bool res = true; //如果文件没有找到,则报错 if (!File.Exists(FileToZip)) { throw new System.IO.FileNotFoundException("指定要压缩的文件: " + FileToZip + " 不存在!"); } ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFile)); s.Password = Password; s.SetLevel(6); _AddFile(FileToZip, s, FileToZip); s.Finish(); s.Close(); return res; } /// /// 压缩单独文件 /// /// 要进行压缩的文件名 /// 压缩后文件的输出流 /// 被压缩目录名称 private static void _CreateZipFile(string FileToZip, ZipOutputStream zips, string zipfolername) { FileStream StreamToZip = null; ZipEntry ZipEn = null; try { StreamToZip = new FileStream(FileToZip, FileMode.Open, FileAccess.Read); FileToZip = FileToZip.Substring(zipfolername.LastIndexOf("\\") + 1); ZipEn = new ZipEntry(FileToZip); zips.PutNextEntry(ZipEn); byte[] buffer = new byte[16384]; System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length); zips.Write(buffer, 0, size); while (size < StreamToZip.Length) { int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length); zips.Write(buffer, 0, sizeRead); size += sizeRead; } } catch (Exception e) { throw e; } finally { if (StreamToZip != null) { StreamToZip.Close(); StreamToZip = null; } if (ZipEn != null) { ZipEn = null; } } } /// /// 压缩某个子文件 /// /// /// /// private static void _AddFile(string fileName, ZipOutputStream zips, string zipfolername) { if (File.Exists(fileName)) { _CreateZipFile(fileName, zips, zipfolername); } } /// /// 压缩某个子文件夹 /// /// /// /// private static void _CompressFolder(string basePath, ZipOutputStream zips, string zipfolername) { if (File.Exists(basePath)) { _AddFile(basePath, zips, zipfolername); return; } string[] names = Directory.GetFiles(basePath); foreach (string fileName in names) { _AddFile(fileName, zips, zipfolername); } names = Directory.GetDirectories(basePath); foreach (string folderName in names) { _CompressFolder(folderName, zips, zipfolername); } } /// /// 解压功能(解压压缩文件到指定目录) /// /// 待解压的文件 /// 指定解压目标目录 public static void UnZip(string FileToUpZip, string ZipedFolder, string Password) { byte[] buffer = new byte[2048]; while (ZipedFolder.LastIndexOf("\\") + 1 == ZipedFolder.Length)//检查路径是否以"\"结尾 { ZipedFolder = ZipedFolder.Substring(0, ZipedFolder.Length - 1);//如果是则去掉末尾的"\" } using (ZipInputStream zipStream = new ZipInputStream(File.OpenRead(FileToUpZip))) { //判断Password if (Password != null && Password.Length > 0) { zipStream.Password = Password; } ZipEntry zipEntry = null; while ((zipEntry = zipStream.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(zipEntry.Name); string fileName = Path.GetFileName(zipEntry.Name); if (!string.IsNullOrEmpty(directoryName) && !Directory.Exists(ZipedFolder + @"\" + directoryName)) { Directory.CreateDirectory(ZipedFolder + @"\" + directoryName); } if (!string.IsNullOrEmpty(fileName)) { if (zipEntry.CompressedSize == 0) break; if (zipEntry.IsDirectory)//如果压缩格式为文件夹方式压缩 { directoryName = Path.GetDirectoryName(ZipedFolder + @"\" + zipEntry.Name); Directory.CreateDirectory(directoryName); } else//2012-5-28修改,支持单个文件压缩时自己创建目标文件夹 { if (!Directory.Exists(ZipedFolder)) { Directory.CreateDirectory(ZipedFolder); } } using (FileStream stream = File.Create(ZipedFolder + @"\" + zipEntry.Name)) { while (true) { int size = zipStream.Read(buffer, 0, buffer.Length); if (size > 0) { stream.Write(buffer, 0, size); } else { break; } } } } } } } } }