C/C++操作加密与不加密的zip文件

    为了后续的方便操作zip文件, 将所有的操作封装成了一个动态库了。

/**
 * \description 从压缩包文件中解压出指定的文件到指定的目录.
 * \author sunsz
 * \date   2023/09/09
 *
 */
LIBZIP_API int  UnpackFile(const char* password, char zipfilename[], char filename_to_extract[], char filepath_to_extract[]);

/**
 * \description .从压缩包文件中读出指定的文件到buffer中.
 * 
 * \author sunsz
 * \date   2023/09/09 
 * 
 */
LIBZIP_API int  UnpackBuffer(const char* password, char zipfilename[],char filenameinzip[], char *buffer,unsigned int *buffer_size);

/**
 * \description 获取指定文件的大小.
 * 
 * \author sunsz
 * \date   2023/09/09 
 * 
 */
LIBZIP_API int  UnpackBufferSize(const char* password, char zipfilename[], char filenameinzip[]);


/**
 * \description 从压缩包中获取文件列表.
 * 
 * \author sunsz
 * \date   2023/09/09 
 * 
 */
LIBZIP_API int UnpackFileList(const char* password, char zipfilename[], char filename_list[256][256]);


/**
 * \description 将指定的文件写入到压缩包文件中.
 *  opt_overwrite = 0 重新新建文件 opt_overwrite = 2 以追加的方式添加到压缩包不存在则创建  
 * \author sunsz
 * \date   2023/09/09 
 * 
 */
LIBZIP_API int PackFile(const char* password,char m_zipname[],char filein[],int opt_overwrite);

/**
 * \description 将内容以指定的文件名称存入到压缩包文件中.
 *  opt_overwrite = 0 重新新建文件 opt_overwrite = 2 以追加的方式添加到压缩包不存在则创建
 * \author sunsz
 * \date   2023/09/09
 *
 */
LIBZIP_API int PackBuffer(const char* password, char m_zipname[], char filenameinzip[], char* buffer, unsigned int buffer_size, int opt_overwrite);

 示例代码:


//依赖头文件
#include 
#include 
#include 
#include 
#include 
#include 

//定义函数指针

//typedef  int(WINAPI*S_dll_callback_demo1)(unsigned char*,unsigned char*);
typedef  int(WINAPI*ZipRead)(const char*, char*, char*, char*);
typedef  int(WINAPI*ZipReadFileList)(const char*, char*, char**);
typedef  int(WINAPI*ZipWrite)(const char*, char*, char*,int);
typedef  int(WINAPI* ZipWriteBuffter)(const char* , char *, char*, char* , unsigned int , int );
typedef  int(WINAPI* ZipReadBuffer)(const char* , char *, char *, char *, unsigned int* );
typedef  int(WINAPI* ZipReadBufferSize)(const char* , char *, char *);
//定义API函数变量
static ZipWrite	zipWriteFile;
static ZipRead	zipReadFile;
static ZipReadFileList	readList;
static ZipWriteBuffter	zipWriteBuffter;
static ZipReadBuffer	zipReadBuffter;
static ZipReadBufferSize	zipReadBuffterSize;
int main(){
	int i = 0;
	int j = 0;
	int malloclen = 0;
	unsigned int len = 0;
	char* dataout = NULL;
	char* passwd = "123456";
	int filenum = 0;
	char filelist[256][256] = { 0x00 };
	char data[] = "hello zip!";
	char path[] = ".//libzip.dll";
	HMODULE hDll = LoadLibrary(path);
	if (hDll != NULL) {
	    zipWriteFile = (ZipWrite)GetProcAddress(hDll, "PackFile");
		zipReadFile = (ZipRead)GetProcAddress(hDll, "UnpackFile");
		readList = (ZipReadFileList)GetProcAddress(hDll, "UnpackFileList");
		zipWriteBuffter = (ZipWriteBuffter)GetProcAddress(hDll, "PackBuffer");
		zipReadBuffter = (ZipReadBuffer)GetProcAddress(hDll, "UnpackBuffer");
		zipReadBuffterSize = (ZipReadBufferSize)GetProcAddress(hDll, "UnpackBufferSize");
		/*if (zipWriteFile) {
			zipWriteFile(passwd, "2.zip", "D:\\D-Pro\\Test\\test_zip\\test_zip\\test.S19",0);
		}
		else {
			printf("\nZipWriteFile\n");
		}
		if (zipReadFile) {
			zipReadFile(passwd, "2.zip", "test.S19",".//test//11111.s19");
		}
		else {
			printf("\nZipReadFile\n");
		}*/
		
		if (zipWriteBuffter) {
			zipWriteBuffter(passwd, "3.zip", "11112.txt", data, sizeof(data), 2);
		}
		else {
			printf("\nzipWriteBuffter \n");
		}
		
		if (zipReadBuffter) {
			if (zipReadBuffterSize) {
				malloclen = zipReadBuffterSize(passwd, "3.zip", "11112.txt");
				if (malloclen) {
					dataout = (char*)malloc(malloclen + 8);
					zipReadBuffter(passwd, "3.zip", "11112.txt", dataout, &len);
					printf("%s\n", dataout);
					if (dataout != NULL) {
						free(dataout);
						dataout = NULL;
					}
				}
			}
		}
		else {
			printf("\nzipWriteBuffter \n");
		}
	}
	else
	{
		printf("\n加载失败!\n");
	}
	printf("\n请按任意键结束!\n");
	_getch();
	return 0;
}

示例测试: 

C/C++操作加密与不加密的zip文件_第1张图片

你可能感兴趣的:(c,plus,c,plus,plus,windows,c语言,c++)