zip

写个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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值