写个zip工具玩玩。
// ZlibTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "zip.h"
#include "zlib.h"
#include "unzip.h"
#include "windows.h"
#include "iostream"
using namespace std;
#define MAX_SIZE 1024
int decompress()
{
string zip = "F:/project/cpp/ZlibTest/ZlibTest/test.zip";
int folderpos = zip.find_last_of("/");
int dotPos = zip.find_last_of(".");
string rootPath = zip.substr(0,folderpos+1);
string zipFileName = zip.substr(folderpos+1,dotPos-folderpos-1);
unzFile zipFile = unzOpen(zip.c_str());
if (!zipFile)
{
cout<<"没有找到"<<zip.c_str()<<"文件"<<endl;
return 0;
}
unz_global_info globalInfo;
if(unzGetGlobalInfo(zipFile,&globalInfo)!=UNZ_OK){
cout<<"没有获取到zip文件的信息"<<endl;
unzClose(zipFile);
return 0;
}
rootPath.append(zipFileName).append("/");
::CreateDirectoryA(rootPath.c_str(),NULL);
for(int i = 0;i<globalInfo.number_entry;i++){
unz_file_info fileInfo;
char fileName[MAX_SIZE];
if(unzGetCurrentFileInfo(zipFile,&fileInfo,fileName,MAX_SIZE,NULL,0,NULL,0)!=UNZ_OK){
cout<<"获取文件"<<i<<"失败"<<endl;
unzClose(zipFile);
return 0;
}
string temp = rootPath;
string fullPath = temp.append(fileName);
int filenameLength = strlen(fileName);
if(fileName[filenameLength-1] == '/'){
::CreateDirectoryA(fullPath.c_str(),NULL);
}else{
cout<<"文件"<<i<<": "<<fullPath.c_str()<<endl;
if (unzOpenCurrentFile(zipFile)!=UNZ_OK)
{
cout<<"打开文件失败"<<endl;
unzClose(zipFile);
return 0;
}
FILE* out;
fopen_s(&out,fullPath.c_str(),"wb");
if (!out)
{
cout<<"打开文件失败"<<endl;
unzCloseCurrentFile(zipFile);
unzClose(zipFile);
return 0;
}
char buff[MAX_SIZE]={0};
int size =0;
while ((size = unzReadCurrentFile(zipFile,buff,MAX_SIZE))!=0)
{
fwrite(buff,size,1,out);
}
fclose(out);
unzCloseCurrentFile(zipFile);
}
if((i+1)<globalInfo.number_entry){
if (unzGoToNextFile(zipFile)!= UNZ_OK)
{
cout<<"转到下一个文件失败"<<endl;
unzClose(zipFile);
return 0;
}
}
}
unzClose(zipFile);
cout<<"end"<<endl;
return 0;
}
void compress()
{
string targetpath = "F:/project/cpp/ZlibTest/ZlibTest/compress.zip";
string files[3]={"F:/project/cpp/ZlibTest/ZlibTest/compress/txt2.txt",
"F:/project/cpp/ZlibTest/ZlibTest/compress/txt3.txt",
"F:/project/cpp/ZlibTest/ZlibTest/compress/update/txt1.txt"};
zipFile zip = zipOpen(targetpath.c_str(),APPEND_STATUS_CREATE);
for (int i = 0;i<3;i++)
{
string curFile = files[i];
string splitStr = "compress";
int splitPos = curFile.find(splitStr);
int nextPos = splitPos+splitStr.length()+1;
string filename = curFile.substr(nextPos);
zip_fileinfo zipFileInfo;
int result = zipOpenNewFileInZip(zip,filename.c_str(),&zipFileInfo,NULL,0,NULL,0,NULL,0,0);
if (result!=Z_OK)
{
cout<<"打开压缩文件:"<<curFile.c_str()<<"失败"<<endl;
return;
}
char buff[MAX_SIZE]={0};
FILE* file;
fopen_s(&file,curFile.c_str(),"r");
while (!feof(file))
{
int numBytes = fread_s(buff,MAX_SIZE,1,MAX_SIZE,file);
zipWriteInFileInZip(zip,buff,numBytes);
}
cout<<"文件"<<curFile.c_str()<<"压缩成功"<<endl;
fclose(file);
zipCloseFileInZip(zip);
}
zipClose(zip,NULL);
}
int _tmain(int argc, _TCHAR* argv[])
{
compress();
decompress();
system("pause");
return 0;
}