using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; using System.IO; using ICSharpCode.SharpZipLib.Zip; namespace Test { public class ZIP { /// <summary> /// 压缩文件 /// </summary> /// <param name="fileName">待压缩文件完整路径</param> /// <param name="srcName">压缩后文件完整路径</param> public static string ZipFile(string srcName, string zipName) { srcName = new Regex("[\/]+").Replace(srcName, "/"); zipName = new Regex("[\/]+").Replace(zipName, "/"); string errorMsg = ""; try { if (!File.Exists(srcName)) { throw new Exception("指定的压缩文件不存在!"); } ZipOutputStream zos = new ZipOutputStream(File.Create(zipName)); AddZipEntry(srcName, srcName.Substring(0, srcName.LastIndexOf("/")), zos, out zos); zos.Close(); } catch (Exception ex) { errorMsg = ex.Message; } return errorMsg; } /// <summary> /// 压缩目录 /// </summary> /// <param name="directory">待压缩目录</param> /// <param name="zipName">压缩后文件完整路径</param> public static string ZipDirectory(string directory, string zipName) { directory = new Regex("[\/]+").Replace(directory, "/"); zipName = new Regex("[\/]+").Replace(zipName, "/"); string errorMsg = ""; try { if (!Directory.Exists(directory)) { throw new Exception("指定的压缩目录不存在!"); } ZipOutputStream zos = new ZipOutputStream(File.Create(zipName)); DirectoryInfo di = new DirectoryInfo(directory); foreach (DirectoryInfo item in di.GetDirectories()) { AddZipEntry(item.FullName, directory, zos, out zos); } foreach (FileInfo item in di.GetFiles()) { AddZipEntry(item.FullName, directory, zos, out zos); } zos.Close(); } catch (Exception ex) { errorMsg = ex.Message; } return errorMsg; } /// <summary> /// 添加压缩项 /// </summary> /// <param name="name">目录/文件完整路径</param> /// <param name="zos1"></param> /// <param name="zos2"></param> private static void AddZipEntry(string name, string rootPath, ZipOutputStream zos1, out ZipOutputStream zos2) { ZipEntry ze = null; //若待压缩项是个目录 if (Directory.Exists(name)) { DirectoryInfo di = new DirectoryInfo(name); if (di.GetDirectories().Length == 0) //添加空目录 { ze = new ZipEntry(GetFilePath(name, rootPath) + "/"); zos1.PutNextEntry(ze); } foreach (DirectoryInfo item in di.GetDirectories()) //添加子目录 { ze = new ZipEntry(GetFilePath(item.FullName, rootPath) + "/"); zos1.PutNextEntry(ze); AddZipEntry(item.FullName, rootPath, zos1, out zos1); //递归添加目录 } foreach (FileInfo item in di.GetFiles()) { AddZipEntry(item.FullName, rootPath, zos1, out zos2); //递归添加文件 } } //若待压缩项是个文件 if (File.Exists(name)) { zos1.SetLevel(9); FileStream fs = File.OpenRead(name); int index = 0; byte[] bs = new byte[2048]; ze = new ZipEntry(GetFilePath(name, rootPath)); zos1.PutNextEntry(ze); while ((index = fs.Read(bs, 0, 2048)) != 0) { zos1.Write(bs, 0, index); } fs.Close(); } zos2 = zos1; } /// <summary> /// 解压缩文件 /// </summary> /// <param name="zipFile">待解压文件</param> /// <param name="dePath">解压缩至dePath目录下</param> public static string UnZipFile(string zipFile, string dePath) { zipFile = new Regex("[\/]+").Replace(zipFile, "/"); dePath = new Regex("[\/]+").Replace(dePath, "/"); string errorMsg = ""; try { if (!File.Exists(zipFile)) { throw new Exception("待解压文件不存在!"); } if (!Directory.Exists(dePath)) { Directory.CreateDirectory(dePath); } ZipInputStream zis = new ZipInputStream(File.OpenRead(zipFile)); ZipEntry ze = null; string directoryPath = ""; //目录完整路径 string filePath = ""; //文件完整路径 Regex regex = new Regex("[/\\]+"); FileStream fs = null; byte[] bs = new byte[2048]; int index = 0; while ((ze = zis.GetNextEntry()) != null) { if (ze.IsDirectory) //如果是目录 { directoryPath = dePath + "/" + ze.Name.Substring(0, ze.Name.LastIndexOf("/")); directoryPath = regex.Replace(directoryPath, "/"); if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } } else // 否则为文件 { if (ze.Crc != 00000000L) ////此ZipEntry不是标记文件 { filePath = dePath + "/" + ze.Name; filePath = regex.Replace(filePath, "/"); directoryPath = IOUtils.GetFilePath(filePath); if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } fs = File.Open(filePath, FileMode.Create, FileAccess.Write); while ((index = zis.Read(bs, 0, 2048)) != 0) { fs.Write(bs, 0, index); } fs.Close(); } } } zis.Close(); } catch (Exception ex) { errorMsg = ex.Message; } return errorMsg; } /// <summary> /// 获取文件相对路径 /// </summary> private static string GetFilePath(string filePath, string rootPath) { rootPath = new Regex("/+").Replace(rootPath + "/", "/"); filePath = new Regex("/+").Replace(filePath, "/"); return filePath.Replace(rootPath, ""); } } }