使用 libzip 将文件解压缩到磁盘
Unzipping a file to disk using libzip
我正在尝试使用 c++ 和 libzip 库解压缩可执行文件。从 rodrigo 对类似问题的回答开始,我来到此示例代码:
#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);
}
据我了解,这段代码确实可以解压缩文件,但我不知道如何将该文件写入磁盘,就像使用 Winzip 这样的工具提取 zip 文件一样。将解压缩的数据放在内存中对我没有帮助,但是我一直无法弄清楚如何将文件实际放入磁盘。
这样的事情应该这样做:
if(!std::ofstream("file1.txt").write(contents, st.size))
{
std::cerr << "Error writing file" << 'n';
return EXIT_FAILURE;
}
查找 std::ofstream。
当然,您应该检查所有zip
文件函数,看看它们是否返回了错误,然后再继续。
相关文章:
- 使用C++进行运行长度解压缩
- C++ 如何将数组值解压缩为函数参数
- struct.error:解压缩 C++ 结构时,解包需要 288 字节的缓冲区
- 在 Qt(C++) 中使用 QProcess 解压缩 - 提取目录问题
- 浏览压缩文件与游览解压缩它们
- 如何在C++向量中解压缩多个值
- 解压缩 C 样式数组以及C++中的参数包
- 如何在 cpp 中解压缩数字,如果它们是使用 struct.pack(fmt, v1, v2, ..) 打包在 pyth
- 如何使用 Poco::ZIP 压缩/解压缩 zip 文件
- 在编译时解压缩数组扩展数据块 (C++11/14)
- 使用 RtMidi 解压缩 Midi 时间码
- 将参数包解压缩到 std::initializer_list?
- 解压缩可变模板参数
- 解压缩附加的压缩字符串
- 使用 AVX2 将 8 位从 32 位值 (__m256i) 解压缩到__m256的最快方法
- C++宏解压缩并加入
- 解压缩串联的 zlib 流而不读取下一个字节
- 解压缩位大小不能被 8 整除的值流
- 为流运算符返回代理类时解压缩参数
- 使用 libzip 将文件解压缩到磁盘