using ICSharpCode.SharpZipLib.Checksums; using ICSharpCode.SharpZipLib.Zip; using Microsoft.Win32; /// <summary> /// 文件压缩解压缩 /// /// 修改纪录 /// /// 2015-6-23版本:1.0 SongBiao 创建文件。 /// /// <author> /// <name>SongBiao</name> /// <date>2015-6-23</date> /// </author> /// </summary> public class SharpZip { /// <summary> /// 压缩 /// </summary> /// <param name="filename"> 压缩后的文件名(包含物理路径)</param> /// <param name="directory">待压缩的文件夹(包含物理路径)</param> public static void PackFiles(string filename, string directory) { FastZip fz = new FastZip { CreateEmptyDirectories = true }; fz.CreateZip(filename, directory, true, ""); } /// <summary> /// 解压缩 /// </summary> /// <param name="file">待解压文件名(包含物理路径)</param> /// <param name="dir"> 解压到哪个目录中(包含物理路径)</param> public static bool UnpackFiles(string file, string dir) { if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } ZipInputStream s = new ZipInputStream(File.OpenRead(file)); ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(theEntry.Name); string fileName = Path.GetFileName(theEntry.Name); if (directoryName != String.Empty) { Directory.CreateDirectory(dir + directoryName); } if (fileName != String.Empty) { FileStream streamWriter = File.Create(dir + theEntry.Name); byte[] data = new byte[2048]; while (true) { var size = s.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else { break; } } streamWriter.Close(); } } s.Close(); return true; } } public class ClassZip { #region 私有方法 /// <summary> /// 递归压缩文件夹方法 /// </summary> private static bool ZipFileDictory(string folderToZip, ZipOutputStream s, string parentFolderName) { bool res = true; ZipEntry entry = null; FileStream fs = null; Crc32 crc = new Crc32(); try { entry = new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/")); s.PutNextEntry(entry); s.Flush(); var filenames = Directory.GetFiles(folderToZip); foreach (string file in filenames) { fs = File.OpenRead(file); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); entry = new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/" + Path.GetFileName(file))) { DateTime = DateTime.Now, Size = fs.Length }; fs.Close(); crc.Reset(); crc.Update(buffer); entry.Crc = crc.Value; s.PutNextEntry(entry); s.Write(buffer, 0, buffer.Length); } } catch { res = false; } finally { if (fs != null) { fs.Close(); } if (entry != null) { } GC.Collect(); GC.Collect(1); } var folders = Directory.GetDirectories(folderToZip); if (folders.Any(folder => folderToZip != null && !ZipFileDictory(folder, s, Path.Combine(parentFolderName, Path.GetFileName(folderToZip))))) { return false; } return res; } /// <summary> /// 压缩目录 /// </summary> /// <param name="folderToZip">待压缩的文件夹,全路径格式</param> /// <param name="zipedFile">压缩后的文件名,全路径格式</param> /// <param name="level"></param> private static bool ZipFileDictory(string folderToZip, string zipedFile, int level) { if (!Directory.Exists(folderToZip)) { return false; } ZipOutputStream s = new ZipOutputStream(File.Create(zipedFile)); s.SetLevel(level); var res = ZipFileDictory(folderToZip, s, ""); s.Finish(); s.Close(); return res; } /// <summary> /// 压缩文件 /// </summary> /// <param name="fileToZip">要进行压缩的文件名</param> /// <param name="zipedFile">压缩后生成的压缩文件名</param> /// <param name="level"></param> private static bool ZipFile(string fileToZip, string zipedFile, int level) { if (!File.Exists(fileToZip)) { throw new FileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!"); } FileStream zipFile = null; ZipOutputStream zipStream = null; ZipEntry zipEntry = null; bool res = true; try { zipFile = File.OpenRead(fileToZip); byte[] buffer = new byte[zipFile.Length]; zipFile.Read(buffer, 0, buffer.Length); zipFile.Close(); zipFile = File.Create(zipedFile); zipStream = new ZipOutputStream(zipFile); zipEntry = new ZipEntry(Path.GetFileName(fileToZip)); zipStream.PutNextEntry(zipEntry); zipStream.SetLevel(level); zipStream.Write(buffer, 0, buffer.Length); } catch { res = false; } finally { if (zipEntry != null) { } if (zipStream != null) { zipStream.Finish(); zipStream.Close(); } if (zipFile != null) { zipFile.Close(); } GC.Collect(); GC.Collect(1); } return res; } #endregion /// <summary> /// 压缩 /// </summary> /// <param name="fileToZip">待压缩的文件目录</param> /// <param name="zipedFile">生成的目标文件</param> /// <param name="level">6</param> public static bool Zip(String fileToZip, String zipedFile, int level) { if (Directory.Exists(fileToZip)) { return ZipFileDictory(fileToZip, zipedFile, level); } else if (File.Exists(fileToZip)) { return ZipFile(fileToZip, zipedFile, level); } else { return false; } } /// <summary> /// 解压 /// </summary> /// <param name="fileToUpZip">待解压的文件</param> /// <param name="zipedFolder">解压目标存放目录</param> public static void UnZip(string fileToUpZip, string zipedFolder) { if (!File.Exists(fileToUpZip)) { return; } if (!Directory.Exists(zipedFolder)) { Directory.CreateDirectory(zipedFolder); } ZipInputStream s = null; ZipEntry theEntry = null; FileStream streamWriter = null; try { s = new ZipInputStream(File.OpenRead(fileToUpZip)); while ((theEntry = s.GetNextEntry()) != null) { if (theEntry.Name != String.Empty) { var fileName = Path.Combine(zipedFolder, theEntry.Name); if (fileName.EndsWith("/") || fileName.EndsWith("\")) { Directory.CreateDirectory(fileName); continue; } streamWriter = File.Create(fileName); byte[] data = new byte[2048]; while (true) { var size = s.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else { break; } } } } } finally { if (streamWriter != null) { streamWriter.Close(); } if (theEntry != null) { } if (s != null) { s.Close(); } GC.Collect(); GC.Collect(1); } } } public class ZipHelper { #region 私有变量 String _theRar; RegistryKey _theReg; Object _theObj; String _theInfo; ProcessStartInfo _theStartInfo; Process _theProcess; #endregion /// <summary> /// 压缩 /// </summary> /// <param name="zipname">要解压的文件名</param> /// <param name="zippath">要压缩的文件目录</param> /// <param name="dirpath">初始目录</param> public void EnZip(string zipname, string zippath, string dirpath) { try { _theReg = Registry.ClassesRoot.OpenSubKey(@"ApplicationsWinRAR.exeShellOpenCommand"); if (_theReg != null) { _theObj = _theReg.GetValue(""); _theRar = _theObj.ToString(); _theReg.Close(); } _theRar = _theRar.Substring(1, _theRar.Length - 7); _theInfo = " a " + zipname + " " + zippath; _theStartInfo = new ProcessStartInfo { FileName = _theRar, Arguments = _theInfo, WindowStyle = ProcessWindowStyle.Hidden, WorkingDirectory = dirpath }; _theProcess = new Process { StartInfo = _theStartInfo }; _theProcess.Start(); } catch (Exception ex) { throw new Exception(ex.Message); } } /// <summary> /// 解压缩 /// </summary> /// <param name="zipname">要解压的文件名</param> /// <param name="zippath">要解压的文件路径</param> public void DeZip(string zipname, string zippath) { try { _theReg = Registry.ClassesRoot.OpenSubKey(@"ApplicationsWinRar.exeShellOpenCommand"); if (_theReg != null) { _theObj = _theReg.GetValue(""); _theRar = _theObj.ToString(); _theReg.Close(); } _theRar = _theRar.Substring(1, _theRar.Length - 7); _theInfo = " X " + zipname + " " + zippath; _theStartInfo = new ProcessStartInfo { FileName = _theRar, Arguments = _theInfo, WindowStyle = ProcessWindowStyle.Hidden }; _theProcess = new Process { StartInfo = _theStartInfo }; _theProcess.Start(); } catch (Exception ex) { throw new Exception(ex.Message); } } }