using System; using System.Collections.Generic; using System.Text; using SevenZip; using System.IO; using System.Windows.Forms; namespace TestZip { internal static class Zip { public static bool Init() { string _7z32 = Application.StartupPath+ "\\7z32.dll"; string _7z64 = Application.StartupPath + "\\7z64.dll"; string _7z = _7z32; try { if (IntPtr.Size == 8) _7z = _7z64; SevenZipExtractor.SetLibraryPath(_7z); SevenZipCompressor.SetLibraryPath(_7z); return true; } catch { return false; } } /// <summary> /// 解压 /// </summary> /// <param name="zip">解压的zip文件</param> /// <param name="folder">解压到某个文件夹</param> /// <returns></returns> public static bool UnZip(string zip, string folder) { try { var tmp = new SevenZipExtractor(zip); if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } tmp.ExtractArchive(folder); return true; } catch(Exception ex) { return false; } } /// <summary> /// 压缩 /// </summary> /// <param name="zip">压缩文件新的保存路径</param> /// <param name="fileFullNames">需要压缩文件所在路径(多个需要压缩的文件)</param> /// <returns></returns> public static bool ToZip(string zip, params string[] fileFullNames) { try { var tmp = new SevenZipCompressor(); tmp.CompressFiles(zip, fileFullNames); return true; } catch { return false; } } /// <summary> /// 压缩 /// </summary> /// <param name="folder">需要压缩文件所在路径</param> /// <param name="zip">压缩文件新的保存路径</param> /// <returns></returns> public static bool ToZip(string folder, string zip) { try { var tmp = new SevenZipCompressor(folder); tmp.CompressDirectory(folder, zip); return true; } catch(Exception ex) { return false; } } } }