I am trying to get a function going to unzip a single text file compressed with .gz. It needs to uncompress the .gz file given its path and write the uncompressed text file given its destination. I am using C++ and what I have seen is that ZLIB does exactly what I need except I cannot find 1 single example anywhere on the net that shows it doing this. Can anyone show me an example or at least guide me in the right direction?
23467 次
4 に答える
5
圧縮された生データ (つまり、アーカイブなし) でファイルを膨張させたいだけの場合は、次のようなものを使用できます。
gzFile inFileZ = gzopen(fileName, "rb");
if (inFileZ == NULL) {
printf("Error: Failed to gzopen %s\n", filename);
exit(0);
}
unsigned char unzipBuffer[8192];
unsigned int unzippedBytes;
std::vector<unsigned char> unzippedData;
while (true) {
unzippedBytes = gzread(inFileZ, unzipBuffer, 8192);
if (unzippedBytes > 0) {
unzippedData.insert(unzippedData.end(), unzipBuffer, unzipBuffer + unzippedBytes);
} else {
break;
}
}
gzclose(inFileZ);
unzippedData
ベクトルは膨張したデータを保持するようになりました。特に圧縮前のサイズが事前にわかっている場合は、膨張したデータを保存するためのより効率的な方法がおそらくありますが、このアプローチは私にとってはうまくいきます。
それ以上の処理を行わずに、膨張したデータのみをファイルに保存したい場合は、ベクターをスキップして、unzipBuffer
コンテンツを別のファイルに書き込むことができます。
于 2013-06-12T09:27:59.307 に答える
2
gzopen()
、gzread()
、およびgzclose()
zlib の関数を、対応する stdio 関数などと同じように使用できますfopen()
。これにより、gzip ファイルが読み取られ、解凍されます。その後fopen()
、fwrite()
、 などを使用して、圧縮されていないデータを書き戻すことができます。
于 2013-06-12T05:05:17.683 に答える
1
これを行うには、ZLibComplete を使用できます。GZip 解凍のフロント ページに C++ の完全な例があります。
于 2015-08-16T18:59:00.230 に答える
0
ああ、私はhttp://zlib.net/zlib_how.htmlがあなたが望むことをすると思いますか?
于 2013-06-12T03:45:13.017 に答える