9 Star 31 Fork 11

kanakdillon / zipclass

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
cUnpackFile.cpp 3.20 KB
一键复制 编辑 原始数据 按行查看 历史
kanakdillon 提交于 2013-08-06 17:49 . v.1.0.0
#include "cUnpackFile.h"
static inline bool IsFileExist(const char* pPath)
{
return GetFileAttributesA(pPath) != INVALID_FILE_ATTRIBUTES;
}
void cUnpackFile::CreateDirFromZip(const char * dirName, const char * zipFileName)
{
bool slashFlag = true;
unzFile unZipDir = unzOpen(zipFileName);
if (unZipDir == NULL)
{
printf("无法解压zip文件!");
return;
}
if (!IsFileExist(dirName))
_mkdir(dirName);
int nResult = unzGoToFirstFile(unZipDir);
BYTE * buf;
while(nResult == UNZ_OK)
{
char szCurrentFile[MAX_PATH];
unz_file_info unZipFileInfo;
unzGetCurrentFileInfo(unZipDir, &unZipFileInfo, szCurrentFile, sizeof(szCurrentFile), NULL, 0, NULL, 0);
std::string filePath = std::string(szCurrentFile);
std::string fileName;
char fileRoot[MAX_PATH];
unsigned last_slash_pos = filePath.find_last_of('\\');
if (last_slash_pos == std::string::npos){
last_slash_pos = filePath.find_last_of('/');
slashFlag = false;
}
if (last_slash_pos != std::string::npos )
{
strcpy_s(fileRoot,dirName);
if (slashFlag)
strcat_s(fileRoot,"\\");
else
strcat_s(fileRoot,"/");
std::string filetemp = filePath.substr(0,last_slash_pos);
strcat_s(fileRoot,filetemp.c_str());
if (!IsFileExist(fileRoot))
_mkdir(fileRoot);
if (last_slash_pos == filePath.length()-1)
{
nResult = unzGoToNextFile(unZipDir);
continue;//文件夹项
}
}
int size = unZipFileInfo.uncompressed_size;
buf = new BYTE[size];
if (!buf)
{
printf("No enough Memory!\n");
return;
}
memset(buf, 0, size*sizeof(char));
int nReadBytes;
if (UNZ_OK == unzOpenCurrentFilePassword(unZipDir,NULL))
{
nReadBytes = unzReadCurrentFile(unZipDir, buf , size);
unzCloseCurrentFile(unZipDir);
}
FILE * pFile;
char filePos[MAX_PATH];
strcpy_s(filePos,dirName);
if (slashFlag)
strcat_s(filePos,"\\");
else
strcat_s(filePos,"/");
strcat_s(filePos,filePath.c_str());
fopen_s(&pFile,filePos, "wb");
if (pFile)
fwrite(buf,nReadBytes,1,pFile);
else
printf("无法打开输出文件 %s \n",filePos);
fclose(pFile);
delete buf;
nResult = unzGoToNextFile(unZipDir);
}
unzClose(unZipDir);
}
void cUnpackFile::CreateFileFromZip(const char * fName, const char * zipFileName)
{
bool slashFlag = true;
unzFile unZipDir = unzOpen(zipFileName);
if (unZipDir == NULL)
{
printf("无法解压zip文件!");
return;
}
int nResult = unzGoToFirstFile(unZipDir);
BYTE * buf;
if(nResult == UNZ_OK)
{
char szCurrentFile[MAX_PATH];
unz_file_info unZipFileInfo;
unzGetCurrentFileInfo(unZipDir, &unZipFileInfo, szCurrentFile, sizeof(szCurrentFile), NULL, 0, NULL, 0);
int size = unZipFileInfo.uncompressed_size;
buf = new BYTE[size];
if (!buf)
{
printf("No enough Memory!\n");
return;
}
memset(buf, 0, size*sizeof(char));
int nReadBytes;
if (UNZ_OK == unzOpenCurrentFilePassword(unZipDir,NULL))
{
nReadBytes = unzReadCurrentFile(unZipDir, buf , size);
unzCloseCurrentFile(unZipDir);
}
FILE * pFile;
fopen_s(&pFile,fName, "wb");
if (pFile)
fwrite(buf,nReadBytes,1,pFile);
else
printf("无法打开输出文件 %s \n",fName);
fclose(pFile);
delete buf;
}
unzClose(unZipDir);
}
cUnpackFile::cUnpackFile()
{
}
cUnpackFile::~cUnpackFile()
{
}
C
1
https://gitee.com/kanakdillon/zipclass.git
git@gitee.com:kanakdillon/zipclass.git
kanakdillon
zipclass
zipclass
master

搜索帮助