using ICSharpCode.SharpZipLib.Zip; using Microsoft.Win32; using System; using System.Configuration; using System.Diagnostics; using System.IO; using System.Linq; namespace VRS.Util { public class ZipHelper { /// /// 压缩多个文件目录 /// /// 需要压缩的目录 /// 压缩后的文件名 /// 压缩等级 /// 密码 public static void ZipDir(string dirname, string zipFile, int level = 5, string password = "123") { ZipOutputStream zos = new ZipOutputStream(File.Create(zipFile)); zos.Password = password; zos.SetLevel(level); addZipEntry(dirname, zos, dirname); zos.Finish(); zos.Close(); } /// /// 往压缩文件里面添加Entry /// /// 文件路径 /// ZipOutputStream /// 基础目录 private static void addZipEntry(string PathStr, ZipOutputStream zos, string BaseDirName) { DirectoryInfo dir = new DirectoryInfo(PathStr); foreach (FileSystemInfo item in dir.GetFileSystemInfos()) { if ((item.Attributes & FileAttributes.Directory) == FileAttributes.Directory)//如果是文件夹继续递归 { addZipEntry(item.FullName, zos, BaseDirName); } else { FileInfo f_item = (FileInfo)item; using (FileStream fs = f_item.OpenRead()) { byte[] buffer = new byte[(int)fs.Length]; fs.Position = 0; fs.Read(buffer, 0, buffer.Length); fs.Close(); ZipEntry z_entry = new ZipEntry(item.FullName.Replace(BaseDirName, "")); zos.PutNextEntry(z_entry); zos.Write(buffer, 0, buffer.Length); } } } } #region 压缩多个文件 #endregion 压缩多个文件 #region 解压文件 包括.rar 和zip /// ///解压文件 /// /// 解压前的文件路径(绝对路径) /// 解压后的文件目录(绝对路径) public static void UnpackFileRarOrZip(string fileFromUnZip, string fileToUnZip) { //获取压缩类型 string unType = fileFromUnZip.Substring(fileFromUnZip.LastIndexOf(".") + 1, 3).ToLower(); switch (unType) { case "rar": UnRar(fileFromUnZip, fileToUnZip); break; default: UnZip(fileFromUnZip, fileToUnZip); break; } } #endregion 解压文件 包括.rar 和zip #region 解压文件 .rar文件 /// /// 解压 /// /// /// /// /// public static void UnRar(string fileFromUnZip, string fileToUnZip) { string the_rar; RegistryKey the_Reg; object the_Obj; string the_Info; try { the_Reg = Registry.LocalMachine.OpenSubKey( @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe"); the_Obj = the_Reg.GetValue(""); the_rar = the_Obj.ToString(); the_Reg.Close(); //the_rar = the_rar.Substring(1, the_rar.Length - 7); if (Directory.Exists(fileToUnZip) == false) { Directory.CreateDirectory(fileToUnZip); } the_Info = "x " + Path.GetFileName(fileFromUnZip) + " " + fileToUnZip + " -y"; ProcessStartInfo the_StartInfo = new ProcessStartInfo(); the_StartInfo.FileName = the_rar; the_StartInfo.Arguments = the_Info; the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden; the_StartInfo.WorkingDirectory = Path.GetDirectoryName(fileFromUnZip);//获取压缩包路径 Process the_Process = new Process(); the_Process.StartInfo = the_StartInfo; the_Process.StartInfo.UseShellExecute = false; the_Process.Start(); the_Process.WaitForExit(); the_Process.Close(); } catch (Exception ex) { throw ex; } //return unRarPatch; } #endregion 解压文件 .rar文件 #region 修改上传成功的文件夹名称 public static void ChangeFileName(string oldFileName) { string newFileName = oldFileName + "_完成"; if (System.IO.Directory.Exists(oldFileName)) { System.IO.DirectoryInfo folder = new System.IO.DirectoryInfo(oldFileName); folder.MoveTo(newFileName); } } #endregion 修改上传成功的文件夹名称 #region 解压文件 .zip文件 /// /// 解压功能(解压压缩文件到指定目录) /// /// 待解压的文件 /// 指定解压目标目录 public static void UnZip(string FileToUpZip, string ZipedFolder) { if (!File.Exists(FileToUpZip)) { return; } if (!Directory.Exists(ZipedFolder)) { Directory.CreateDirectory(ZipedFolder); } ICSharpCode.SharpZipLib.Zip.ZipEntry theEntry = null; string fileName; using (ICSharpCode.SharpZipLib.Zip.ZipInputStream s = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(File.OpenRead(FileToUpZip))) { string extractKey = ConfigurationManager.AppSettings["ExtractKey"]; if (!string.IsNullOrEmpty(extractKey)) { s.Password = extractKey; } while ((theEntry = s.GetNextEntry()) != null) { if (theEntry.Name != String.Empty) { fileName = Path.Combine(ZipedFolder, theEntry.Name); ///判断文件路径是否是文件夹如果是文件夹就直接跳过不处理 if (fileName.EndsWith("/") || fileName.EndsWith("\\")) { //Directory.CreateDirectory(fileName); continue; } // 根据文件目录创建文件所在的根目录路径 if (!Directory.Exists(Path.GetDirectoryName(fileName))) { Directory.CreateDirectory(Path.GetDirectoryName(fileName)); } using (FileStream streamWriter = File.Create(fileName)) { int size = 2048; byte[] data = new byte[2048]; while ((size = s.Read(data, 0, data.Length)) > 0) { streamWriter.Write(data, 0, size); } } } } } } #endregion 解压文件 .zip文件 /// /// 解压功能(解压压缩文件到指定目录) /// /// 待解压的文件 /// 指定解压目标目录 /// 密码 /// 解压结果 public static bool UnZipNew(string fileToUnZip, string zipedFolder, string password) { bool result = true; ZipInputStream zipStream = null; ZipEntry ent = null; string fileName; if (!File.Exists(fileToUnZip)) return false; if (!Directory.Exists(zipedFolder)) Directory.CreateDirectory(zipedFolder); try { zipStream = new ZipInputStream(File.OpenRead(fileToUnZip)); if (!string.IsNullOrEmpty(password)) zipStream.Password = password; while ((ent = zipStream.GetNextEntry()) != null) { if (!string.IsNullOrEmpty(ent.Name)) { fileName = Path.Combine(zipedFolder, ent.Name); fileName = fileName.Replace('/', '\\');//change by Mr.HopeGi if (!Directory.Exists(Path.GetDirectoryName(fileName))) { Directory.CreateDirectory(Path.GetDirectoryName(fileName)); } using (System.IO.FileStream fs = File.Create(fileName)) { int size = 2048; byte[] data = new byte[size]; while (true) { size = zipStream.Read(data, 0, data.Length); if (size > 0) fs.Write(data, 0, size); else break; } fs.Flush(); fs.Close(); fs.Dispose(); } } } } catch (Exception ex) { throw ex; //result = false; } finally { if (zipStream != null) { zipStream.Close(); zipStream.Dispose(); } if (ent != null) { ent = null; } GC.Collect(); GC.Collect(1); } return result; } public static bool IsZip(string file) { try { ZipFile zip = new ZipFile(file); zip.Password = null; zip.Close(); return true; } catch { return false; } } } }