使用zip32.dll,ZIP32J.DLL,UNZIP32.DLL进行压缩和解压zip文件
来源:互联网 发布:手机淘宝进入二手市场 编辑:程序博客网 时间:2024/06/05 08:23
首先,需要准备好题目中必须得的3个dll文件。可以在网络中搜索并下载。
然后,我习惯把他们放在工程的debug目录下。
以下是代码:
public static class ZipManager { //锁定对象 private static readonly object m_oLockObject = new object(); //ZIP解压缩 [DllImport("Unzip32")] private extern static int UnZip(int hwnd, string szCmdLine, StringBuilder szOutput, int dwSize); //ZIP压缩 [DllImport("ZIP32J.DLL", CharSet = CharSet.Ansi)] private extern static int Zip(IntPtr hwnd, string szCmdLine, StringBuilder szOutput, int dwSize); /// <summary> /// 解压指定的ZIP文件 /// </summary> /// <param name="targetFile">解压文件</param> /// <param name="destFolder">目标文件夹</param> /// <returns>执行结果</returns> public static long ExpandZip(string targetFile, string destFolder) { lock (m_oLockObject) { string command = "-xsoqq--i \"" + targetFile + "\" \"" + destFolder + "\\\""; return UnZip(0, command, new StringBuilder(), 0); } } /// <summary> /// 解压指定的ZIP文件[指定密码] /// </summary> /// <param name="targetFile">解压文件</param> /// <param name="destFolder">目标文件夹</param> /// <param name="password">密码(半角英文数字)</param> /// <returns>执行结果</returns> public static long ExpandZipWithPassword(string targetFile, string destFolder, string password) { lock (m_oLockObject) { StringBuilder sbOutput = new StringBuilder(1024); string sCmdLine = string.Format("-x -o -q -P {0} \"{1}\" \"{2}\\\" *.*", password, targetFile, destFolder); return UnZip(0, sCmdLine, sbOutput, sbOutput.Capacity); } } /// <summary> /// ZIP压缩[指定文件夹] /// </summary> /// <param name="folderPath">压缩对象文件夹(指定完全路径)</param> /// <param name="zipFilePath">ZIP文件名</param> /// <returns>返回值:0正常结束</returns> public static int ArchiveZip(string folderPath, string zipFilePath) { lock (m_oLockObject) { StringBuilder sbOutput = new StringBuilder(1024); string sCmdLine = string.Format("-r -q \"{0}\" \"{1}\"\\ \"{2}\\*\"", zipFilePath, Directory.GetParent(folderPath).FullName, folderPath); int iZipRtn = Zip((IntPtr)0, sCmdLine, sbOutput, sbOutput.Capacity); return iZipRtn; } } /// <summary> /// ZIP压缩[指定文件夹,指定密码] /// </summary> /// <param name="folderPath">压缩对象文件夹(指定完全路径)</param> /// <param name="zipFilePath">ZIP文件名</param> /// <param name="password">密码(半角英文数字)</param> /// <returns>返回值:0正常结束</returns> public static int ArchiveZipWithPassword(string folderPath, string zipFilePath, string password) { lock (m_oLockObject) { StringBuilder sbOutput = new StringBuilder(1024); string sCmdLine = string.Format("-r -q -P {0} \"{1}\" \"{2}\"\\ \"{3}\\*\"", password, zipFilePath, Directory.GetParent(folderPath).FullName, folderPath); int iZipRtn = Zip((IntPtr)0, sCmdLine, sbOutput, sbOutput.Capacity); return iZipRtn; } } /// <summary> /// ZIP压缩[指定ファイル] /// </summary> /// <param name="aryFilePath">压缩对象文件数组(指定完全路径)</param> /// <param name="zipFilePath">ZIP文件名</param> /// <returns>返回值:0正常结束</returns> public static int ArchiveZip(string[] aryFilePath, string zipFilePath) { lock (m_oLockObject) { StringBuilder sbCmdLine = new StringBuilder(1024); StringBuilder sbOutput = new StringBuilder(1024); sbCmdLine.AppendFormat("-q -j \"{0}\"", zipFilePath); foreach (string sFilePath in aryFilePath) { sbCmdLine.AppendFormat(" \"{0}\"", sFilePath); } string sCmdLine = sbCmdLine.ToString(); int iZipRtn = Zip((IntPtr)0, sCmdLine, sbOutput, sbOutput.Capacity); return iZipRtn; } } /// <summary> /// ZIP压缩[指定ファイル,指定密码] /// </summary> /// <param name="aryFilePath">压缩对象文件数组(指定完全路径)</param> /// <param name="zipFilePath">ZIP文件名</param> /// <param name="password">密码</param> /// <returns>返回值:0正常结束</returns> public static int ArchiveZipWithPassword(string[] aryFilePath, string zipFilePath, string password) { lock (m_oLockObject) { StringBuilder sbCmdLine = new StringBuilder(1024); StringBuilder sbOutput = new StringBuilder(1024); sbCmdLine.AppendFormat("-q -j -P {0} \"{1}\"", password, zipFilePath); foreach (string sFilePath in aryFilePath) { sbCmdLine.AppendFormat(" \"{0}\"", sFilePath); } string sCmdLine = sbCmdLine.ToString(); int iZipRtn = Zip((IntPtr)0, sCmdLine, sbOutput, sbOutput.Capacity); return iZipRtn; } } }
- 使用zip32.dll,ZIP32J.DLL,UNZIP32.DLL进行压缩和解压zip文件
- Ionic.Zip.dll文件压缩和解压
- Ionic.Zip.dll文件压缩和解压
- 使用Ionic.Zip.dll压缩和解压文件代码笔记
- 通过ICSharpCode.SharpZipLib.dll压缩和解压ZIP文件
- 不使用zip32.dll打包的方法
- 使用ZLIB生成DLL文件,并进行有中文的ZIP文件压缩与解压操作!支持目录压与解!
- tar32.dll解压,压缩
- Delphi 7zip 示例调用7z.dll 压缩及解压.7z文件
- pb 调用zip压缩dll
- C# ZIP文件的压缩和解压缩(SharpZipLib.dll)
- 使用ICSharpCode.SharpZipLib.dll解压指定文件
- loadrunner中使用zlib.dll压缩或解压
- .dll 文件编写和使用
- C#中用SharpZipLib.dll实现压缩解压
- 使用PHP对文件进行压缩解压(zip)
- 文件压缩和解压zip
- 压缩和解压zip文件
- Struts+Spring+Hibernate快速入门
- 产业--安防,物联网
- ofstream ifstream 文件操作
- Android之Activity生命周期讲解
- 构建基本的嵌入式Linux根文件系统
- 使用zip32.dll,ZIP32J.DLL,UNZIP32.DLL进行压缩和解压zip文件
- 在Eclipse中使用Hibernate插件
- ubuntu11 下安装86五笔输入法
- JPA 简单应用 学习
- linux 内核编译选项详解
- as3 zlib jpg
- Hibernate 关联映射 多对多
- 分析影响手机游戏未来走向的5大因素
- VC6中使用COM库的三种方法