跨平台的zip文件压缩处理,支持压缩解压文件夹
根据minizip改写的模块,需要zlib支持
输出的接口:
- #define RG_ZIP_FILE_REPLACE 0
- #define RG_ZIP_FILE_APPEND 1
- //压缩文件夹目录,递归压缩
- //szDir是需要压缩的目录,dstLevel是压缩的目录在压缩包里面的层次标识
- //可直接指定""
- //szZipFile压缩包的文件名
- //replaceFlag指定替换或者是追加进压缩包
- int DoZipDir(const char* szDir, const char* dstLevel, const char* szZipFile, int replaceFlag);
- //压缩单个文件,szFile是文件名,其它参数解析同上
- int DoZipFile(const char* szFile, const char* dstLevel, const char* szZipFile, int replaceFlag);
- //解压缩文件
- //szZipFile是需要解压的压缩包文件名
- //指定需要解压到的目录,直接指定为"",解压至当前目录
- int DoUnzip(const char* szZipFile, const char* szTargetDir);
- //从压缩包里面解压单个文件出来
- //srcFileToExtract对应压缩文件时的dstLevel
- //其它参数解析同上
- int DoUnzipFile(const char* szZipFile, const char* srcFileToExtract, const char* szTargetDir);
本文仅仅提供封装的api,zlib库和info-zip请自行下载
其它涉及的代码可以下载minizip或者info-zip,推荐使用minizip,本代码是在minizip的基础上修改而来,支持跨平台
- /*
- minizip.c
- Version 1.1, February 14h, 2010
- sample part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
- Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
- Modifications of Unzip for Zip64
- Copyright (C) 2007-2008 Even Rouault
- Modifications for Zip64 support on both zip and unzip
- Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
- */
- #ifdef _DEBUG
- #pragma comment(lib, "../../lib/win32/libzd.lib")
- #else
- #pragma comment(lib, "../../lib/win32/libz.lib")
- #endif
- #ifndef _WIN32
- #ifndef __USE_FILE_OFFSET64
- #define __USE_FILE_OFFSET64
- #endif
- #ifndef __USE_LARGEFILE64
- #define __USE_LARGEFILE64
- #endif
- #ifndef _LARGEFILE64_SOURCE
- #define _LARGEFILE64_SOURCE
- #endif
- #ifndef _FILE_OFFSET_BIT
- #define _FILE_OFFSET_BIT 64
- #endif
- #endif
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include <errno.h>
- #include <fcntl.h>
- #ifdef _LINUX
- # include <utime.h>
- # include <sys/types.h>
- # include <sys/stat.h>
- # include <unistd.h>
- #include <dirent.h>
- #else
- # include <direct.h>
- # include <io.h>
- #endif
- #include "zip.h"
- #include "stdstring.h"
- #include <vector>
- #include "comfun.h"
- using namespace std;
- // #ifdef _WIN32
- // #define USEWIN32IOAPI
- // #include "iowin32.h"
- // #endif
- #define WRITEBUFFERSIZE (16384)
- #define MAXFILENAME (256)
- #ifdef _WIN32
- static uLong filetime(const char *f, tm_zip *tmzip, uLong *dt)
- /*char *f; name of file to get info on */
- /* tm_zip *tmzip; return value: access, modific. and creation times */
- /*uLong *dt; dostime */
- {
- int ret = ;
- {
- FILETIME ftLocal;
- HANDLE hFind;
- WIN32_FIND_DATAA ff32;
- hFind = FindFirstFileA(f,&ff32);
- if (hFind != INVALID_HANDLE_VALUE)
- {
- FileTimeToLocalFileTime(&(ff32.ftLastWriteTime),&ftLocal);
- FileTimeToDosDateTime(&ftLocal,((LPWORD)dt)+,((LPWORD)dt)+);
- FindClose(hFind);
- ret = ;
- }
- }
- return ret;
- }
- #else
- #ifdef _LINUX
- static uLong filetime(const char *f, tm_zip *tmzip, uLong *dt)
- /*char *f; name of file to get info on */
- /*tm_zip *tmzip; return value: access, modific. and creation times */
- /*uLong *dt; dostime */
- {
- int ret=;
- struct stat s; /* results of stat() */
- struct tm* filedate;
- time_t tm_t=;
- if (strcmp(f,"-")!=)
- {
- char name[MAXFILENAME+];
- int len = strlen(f);
- if (len > MAXFILENAME)
- len = MAXFILENAME;
- strncpy(name, f,MAXFILENAME-);
- /* strncpy doesnt append the trailing NULL, of the string is too long. */
- name[ MAXFILENAME ] = '\0';
- if (name[len - ] == '/')
- name[len - ] = '\0';
- /* not all systems allow stat'ing a file with / appended */
- if (stat(name,&s)==)
- {
- tm_t = s.st_mtime;
- ret = ;
- }
- }
- filedate = localtime(&tm_t);
- tmzip->tm_sec = filedate->tm_sec;
- tmzip->tm_min = filedate->tm_min;
- tmzip->tm_hour = filedate->tm_hour;
- tmzip->tm_mday = filedate->tm_mday;
- tmzip->tm_mon = filedate->tm_mon ;
- tmzip->tm_year = filedate->tm_year;
- return ret;
- }
- #else
- uLong filetime(char *f, tm_zip *tmzip, uLong *dt)
- {
- return ;
- }
- #endif
- #endif
- static int check_exist_file(const char* filename)
- {
- FILE* ftestexist;
- int ret = ;
- ftestexist = fopen64(filename,"rb");
- if (ftestexist==NULL)
- ret = ;
- else
- fclose(ftestexist);
- return ret;
- }
- static int isLargeFile(const char* filename)
- {
- int largeFile = ;
- ZPOS64_T pos = ;
- FILE* pFile = fopen64(filename, "rb");
- if(pFile != NULL)
- {
- int n = fseeko64(pFile, , SEEK_END);
- pos = ftello64(pFile);
- if(pos >= 0xffffffff)
- largeFile = ;
- fclose(pFile);
- }
- return largeFile;
- }
- static int DoZipFile(zipFile zf, const char* srcFile, const char* dstLevel)
- {
- if(zf == NULL || srcFile == NULL) return __LINE__;
- int err=;
- int size_buf=;
- void* buf=NULL;
- FILE * fin;
- int size_read;
- const char* filenameinzip = srcFile;
- const char *savefilenameinzip;
- zip_fileinfo zi;
- int zip64 = ;
- zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour =
- zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = ;
- zi.dosDate = ;
- zi.internal_fa = ;
- zi.external_fa = ;
- filetime(filenameinzip,&zi.tmz_date,&zi.dosDate);
- zip64 = isLargeFile(filenameinzip);
- /* The path name saved, should not include a leading slash. */
- /*if it did, windows/xp and dynazip couldn't read the zip file. */
- savefilenameinzip = dstLevel;
- while( savefilenameinzip[] == '\\' || savefilenameinzip[] == '/' )
- {
- savefilenameinzip++;
- }
- err = zipOpenNewFileInZip3_64(zf, savefilenameinzip, &zi,
- NULL, , NULL, , NULL /* comment*/,
- Z_DEFLATED,
- Z_DEFAULT_COMPRESSION,,
- -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
- NULL, , zip64);
- if (err != ZIP_OK)
- printf("error in opening %s in zipfile\n",filenameinzip);
- else
- {
- fin = fopen64(filenameinzip,"rb");
- if (fin==NULL)
- {
- err=ZIP_ERRNO;
- printf("error in opening %s for reading\n",filenameinzip);
- }
- }
- if (err == ZIP_OK)
- {
- size_buf = WRITEBUFFERSIZE;
- buf = (void*)malloc(size_buf);
- if (buf==NULL)
- {
- printf("Error allocating memory\n");
- err = ZIP_INTERNALERROR;
- }
- else
- {
- do
- {
- err = ZIP_OK;
- size_read = (int)fread(buf,,size_buf,fin);
- if (size_read < size_buf)
- if (feof(fin)==)
- {
- printf("error in reading %s\n",filenameinzip);
- err = ZIP_ERRNO;
- }
- if (size_read>)
- {
- err = zipWriteInFileInZip (zf,buf,size_read);
- if (err<)
- {
- printf("error in writing %s in the zipfile\n",
- filenameinzip);
- }
- }
- } while ((err == ZIP_OK) && (size_read>));
- free(buf);
- }
- }
- if (fin)
- fclose(fin);
- if (err<)
- err=ZIP_ERRNO;
- else
- {
- err = zipCloseFileInZip(zf);
- if (err!=ZIP_OK)
- printf("error in closing %s in the zipfile\n",
- filenameinzip);
- }
- return ;
- }
- int DoZipFile(const char* strFile, const char* dstLevel, const char* strZipFile, int replaceFlag)
- {
- int openMode = APPEND_STATUS_CREATE;
- if (check_exist_file(strZipFile))
- {
- if (replaceFlag == RG_ZIP_FILE_APPEND)
- {
- openMode = APPEND_STATUS_ADDINZIP;
- }
- else
- {
- openMode = APPEND_STATUS_CREATE;
- }
- }
- zipFile zf;
- zf = zipOpen64(strZipFile, openMode);
- if (zf)
- {
- CStdString strDstLevel;
- if(dstLevel) strDstLevel = dstLevel;
- strDstLevel.Trim();
- if(strDstLevel.IsEmpty())
- {
- strDstLevel = strFile;
- if(strDstLevel.Right() == "\\" || strDstLevel.Right() == "/")
- strDstLevel = strDstLevel.Left(strDstLevel.GetLength() - );
- int nFind = strDstLevel.ReverseFind('\\');
- if(nFind == -) nFind = strDstLevel.ReverseFind('/');
- if(nFind != -)
- strDstLevel = strDstLevel.Mid(nFind);
- }
- if (strDstLevel.Left() == "\\" || strDstLevel.Left() == "/")
- strDstLevel = strDstLevel.Right(strDstLevel.GetLength() - );
- DoZipFile(zf, strFile, strDstLevel.c_str());
- zipClose(zf, NULL);
- return ;
- }
- return ;
- }
- static int GetFilesFromDir(const char* strDir, vector<CStdString> &_fileInfo)
- {
- #ifdef _WIN32
- CStdString strFind = strDir;
- CStdString strPath;
- if (strFind.Right() != "\\")
- {
- strFind += "\\";
- }
- strPath = strFind;
- strFind += "*.*";
- WIN32_FIND_DATA wfd;
- HANDLE hFind = FindFirstFile(strFind, &wfd);
- if (hFind != INVALID_HANDLE_VALUE)
- {
- while(FindNextFile(hFind, &wfd))
- {
- if (strcmp(wfd.cFileName, ".") == || strcmp(wfd.cFileName, "..") == )
- {
- continue;
- }
- CStdString strFilePath = strPath + wfd.cFileName;
- if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
- {
- GetFilesFromDir(strFilePath.c_str(), _fileInfo);
- }
- else
- {
- _fileInfo.push_back(strFilePath);
- }
- }
- FindClose(hFind);
- return ;
- }
- return __LINE__;
- #else
- struct stat statbuf;
- struct dirent *dirp;
- DIR *dp;
- const char* szPath = strDir;
- if(lstat(szPath, &statbuf) < )
- {
- perror("lstat");
- return __LINE__;
- }
- if(S_ISDIR(statbuf.st_mode) == )
- {
- _fileInfo.push_back(szPath);
- return ;
- }
- if((dp = opendir(szPath)) == NULL)
- {
- perror("opendir");
- return __LINE__;
- }
- while((dirp = readdir(dp)) != NULL)
- {
- if(strcmp(dirp->d_name, ".") == ||
- strcmp(dirp->d_name, "..") == )
- continue;
- std::string subDir = string(szPath) + "/";
- subDir += dirp->d_name;
- if(dirp->d_type == DT_REG)
- {
- _fileInfo.push_back(subDir);
- }
- else if(dirp->d_type == DT_DIR)
- {
- GetFilesFromDir(subDir.c_str(), _fileInfo);
- }
- }
- closedir(dp);
- #endif
- }
- int DoZipDir(const char* strDir, const char* dstLevel, const char* strZipFile, int replaceFlag)
- {
- int openMode = APPEND_STATUS_CREATE;
- if(check_exist_file(strZipFile))
- {
- if (replaceFlag == RG_ZIP_FILE_APPEND)
- {
- openMode = APPEND_STATUS_ADDINZIP;
- }
- else
- {
- remove(strZipFile);
- openMode = APPEND_STATUS_CREATE;
- }
- }
- CStdString strDstLevel;
- if(dstLevel) strDstLevel= dstLevel;
- strDstLevel.Trim();
- if(strDstLevel.IsEmpty()) { //use current dir path as zip file path level
- strDstLevel = strDir;
- if(strDstLevel.Right() == "/" || strDstLevel.Right() == "\\") //remove the last slash
- strDstLevel = strDstLevel.Left(strDstLevel.GetLength() - );
- int nFind = strDstLevel.ReverseFind('\\'); //now get the dst level in zip file
- if(nFind == -) nFind = strDstLevel.ReverseFind('/');
- if(nFind != -) strDstLevel = strDstLevel.Mid(nFind);
- }
- //add pending slash
- #ifdef _WIN32
- if(strDstLevel.Right() != "\\") strDstLevel += "\\";
- #else
- if(strDstLevel.Right() != "/") strDstLevel += "/";
- #endif
- //remove slash at the beginning of the string
- if (strDstLevel.Left() == "\\" || strDstLevel.Left() == "/")
- strDstLevel = strDstLevel.Right(strDstLevel.GetLength() - );
- zipFile zf;
- zf = zipOpen64(strZipFile, openMode);
- if (zf)
- {
- vector<CStdString> _fileInfo;
- GetFilesFromDir(strDir, _fileInfo);
- for (size_t i = ; i < _fileInfo.size(); ++i)
- {
- CStdString strFilePath = _fileInfo[i];
- CStdString strFileLevel;
- int nFind = strFilePath.Find(strDir);
- if(nFind != -) strFileLevel = strFilePath.Mid(strlen(strDir));
- if (strFileLevel.Left() == "\\" || strFileLevel.Left() == "/")
- strFileLevel = strFileLevel.Right(strFileLevel.GetLength() - );
- strFileLevel = strDstLevel + strFileLevel;
- strFileLevel.Replace("\\", "/");
- DoZipFile(zf, strFilePath.c_str(), strFileLevel.c_str());
- // printf("%s\n%s\n", strFilePath.c_str(), strFileLevel.c_str());
- }
- zipClose(zf, NULL);
- return ;
- }
- return __LINE__;
- }
- #ifdef _TESTZIP
- int main(int argc, char* argv[])
- {
- if(argc < )
- {
- printf("Usage: %s zipfile filetozip\n", argv[]);
- getchar();
- return ;
- }
- const char* szZipFile = argv[];
- bool bFirst = true;
- for(int i = ; i < argc; ++i)
- {
- const char* filetozip = argv[i];
- DWORD dwAttr = GetFileAttributes(filetozip);
- if(dwAttr == INVALID_FILE_ATTRIBUTES)
- {
- printf("invalid file name: %s\n", filetozip);
- continue;
- }
- if(dwAttr & FILE_ATTRIBUTE_DIRECTORY)
- {
- DoZipDir(filetozip, "", szZipFile,
- bFirst ? RG_ZIP_FILE_REPLACE : RG_ZIP_FILE_APPEND);
- bFirst = false;
- }
- else if(dwAttr & FILE_ATTRIBUTE_NORMAL)
- {
- DoZipFile(filetozip, "", szZipFile,
- bFirst ? RG_ZIP_FILE_REPLACE : RG_ZIP_FILE_APPEND);
- bFirst = false;
- }
- }
- getchar();
- return ;
- }
- #endif
解压缩API实现,其中的CStdString可以使用CString代替,接口与CString一致。
其它涉及的代码可以下载minizip或者info-zip,推荐使用minizip,本代码是在minizip的基础上修改而来,支持跨平台
- /*
- miniunz.c
- Version 1.1, February 14h, 2010
- sample part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
- Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
- Modifications of Unzip for Zip64
- Copyright (C) 2007-2008 Even Rouault
- Modifications for Zip64 support on both zip and unzip
- Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
- */
- #ifndef _WIN32
- #ifndef __USE_FILE_OFFSET64
- #define __USE_FILE_OFFSET64
- #endif
- #ifndef __USE_LARGEFILE64
- #define __USE_LARGEFILE64
- #endif
- #ifndef _LARGEFILE64_SOURCE
- #define _LARGEFILE64_SOURCE
- #endif
- #ifndef _FILE_OFFSET_BIT
- #define _FILE_OFFSET_BIT 64
- #endif
- #endif
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include <errno.h>
- #include <fcntl.h>
- #ifdef _LINUX
- #include <unistd.h>
- #include <utime.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #else
- #include <direct.h>
- #include <io.h>
- #include <Windows.h>
- #endif
- #include "unzip.h"
- #include "stdstring.h"
- #define CASESENSITIVITY (0)
- #define WRITEBUFFERSIZE (8192)
- #define MAXFILENAME (256)
- /*
- #ifdef _WIN32
- #define USEWIN32IOAPI
- #include "iowin32.h"
- #endif
- */
- /*
- mini unzip, demo of unzip package
- usage :
- Usage : miniunz [-exvlo] file.zip [file_to_extract] [-d extractdir]
- list the file in the zipfile, and print the content of FILE_ID.ZIP or README.TXT
- if it exists
- */
- /* change_file_date : change the date/time of a file
- filename : the filename of the file where date/time must be modified
- dosdate : the new date at the MSDos format (4 bytes)
- tmu_date : the SAME new date at the tm_unz format */
- static void change_file_date(const char *filename, uLong dosdate, tm_unz tmu_date)
- {
- #ifdef _WIN32
- HANDLE hFile;
- FILETIME ftm,ftLocal,ftCreate,ftLastAcc,ftLastWrite;
- hFile = CreateFileA(filename,GENERIC_READ | GENERIC_WRITE,
- ,NULL,OPEN_EXISTING,,NULL);
- GetFileTime(hFile,&ftCreate,&ftLastAcc,&ftLastWrite);
- DosDateTimeToFileTime((WORD)(dosdate>>),(WORD)dosdate,&ftLocal);
- LocalFileTimeToFileTime(&ftLocal,&ftm);
- SetFileTime(hFile,&ftm,&ftLastAcc,&ftm);
- CloseHandle(hFile);
- #else
- #ifdef _LINUX
- struct utimbuf ut;
- struct tm newdate;
- newdate.tm_sec = tmu_date.tm_sec;
- newdate.tm_min=tmu_date.tm_min;
- newdate.tm_hour=tmu_date.tm_hour;
- newdate.tm_mday=tmu_date.tm_mday;
- newdate.tm_mon=tmu_date.tm_mon;
- if (tmu_date.tm_year > )
- newdate.tm_year=tmu_date.tm_year - ;
- else
- newdate.tm_year=tmu_date.tm_year ;
- newdate.tm_isdst=-;
- ut.actime=ut.modtime=mktime(&newdate);
- utime(filename,&ut);
- #endif
- #endif
- }
- /* mymkdir and change_file_date are not 100 % portable
- As I don't know well Unix, I wait feedback for the unix portion */
- static int mymkdir(const char* dirname)
- {
- int ret=;
- #ifdef _WIN32
- ret = _mkdir(dirname);
- #else
- #ifdef _LINUX
- ret = mkdir (dirname,);
- #endif
- #endif
- return ret;
- }
- int makedir (const char *newdir)
- {
- char *buffer ;
- char *p;
- int len = (int)strlen(newdir);
- if (len <= )
- return ;
- buffer = (char*)malloc(len+);
- if (buffer==NULL)
- {
- printf("Error allocating memory\n");
- return UNZ_INTERNALERROR;
- }
- strcpy(buffer,newdir);
- if (buffer[len-] == '/') {
- buffer[len-] = '\0';
- }
- if (mymkdir(buffer) == )
- {
- free(buffer);
- return ;
- }
- p = buffer+;
- while ()
- {
- char hold;
- while(*p && *p != '\\' && *p != '/')
- p++;
- hold = *p;
- *p = ;
- if ((mymkdir(buffer) == -) && (errno == ENOENT))
- {
- printf("couldn't create directory %s\n",buffer);
- free(buffer);
- return ;
- }
- if (hold == )
- break;
- *p++ = hold;
- }
- free(buffer);
- return ;
- }
- static int do_extract_currentfile(unzFile uf, const char* password)
- {
- char filename_inzip[];
- char* filename_withoutpath;
- char* p;
- int err=UNZ_OK;
- FILE *fout=NULL;
- void* buf;
- uInt size_buf;
- unz_file_info64 file_info;
- uLong ratio=;
- err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,,NULL,);
- if (err!=UNZ_OK)
- {
- printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
- return err;
- }
- size_buf = WRITEBUFFERSIZE;
- buf = (void*)malloc(size_buf);
- if (buf==NULL)
- {
- printf("Error allocating memory\n");
- return UNZ_INTERNALERROR;
- }
- p = filename_withoutpath = filename_inzip;
- while ((*p) != '\0')
- {
- if (((*p)=='/') || ((*p)=='\\'))
- filename_withoutpath = p+;
- p++;
- }
- if ((*filename_withoutpath)=='\0')
- {
- mymkdir(filename_inzip);
- }
- else
- {
- const char* write_filename;
- write_filename = filename_inzip;
- err = unzOpenCurrentFilePassword(uf,password);
- if (err==UNZ_OK)
- {
- fout=fopen64(write_filename,"wb");
- /* some zipfile don't contain directory alone before file */
- if ((fout==NULL) &&
- (filename_withoutpath!=(char*)filename_inzip))
- {
- char c=*(filename_withoutpath-);
- *(filename_withoutpath-)='\0';
- makedir(write_filename);
- *(filename_withoutpath-)=c;
- fout=fopen64(write_filename,"wb");
- }
- if (fout==NULL)
- {
- printf("error opening %s\n",write_filename);
- }
- }
- else
- {
- printf("error %d with zipfile in unzOpenCurrentFilePassword\n",err);
- }
- if (fout!=NULL)
- {
- do
- {
- err = unzReadCurrentFile(uf,buf,size_buf);
- if (err<)
- {
- printf("error %d with zipfile in unzReadCurrentFile\n",err);
- break;
- }
- if (err>)
- if (fwrite(buf,err,,fout)!=)
- {
- printf("error in writing extracted file\n");
- err=UNZ_ERRNO;
- break;
- }
- }
- while (err>);
- if (fout)
- fclose(fout);
- if (err==UNZ_OK)
- change_file_date(write_filename,file_info.dosDate,
- file_info.tmu_date);
- }
- if (err==UNZ_OK)
- {
- err = unzCloseCurrentFile (uf);
- if (err!=UNZ_OK)
- {
- printf("error %d with zipfile in unzCloseCurrentFile\n",err);
- }
- }
- else
- unzCloseCurrentFile(uf); /* don't lose the error */
- }
- free(buf);
- return err;
- }
- static int do_extract(unzFile uf, const char* password)
- {
- uLong i;
- unz_global_info64 gi;
- int err;
- FILE* fout=NULL;
- err = unzGetGlobalInfo64(uf,&gi);
- if (err!=UNZ_OK)
- printf("error %d with zipfile in unzGetGlobalInfo \n",err);
- for (i=;i<gi.number_entry;i++)
- {
- if (do_extract_currentfile(uf, password) != UNZ_OK)
- break;
- if ((i+) < gi.number_entry)
- {
- err = unzGoToNextFile(uf);
- if (err!=UNZ_OK)
- {
- printf("error %d with zipfile in unzGoToNextFile\n",err);
- break;
- }
- }
- }
- return ;
- }
- static int do_extract_onefile(unzFile uf, const char* filename, const char* password)
- {
- int err = UNZ_OK;
- if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK)
- {
- printf("file %s not found in the zipfile\n",filename);
- return ;
- }
- if (do_extract_currentfile(uf, password) == UNZ_OK)
- return ;
- else
- return ;
- }
- int DoUnzip(const char* szZipFile, const char* szTargetDir)
- {
- if (szZipFile == NULL)
- {
- return __LINE__;
- }
- CStdString dirname;
- if(szTargetDir) dirname = szTargetDir;
- dirname.Trim();
- unzFile uf = unzOpen64(szZipFile);
- if (uf)
- {
- #ifdef _WIN32
- if (!dirname.IsEmpty() && _chdir(dirname.c_str()))
- #else
- if (!dirname.IsEmpty() && chdir(dirname.c_str()))
- #endif
- {
- printf("Error changing into %s, aborting\n", dirname.c_str());
- return __LINE__;
- }
- do_extract(uf, NULL);
- unzClose(uf);
- return ;
- }
- return __LINE__;
- }
- int DoUnzipFile(const char* szZipFile, const char* srcFileToExtract, const char* szTargetDir)
- {
- if (szZipFile == NULL)
- {
- return __LINE__;
- }
- CStdString dirname;
- if(szTargetDir) dirname = szTargetDir;
- dirname.Trim();
- unzFile uf = unzOpen64(szZipFile);
- if (uf)
- {
- #ifdef _WIN32
- if (!dirname.IsEmpty() && _chdir(dirname.c_str()))
- #else
- if (!dirname.IsEmpty() && chdir(dirname.c_str()))
- #endif
- {
- printf("Error changing into %s, aborting\n", dirname.c_str());
- return __LINE__;
- }
- do_extract_onefile(uf, srcFileToExtract, NULL);
- unzClose(uf);
- return ;
- }
- return __LINE__;
- }
- #ifdef _TESTUNZIP
- int main(int argc, char* argv[])
- {
- if(argc < )
- {
- printf("Usage: %s zipfile\n", argv[]);
- return ;
- }
- bool bExtractSingalFile = false;
- for(int i = ; i < argc; ++i)
- {
- if(strcmp(argv[i], "-s") == )
- {
- if(i + < argc)
- {
- i++;
- const char* filetoextract = argv[i];
- printf("extract singal file %s\n", filetoextract);
- i++;
- if(i < argc)
- {
- DoUnzipFile(argv[i++], filetoextract, NULL);
- }
- }
- }
- else
- DoUnzip(argv[i], NULL);
- }
- return ;
- }
- #endif
跨平台的zip文件压缩处理,支持压缩解压文件夹的更多相关文章
- Linux命令(16)压缩,解压文件
tar: 简介:tar命令只是把目录打包成一个归档(文件),并不负责压缩.在tar命令中可以带参数调用gzip或bzip2压缩.因为gzip和bzip2只能压缩单个文件. 在linux下是不需要后缀名 ...
- 关于Java解压文件的一些坑及经验分享(MALFORMED异常)
文章也已经同步到我的csdn博客: http://blog.csdn.net/u012881584/article/details/72615481 关于Java解压文件的一些坑及经验分享 就在本周, ...
- java批量解压文件夹下的所有压缩文件(.rar、.zip、.gz、.tar.gz)
// java批量解压文件夹下的所有压缩文件(.rar..zip..gz..tar.gz) 新建工具类: package com.mobile.utils; import com.github.jun ...
- .NET使用ICSharpCode.SharpZipLib压缩/解压文件
SharpZipLib是国外开源加压解压库,可以方便的对文件进行加压/解压 1.下载ICSharpCode.SharpZipLib.dll,并复制到bin目录下 http://www.icsharpc ...
- SpringMVC上传压缩文件,解压文件,并检测上传文件中是否有index.html
SpringMVC上传压缩文件,解压文件,并检测上传文件中是否有index.html 说明: 1.环境:SpringMVC+Spring+Tomcat7+JDK1.7 2.支持 zip和rar格式的压 ...
- 【转载】.NET压缩/解压文件/夹组件
转自:http://www.cnblogs.com/asxinyu/archive/2013/03/05/2943696.html 阅读目录 1.前言 2.关于压缩格式和算法的基础 3.几种常见的.N ...
- 本地上传文件至服务器的技巧(linux文件压缩及解压文件)
linux(ubuntu)文件解压及压缩文件 ubuntu支持文件的解压及压缩功能, 如果ubuntu上面没有安装过unzip工具的话,可以通过下面命令安装: sudo apt-get install ...
- C# 压缩、解压文件夹或文件(带密码)
今天梳理一下项目中用到的压缩.解压文件夹或文件的方法,发现因为需求不同,已经用了好几个不同组件.今天就好好整理记录下,别下次遇到需求又重头开始了. DotNetZip DotNetZip是一个开源的免 ...
- Unity3D研究院之LZMA压缩文件与解压文件
原地址:http://www.xuanyusong.com/archives/3095 前两天有朋友告诉我Unity的Assetbundle是LZMA压缩的,刚好今天有时间那么就研究研究LZMA.它是 ...
- AIX系统上压缩与解压文件
压缩. 命令格式: #tar -cvf (或xvf)+文件名+设备 C:是本地到其他设备 x:是其他设备到本地 r:是追加,比如打包时,将其他文件追加进来使用该参数. t:显示tar包里的内容,但还原 ...
随机推荐
- SQL第二课-创建数据表
查看有多少数据库 SHOW DATABASES; 进入数据库:USE <数据库名> 举例:USE test;//进入test数据库 查看当前进入的是哪个数据库 SELECT DATABAS ...
- 如何vs升级后10和12都能同时兼容
如图: 项目2008解决方案sln文件升级2012后,都能同时使用. 升级办法:先复制vs2008版本的解决方案文件.升级2012后,再将文件复制到目录里面即可.注意升级过程中产生的升级文件(Upgr ...
- BAE、SAE 与 GAE 对比
从数据库.应用配置.计费.域名绑定.平台服务对比了 BAE.SAE 以及 GAE 的优劣,最后给出云平台选型的建议. 数据库SAE 不支持 InnoDB(可申请支持),BAE 默认支持. BAE 不支 ...
- docker-compose.yml 语法说明
YAML 模板文件语法 默认的模板文件是 docker-compose.yml,其中定义的每个服务都必须通过 image 指令指定镜像或 build 指令(需要 Dockerfile)来自动构建. 其 ...
- ReentrantLock与synchronized的差别
总的来说,lock更加灵活. 主要同样点:Lock能完毕synchronized所实现的全部功能 不同: 1.ReentrantLock功能性方面更全面,比方时间锁等候,可中断锁等候,锁投票等,因此更 ...
- thinkphp实现短信验证注册
前言 注册时经常需要用到短信验证码,本文记录一下思路和具体实现. 短信验证平台使用云片,短信验证码的生成使用thinkphp. 思路 1.用户输入手机号,请求获取短信验证码. 2.thinkphp生成 ...
- 简单回顾C++中的字符串
C++中有两种字符串形式,一种是C语言字符数组,一般可以使用 char*指针来操作它:另一种是C++中基于标准库的string类型,这算是更高层次的抽象数据类型. 主要讨论一下string类型,既然是 ...
- eclipse4.3 kepler中安装maven
1.软件准备 a:Eclipse 4.3 http://www.eclipse.org/downloads/ b:maven http://maven.apache.org/download.cgi ...
- js 的post提交的写法
function AddEditDevice(data){ var form = $("#deviceEditform"); if (form.length == 0) { for ...
- 23、Javascript DOM
DOM Document Object Model(文档对象模型)定义了html和xml的文档标准. DOM 节点树 <html> <head> <title>DO ...