Skip to content

Instantly share code, notes, and snippets.

@xkyii
Created January 27, 2013 15:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xkyii/4648779 to your computer and use it in GitHub Desktop.
Save xkyii/4648779 to your computer and use it in GitHub Desktop.
Minizip压缩加密示例代码
void crypt_test()
{
zipFile zf = zipOpen("myarch.zip",APPEND_STATUS_ADDINZIP);
int ret = zipOpenNewFileInZip(zf,
"myfile.txt",
&zfi,
NULL, 0,
NULL, 0,
"my comment for this interior file",
Z_DEFLATED,
Z_NO_COMPRESSION
);
zipCloseFileInZip(zf);
zipClose(zf, "my comment for exterior file");
return 0;
}
void uncrypt_test()
{
//采用MINIZIP进行文件解压缩
unzFile uf=NULL;
unzFile data[1200];
unz_global_info64 gi;
unz_file_info64 FileInfo;
//打开zip文件
uf = unzOpen64("D:\\myfile.zip");
int result=unzGetGlobalInfo64(uf, &gi);
if (result != UNZ_OK)
throw "文件错误";
//循环解压缩文件
for(int i=0;i<gi.number_entry;++i)
{
if (unzGetCurrentFileInfo64(uf, &FileInfo, 0, 0,NULL,0,NULL,0)!= UNZ_OK)
throw "文件错误";
if(!(FileInfo.external_fa & FILE_ATTRIBUTE_DIRECTORY)) //文件,否则为目录
//打开文件
//result=unzOpenCurrentFile(uf);/* 无密码 */
result=unzOpenCurrentFilePassword(uf,"123"); /* 有密码 */
//读取内容
int size= unzReadCurrentFile(uf,data,sizeof(data));
//关闭当前文件
unzCloseCurrentFile(uf);
//出错
if(i < gi.number_entry - 1 && unzGoToNextFile(uf) != UNZ_OK)
throw "error";
}
//关闭流
unzClose(uf);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment