跨平台的zip文件压缩处理,支持压缩解压文件夹
根据minizip改写的模块,需要zlib支持
输出的接口:
1 #define RG_ZIP_FILE_REPLACE 0 2 #define RG_ZIP_FILE_APPEND 1 3 4 //压缩文件夹目录,递归压缩 5 //szDir是需要压缩的目录,dstLevel是压缩的目录在压缩包里面的层次标识 6 //可直接指定"" 7 //szZipFile压缩包的文件名 8 //replaceFlag指定替换或者是追加进压缩包 9 int DoZipDir(const char* szDir, const char* dstLevel, const char* szZipFile, int replaceFlag); 10 11 //压缩单个文件,szFile是文件名,其它参数解析同上 12 int DoZipFile(const char* szFile, const char* dstLevel, const char* szZipFile, int replaceFlag); 13 14 //解压缩文件 15 //szZipFile是需要解压的压缩包文件名 16 //指定需要解压到的目录,直接指定为"",解压至当前目录 17 int DoUnzip(const char* szZipFile, const char* szTargetDir); 18 19 //从压缩包里面解压单个文件出来 20 //srcFileToExtract对应压缩文件时的dstLevel 21 //其它参数解析同上 22 int DoUnzipFile(const char* szZipFile, const char* srcFileToExtract, const char* szTargetDir);
本文仅仅提供封装的api,zlib库和info-zip请自行下载
其它涉及的代码可以下载minizip或者info-zip,推荐使用minizip,本代码是在minizip的基础上修改而来,支持跨平台
1 /* 2 minizip.c 3 Version 1.1, February 14h, 2010 4 sample part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html ) 5 6 Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html ) 7 8 Modifications of Unzip for Zip64 9 Copyright (C) 2007-2008 Even Rouault 10 11 Modifications for Zip64 support on both zip and unzip 12 Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com ) 13 */ 14 15 #ifdef _DEBUG 16 #pragma comment(lib, "../../lib/win32/libzd.lib") 17 #else 18 #pragma comment(lib, "../../lib/win32/libz.lib") 19 #endif 20 21 #ifndef _WIN32 22 #ifndef __USE_FILE_OFFSET64 23 #define __USE_FILE_OFFSET64 24 #endif 25 #ifndef __USE_LARGEFILE64 26 #define __USE_LARGEFILE64 27 #endif 28 #ifndef _LARGEFILE64_SOURCE 29 #define _LARGEFILE64_SOURCE 30 #endif 31 #ifndef _FILE_OFFSET_BIT 32 #define _FILE_OFFSET_BIT 64 33 #endif 34 #endif 35 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <string.h> 39 #include <time.h> 40 #include <errno.h> 41 #include <fcntl.h> 42 43 #ifdef _LINUX 44 # include <utime.h> 45 # include <sys/types.h> 46 # include <sys/stat.h> 47 # include <unistd.h> 48 #include <dirent.h> 49 #else 50 # include <direct.h> 51 # include <io.h> 52 #endif 53 54 #include "zip.h" 55 #include "stdstring.h" 56 #include <vector> 57 #include "comfun.h" 58 59 using namespace std; 60 61 // #ifdef _WIN32 62 // #define USEWIN32IOAPI 63 // #include "iowin32.h" 64 // #endif 65 66 67 68 #define WRITEBUFFERSIZE (16384) 69 #define MAXFILENAME (256) 70 71 #ifdef _WIN32 72 static uLong filetime(const char *f, tm_zip *tmzip, uLong *dt) 73 /*char *f; name of file to get info on */ 74 /* tm_zip *tmzip; return value: access, modific. and creation times */ 75 /*uLong *dt; dostime */ 76 { 77 int ret = 0; 78 { 79 FILETIME ftLocal; 80 HANDLE hFind; 81 WIN32_FIND_DATAA ff32; 82 83 hFind = FindFirstFileA(f,&ff32); 84 if (hFind != INVALID_HANDLE_VALUE) 85 { 86 FileTimeToLocalFileTime(&(ff32.ftLastWriteTime),&ftLocal); 87 FileTimeToDosDateTime(&ftLocal,((LPWORD)dt)+1,((LPWORD)dt)+0); 88 FindClose(hFind); 89 ret = 1; 90 } 91 } 92 return ret; 93 } 94 #else 95 #ifdef _LINUX 96 static uLong filetime(const char *f, tm_zip *tmzip, uLong *dt) 97 /*char *f; name of file to get info on */ 98 /*tm_zip *tmzip; return value: access, modific. and creation times */ 99 /*uLong *dt; dostime */ 100 { 101 int ret=0; 102 struct stat s; /* results of stat() */ 103 struct tm* filedate; 104 time_t tm_t=0; 105 106 if (strcmp(f,"-")!=0) 107 { 108 char name[MAXFILENAME+1]; 109 int len = strlen(f); 110 if (len > MAXFILENAME) 111 len = MAXFILENAME; 112 113 strncpy(name, f,MAXFILENAME-1); 114 /* strncpy doesnt append the trailing NULL, of the string is too long. */ 115 name[ MAXFILENAME ] = '