使用libzip将文件解压缩到磁盘

编程入门 行业动态 更新时间:2024-10-19 19:38:12
本文介绍了使用libzip将文件解压缩到磁盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用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将文件解压缩到磁盘

本文发布于:2023-11-05 08:08:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560316.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:解压缩   磁盘   文件   libzip

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!