C/C++Zlib 解压文件
还是记下来吧,用了这么N多次了,博客方便点
.h头文件
#include <string>
using namespace std;class ZlibClass
{
public:static bool UnCompress(const string &pSzSrcFile, const string & pSzPSW, const string &pSzOutDir);
};
.cpp文件
#include "ZlibClass.h"#include <direct.h> //_mkdir函数的头文件
#include <io.h> //_access函数的头文件
#include <windows.h>#include "zlib\unzip.h"
#include "zlib\zlib.h"
#include "zlib\zip.h"void FormatDirectorys(LPSTR lpszDirectorys)
{if (lpszDirectorys != NULL){int i = 0;while (lpszDirectorys[i] != ('\0')){if (lpszDirectorys[i] == ('/')){lpszDirectorys[i] = ('\\');}i++;}}
}void CreateDir(const char *dir)
{int m = 0, n;string str1, str2;str1 = dir;str2 = str1.substr(0, 2);str1 = str1.substr(3, str1.size());while (m >= 0){m = str1.find('\\');str2 += '\\' + str1.substr(0, m);n = _access(str2.c_str(), 0); //判断该目录是否存在if (n == -1){_mkdir(str2.c_str()); //创建目录}str1 = str1.substr(m + 1, str1.size());}
}bool ZlibClass::UnCompress(const string &pSzSrcFile, const string & pSzPSW, const string &pSzOutDir)
{char szSrcFile[MAX_PATH] = { 0 };char szOutDir[MAX_PATH] = { 0 };strcpy(szSrcFile, pSzSrcFile.c_str());strcpy(szOutDir, pSzOutDir.c_str());char szDestFile[MAX_PATH] = ("");char szFile[50] = "0";if (pSzSrcFile.empty() && pSzOutDir.empty())return false;FormatDirectorys(szSrcFile); // 格式化字符串FormatDirectorys(szOutDir); // 格式化字符串unzFile uf = NULL;unz_global_info gi;unz_file_info FileInfo;uf = unzOpen(szSrcFile);int result = unzGetGlobalInfo(uf, &gi);
#define BUFF_SZIE 16384char szBuf[BUFF_SZIE] = "0";for (int i = 0; i < gi.number_entry; ++i){HANDLE h = NULL;if (result != UNZ_OK)return false;if (unzGetCurrentFileInfo(uf, &FileInfo, szFile, 50, NULL, 0, NULL, 0) != UNZ_OK)return false;if (!(FileInfo.external_fa & FILE_ATTRIBUTE_DIRECTORY)) //文件,否则为目录{// 创建文件FormatDirectorys(szFile);sprintf(szDestFile, ("%s\\%s"), szOutDir, szFile);bool bCreateFile = true;if (!pSzPSW.empty()){result = unzOpenCurrentFilePassword(uf, pSzPSW.c_str()); /* 有密码 */}else{result = unzOpenCurrentFile(uf); /* 无密码 */// 感觉有的时候,即使压缩包有密码,但是解压时,不输入密码,也能解压成功?Wht?是因为密码太简单的缘故么?还是因为zlib破解了?}while (true){int size = unzReadCurrentFile(uf, szBuf, BUFF_SZIE); //读取内容if (size >= 0){if (bCreateFile)h = CreateFileA(szDestFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);bCreateFile = false;}if (size < 0){if (h){CloseHandle(h);}unzCloseCurrentFile(uf); //关闭当前文件return false;}if (size == 0){break;}DWORD writ;BOOL bRes = WriteFile(h, szBuf, size, &writ, NULL);if (!bRes){break;}}CloseHandle(h);unzCloseCurrentFile(uf); //关闭当前文件}else{//创建文件夹char szDestDir[MAX_PATH] = ("");FormatDirectorys(szFile);sprintf(szDestDir, ("%s\\%s"), szOutDir, szFile);CreateDir(szDestDir);}unzGoToNextFile(uf);}return true;
}
把zlib的码放进来就okay
C/C++Zlib 解压文件相关推荐
- python批量解压文件_python 批量解压压缩文件的实例代码
下面给大家介绍python 批量解压压缩文件的实例代码,代码如下所述: #/usr/bin/python#coding=utf-8import os,sys import zipfile open_p ...
- Zlib解压/压缩实现
Zlib解压/压缩实现 针对目前2503平台,请参考以下方式实现zlib解压/压缩文件: 1:将plutommi\Customer\ResGenerator\zlib整个文件夹copy到贵司的modu ...
- 使用Ionic.Zip压缩、分卷压缩、解压文件
分卷压缩使用方法: int iMinLength = 100;//最小压缩包单位(100M)ZipHelper.Compress("../../../xxx.xx", " ...
- python解压文件中哪个是安装包_python解压文件格式的基本方法
应对多种压缩包格式的python库:patool.如果平时只用基本的解压.打包等操作,也不想详细了解各种压缩格式对应的python库,patool应该是个不错的选择. patool库支持的格式包括: ...
- 通过C#代码 压缩/解压文件
通过引用一DLL(ICSharpCode.dll)可以实现所述功能... 一.压缩文件 using System; using ICSharpCode.SharpZipLib; using ICSha ...
- 压缩,解压文件|*.tar.gz/.tar.bz2 文件
参考 Tar选项: c – 创建压缩文件 x – 解压文件 v – 显示进度 f – 文件名 t – 查看压缩文件内容 j – 通过bzip2归档 z –通过gzip归档 r – 在压缩文件中追加文件 ...
- 测试掌握的Linux解压,轻松掌握Linux压缩/解压文件的方法
对于在Linux下解压大型的*.zip文件,相信大家一般都会通过使用winrar直接在smb中来进行解压的操作,虽然说最终可能能够解压但有时候会存在解压时间长或者网络原因出错等故障的情况出现.那么有没 ...
- 解压文件出错解决方法(invalid compressed data--format violated)
解压文件出错解决方法(invalid compressed data--format violated) 参考文章: (1)解压文件出错解决方法(invalid compressed data--fo ...
- tar 打包文件与解压文件
tar 命令打包文件与解压文件 打包文件 1.把/root目录下的test1整个打包为test1.tar文件,如下所示 [root@localhost ~]# pwd /root [root@loca ...
最新文章
- swift_045(Swift @IBDesignable和@IBInspectable使用)
- Linux Kernel TCP/IP Stack — L1 Layer — NIC Controller — SKB
- iphone定时关机_成都苹果维修点教你iPhone手机死机、关不了机怎么处理?
- python datetime计算时间差_用datetime计算时间差
- Hermes与开源的Solr、ElasticSearch的不同
- [react] 自定义组件时render是可选的吗?为什么?
- Vue之计算属性Computed
- 『转』数据库的委托之类型分类处理
- redis 数据类型详解 以及 redis适用场景场合
- JavaScript原型OOP——你上车了吗?
- 关于孟德斯鸠的出卖官职
- 新浪xweibo代码架构分析
- 优秀项目经理的五大核心能力
- word打开html是空白,网页复制到Word之后或者出现空白或者格式变乱该怎么办
- bat putty shell 连携
- ST_Intersects
- deepin 网速(WIFI)太慢的一种解决方法
- 全终端办公电子邮件集成方案
- 【博学谷学习记录】超强总结,用心分享|大数据之Presto
- 运营中心:网站分析、网络广告注释