Minizip压缩加密示例代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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