/// <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(, 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 + ;
//string objectZipName = objectZipPathName.Substring(objectRarNameIndex);
string objectZipPath = objectZipPathName.Substring(, 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”);

解压zip文件的更多相关文章

  1. 通过javascript在网页端解压zip文件并查看压缩包内容

    WEB前端解压ZIP压缩包 web前端解压zip文件有什么用: 只考虑标准浏览器的话, 服务器只要传输压缩包到客户端, 节约了带宽, 而且节约了传输时间, 听起来好像很厉害的说:     如果前端的代 ...

  2. Android 解压zip文件(支持中文)

    过了n多天后,当再次使用原先博客上写的那篇: Android 压缩解压zip文件 去做zip包的解压的时候,出现了原来没有发现的很多问题.首先是中文汉字问题,使用java的zip包不能很好的解决解压问 ...

  3. (转载)C#压缩解压zip 文件

    转载之: C#压缩解压zip 文件 - 大气象 - 博客园http://www.cnblogs.com/greatverve/archive/2011/12/27/csharp-zip.html C# ...

  4. Android 解压zip文件你知道多少?

    对于Android常用的压缩格式ZIP,你了解多少? Android的有两种解压ZIP的方法,你知道吗? ZipFile和ZipInputStream的解压效率,你对比过吗? 带着以上问题,现在就开始 ...

  5. java实现解压zip文件,(亲测可用)!!!!!!

    项目结构: Util.java内容: package com.cfets.demo; import java.io.File; import java.io.FileOutputStream; imp ...

  6. Android 解压zip文件

    过了n多天后,当再次使用原先博客上写的那篇: Android 压缩解压zip文件 去做zip包的解压的时候,出现了原来没有发现的很多问题.首先是中文汉字问题,使用java的zip包不能很好的解决解压问 ...

  7. python用zipfile模块打包文件或是目录、解压zip文件实例

    #!/usr/bin/env python # -*- coding: utf-8 -*- from zipfile import * import zipfile #解压zip文件 def unzi ...

  8. AIX解压ZIP文件

    AIX系统自身是没有解压ZIP文件的,但在AIX安装oracle数据库服务器的话,在$ORACLE_HOME/bin路径下方却有unzip命令,可以解压ZIP文件. 一.shell脚本   之前的版本 ...

  9. linux 解压zip文件

    linux 解压zip文件 学习了:https://blog.csdn.net/hbcui1984/article/details/1583796 unzip xx.zip

  10. Java 上传解压zip文件,并且解析文件里面的excel和图片

    需求:上传一个zip文件,zip文件里面包含一个excel和很多图片,需要把excel里面的信息解析出来保存到表中,同时图片也转化成base64保存到数据库表中. PS:为了方便不同水平的开发人员阅读 ...

随机推荐

  1. 《图解HTTP》读后记

    看了一遍又一遍…………不如记一下 用做笔记的方式来看,发现了好多之前没发现的知识点.....感觉前几次看了跟没看一样... 1.1HTTP通常被译为超文本传输协议,但这种译法并不严谨.严谨的译名应该为 ...

  2. FIT文件CRC校验

    校验FIT文件CRC代码做个记录,分为两步先校验头部然后再校验整个FIT文件.校验头部不是必需的看个人需要吧.为了偷懒使用Okio库,还有计算CRC的时候用的Garmin的FitSDK. public ...

  3. @Data注解getset不起作用

    在idea的setting安装Lombok插件,重启idea

  4. springboot 集成Redis一主二从三哨兵

    1.Centos7 Redis一主二从三哨兵配置 Redis一主二从三哨兵环境搭建 2.接入过程 与集成redis单机不同的是jedis相关的配置做了修改,JedisPool换成了JedisSenti ...

  5. 【福利】离散&C++&硬件一笔记合集

    离散 C++ 硬件

  6. js中对于数组的操作

    let myArray=[11,22,33]; console.log('原数组:',myArray); myArray.push(44,55); console.log('用push在数组后面插入元 ...

  7. Spring Boot (九): 微服务应用监控 Spring Boot Actuator 详解

    1. 引言 在当前的微服务架构方式下,我们会有很多的服务部署在不同的机器上,相互是通过服务调用的方式进行交互,一个完整的业务流程中间会经过很多个微服务的处理和传递,那么,如何能知道每个服务的健康状况就 ...

  8. MongoDB 学习笔记之 批处理

    批处理: MongoDB批处理方式有2种, 有序插入(有序仍是顺序处理的.发生错误就停止.) 无序插入(无序列表会将操作按类型分组,来提高性能,因此,应确保应用不依赖操作执行顺序.发生错误继续处理剩余 ...

  9. Android通过外部浏览器调用微信H5支付,Android+PHP详解

    看了好多关于讲解微信H5支付开发的文章,大多数都是通过微信内部浏览器来调用支付接口(其实就是公众号支付),可能是因为H5支付接口刚开放不久吧. 微信官方体验链接:http://wxpay.wxutil ...

  10. springmvc中将servlet api对象作为处理方法的入参使用

    在springmvc中,控制器不依赖任何servlet api对象,也可以将servlet api对象作为处理方法的入参使用,非常方便,比如需要使用HttpSession对象,那么就可以直接将Http ...