解压zip文件
/// <summary> /// 执行压缩命令结果 /// </summary> public enum CompressResults { Success, SourceObjectNotExist, UnKnown } /// <summary> /// 执行解压缩命令结果 /// </summary> public enum UnCompressResults { Success, SourceObjectNotExist, PasswordError, UnKnown } /// <summary> /// 进程运行结果 /// </summary> //public enum ProcessResults //{ // Success, // Failed //} public class ZipOperate { #region 单例模式 private static ZipOperate uniqueInstance; private static object _lock = new object(); //private ZipOperate() { } public static ZipOperate getInstance() { if (null == uniqueInstance) //确认要实例化后再进行加锁,降低加锁的性能消耗。 { lock (_lock) { if (null == uniqueInstance) { uniqueInstance = new ZipOperate(); } } } return uniqueInstance; } #endregion #region 7zZip压缩、解压方法 /// <summary> /// 压缩文件 /// </summary> /// <param name="objectPathName">压缩对象(即可以是文件夹|也可以是文件)</param> /// <param name="objectZipPathName">保存压缩文件的路径</param> /// <param name="strPassword">加密码</param> /// 测试压缩文件夹:压缩文件(objectZipPathName)不能放在被压缩文件(objectPathName)内,否则报“文件夹被另一进程使用中”错误。 /// <returns></returns> public CompressResults Compress7zZip(String objectPathName, String objectZipPathName, String strPassword) { try { //http://sevenzipsharp.codeplex.com/releases/view/51254 下载sevenzipsharp.dll //SevenZipSharp.dll、zLib1.dll、7z.dll必须同时存在,否则常报“加载7z.dll错误” string libPath = @"E:\yingyong\Uncompress_7zip\Uncompress_7zip\bin\Debug\7z.dll";//System.AppDomain.CurrentDomain.BaseDirectory + @"..\..\dll\7z.dll"; SevenZipCompressor.SetLibraryPath(libPath); SevenZipCompressor sevenZipCompressor = new SevenZipCompressor(); sevenZipCompressor.CompressionLevel = CompressionLevel.Fast; sevenZipCompressor.ArchiveFormat = OutArchiveFormat.Zip; //被压缩对象是否存在 int beforeObjectNameIndex = objectPathName.LastIndexOf('\\'); string objectPath = objectPathName.Substring(0, beforeObjectNameIndex); //System.IO.DirectoryInfo directoryInfo = new System.IO.DirectoryInfo(objectPathName); if (Directory.Exists(objectPathName)/*directoryInfo.Exists*/ == false && System.IO.File.Exists(objectPathName) == false) { return CompressResults.SourceObjectNotExist; } int beforeObjectRarNameIndex = objectZipPathName.LastIndexOf('\\'); int objectRarNameIndex = beforeObjectRarNameIndex + 1; //string objectZipName = objectZipPathName.Substring(objectRarNameIndex); string objectZipPath = objectZipPathName.Substring(0, beforeObjectRarNameIndex); //目标目录、文件是否存在 if (Directory.Exists(objectZipPath) == false) { Directory.CreateDirectory(objectZipPath); } else if (File.Exists(objectZipPathName) == true) { File.Delete(objectZipPathName); } if (Directory.Exists(objectPathName)) //压缩对象是文件夹 { if (String.IsNullOrEmpty(strPassword)) { sevenZipCompressor.CompressDirectory(objectPathName, objectZipPathName); } else { sevenZipCompressor.CompressDirectory(objectPathName, objectZipPathName, strPassword); } } else //压缩对象是文件 无加密方式 { sevenZipCompressor.CompressFiles(objectZipPathName, objectPathName); } return CompressResults.Success; } catch (Exception ex) { //throw new Exception(ex.Message) ; return CompressResults.UnKnown; } } /// <summary> /// 解压缩文件 /// </summary> /// <param name="zipFilePathName">zip文件具体路径+名</param> /// <param name="unCompressDir">解压路径</param> /// <param name="strPassword">解密码</param> /// <returns></returns> public UnCompressResults UnCompress7zZip(String zipFilePathName, String unCompressDir, String strPassword) { try { //SevenZipSharp.dll、zLib1.dll、7z.dll必须同时存在,否则常报“加载7z.dll错误”而项目引用时,只引用SevenZipSharp.dll就可以了 //string libPath = System.AppDomain.CurrentDomain.BaseDirectory + @"..\..\7z.dll"; string libPath = "7z.dll"; SevenZip.SevenZipCompressor.SetLibraryPath(libPath); bool isFileExist = File.Exists(zipFilePathName); if (false == isFileExist) { //MessageBox.Show("解压文件不存在!" + zipFilePathName); return UnCompressResults.SourceObjectNotExist; } File.SetAttributes(zipFilePathName, FileAttributes.Normal); //去掉只读属性 if (Directory.Exists(unCompressDir) == false) { Directory.CreateDirectory(unCompressDir); } SevenZip.SevenZipExtractor sevenZipExtractor; if (String.IsNullOrEmpty(strPassword)) { sevenZipExtractor = new SevenZip.SevenZipExtractor(zipFilePathName); } else { sevenZipExtractor = new SevenZip.SevenZipExtractor(zipFilePathName, strPassword); } sevenZipExtractor.ExtractArchive(unCompressDir); sevenZipExtractor.Dispose(); return UnCompressResults.Success; } catch (Exception ex) { //MessageBox.Show("解压缩文件失败!" + ex.Message); return UnCompressResults.UnKnown; } } #endregion }
此方法需要引用sevenzipsharp.dll,同时在需要zlib1.dll和7z.dll两个库,这两个不需要引用,但是要跟sevenzipsharp.dll放在同一个文件夹下。
注:代码中
string libPath=... 要改为string libPath =“7z.dll“,或者为:”
string libPath =Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory,“7z.dll”);