我正在尝试使用c ++和libzip库解压缩可执行文件.从rodrigo对类似问题的答案开始,我来看下面的示例代码:
I'm trying to unzip an executable file using c++ and the libzip library. Starting with the answer by rodrigo on a similar question, I come to this sample code:
#include <zip.h> int main() { //Open the ZIP archive int err = 0; zip *z = zip_open("foo.zip", 0, &err); //Search for the file of given name const char *name = "file.txt"; struct zip_stat st; zip_stat_init(&st); zip_stat(z, name, 0, &st); //Alloc memory for its uncompressed contents char *contents = new char[st.size]; //Read the compressed file zip_file *f = zip_fopen(z, "file.txt", 0); zip_fread(f, contents, st.size); zip_fclose(f); //And close the archive zip_close(z); }据我了解,此代码确实可以解压缩文件,但我不知道如何将文件写入磁盘,就像提取zip文件一样,使用诸如winzip之类的工具也是如此.将解压缩后的数据存储在内存中对我没有帮助,但是我一直无法弄清楚如何将文件实际存储到磁盘上.
from what I understand, this code does work to unzip a file, but I do not know how to write that file to the disk, much like extracting a zip file would so using a tool like winzip does. Having the unzipped data in memory doesn't help me, but I've been unable to figure out how to actually get the files onto the disk.
推荐答案应该执行以下操作:
if(!std::ofstream("file1.txt").write(contents, st.size)) { std::cerr << "Error writing file" << '\n'; return EXIT_FAILURE; }查找 std :: ofstream .
当然,您应该在继续操作之前检查所有zip文件功能,以查看它们是否返回了错误.
Of course you should check all your zip file functions to see if they returned errors before proceeding.
更多推荐
使用libzip将文件解压缩到磁盘
发布评论