[使用zlib库解压提取文件]

场景:

1. zlib库跨平台,Windows和MacOSX都可以使用,还支持64位编译,轻量级,没有不用的道理。

2. 处理.zip,apk,docx文件时,因为这类文件都是zip格式,使用zlib能方便提取压缩文件里的数据。

方法:

1. 除了要使用zlib库本身,还需要使用zlib里的contrib目录minizip部分头文件和.c文件。

zlib-1.2.5/src/contrib/minizip/unzip.hunzip.c,ioapi.c,ioapi.h (windows还需要iowin32.h, iowind32.c)

2. 例子,读取压缩文件里的某个文件到内存std::string, 基本原理就是打开zip文件,,定位需要读取的文件,之后读取这个文件,关闭zip文件。

#include "zip/zip_util.h"#include <iostream>#include <string>#include <algorithm>#include <stdlib.h>#include <zlib.h>#include "zip/unzip.h"using namespace std;#define WRITEBUFFERSIZE (1242880) // 5Mb bufferstring ZipUtil::ReadZipFile(string zipFile, string fileInZip) {int err = UNZ_OK;// error statusuInt size_buf = WRITEBUFFERSIZE; // byte size of buffer to store raw csv datavoid* buf;// the bufferstring sout;// output stringschar filename_inzip[256];// for unzGetCurrentFileInfounz_file_info file_info;// for unzGetCurrentFileInfounzFile uf = unzOpen(zipFile.c_str()); // open zipfile streamif (uf==NULL) {cerr << "Cannot open " << zipFile << endl;return sout;} // file is openif ( unzLocateFile(uf,fileInZip.c_str(),1) ) { // try to locate file inside zip// second argument of unzLocateFile: 1 = case sensitive, 0 = case-insensitivecerr << "File " << fileInZip << " not found in " << zipFile << endl;return sout;} // file inside zip foundif (unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0)) {cerr << "Error " << err << " with zipfile " << zipFile << " in unzGetCurrentFileInfo." << endl;return sout;} // obtained the necessary details about file inside zipbuf = (void*)malloc(size_buf); // setup bufferif (buf==NULL) {cerr << "Error allocating memory for read buffer" << endl;return sout;} // buffer readyerr = unzOpenCurrentFilePassword(uf,NULL); // Open the file inside the zip (password = NULL)if (err!=UNZ_OK) {cerr << "Error " << err << " with zipfile " << zipFile << " in unzOpenCurrentFilePassword." << endl;return sout;} // file inside the zip is open// Copy contents of the file inside the zip to the buffercout << "Extracting: " << filename_inzip << " from " << zipFile << endl;do {err = unzReadCurrentFile(uf,buf,size_buf);if (err<0) {cerr << "Error " << err << " with zipfile " << zipFile << " in unzReadCurrentFile" << endl;sout = ""; // empty output stringbreak;}// copy the buffer to a stringif (err>0) for (int i = 0; i < (int) err; i++) sout.push_back( *(((char*)buf)+i) );} while (err>0);err = unzCloseCurrentFile (uf); // close the zipfileif (err!=UNZ_OK) {cerr << "Error " << err << " with zipfile " << zipFile << " in unzCloseCurrentFile" << endl;sout = ""; // empty output string}free(buf); // free up buffer memoryreturn sout;}

钱财何足贵,仁义值千金。

[使用zlib库解压提取文件]

相关文章:

你感兴趣的文章:

标签云: