我使用头zip.hpp (可以在这里找到https://markand.bitbucket.io/libzip/index.html或http://hg.markand.fr/libzip/file)来压缩一些文件。
之后要删除原始文件,我使用remove("myfile.txt")。
显然,zip.hpp在运行时结束时会压缩文件,因此它找不到文件,也不会创建压缩文件夹。如果我省略了remove("myfile.txt"),一切都很好,除了有几个文件在我周围飞来飞去,我只想有它们的压缩形式。
您知道如何强制libzip编写zip文件吗?我希望如果删除archive-instance,它会强制创建,但显然libzip::Archive-class没有析构函数(至少我找不到一个,delete archive抛出了许多错误)。
我的基本代码如下所示:
#include <fstream>
#include <zip.h>
#include "lib/zip.hpp"
int main () {
libzip::Archive archive("output.zip", ZIP_CREATE);
std::ofstream outfile ("myfile.txt");
outfile << "Hello World\n";
outfile.close();
archive.add(libzip::source::file("myfile.txt"), "myfile2.txt");
// delete archive; // throws an error...
// remove("myfile.txt");
// if left out, output.zip gets created otherwise nothing is created
return 0;
}发布于 2017-02-01 04:09:02
当libzip::Archive超出作用域时,它将写入其内容。因此,您所需要做的就是在删除文件之前引入一个调整范围。
#include <fstream>
#include <zip.h>
#include "lib/zip.hpp"
int main () {
{ // Additional scope
libzip::Archive archive("output.zip", ZIP_CREATE);
std::ofstream outfile ("myfile.txt");
outfile << "Hello World\n";
outfile.close();
archive.add(libzip::source::file("myfile.txt"), "myfile2.txt");
} // Archive is written now.
remove("myfile.txt");
return 0;
}https://stackoverflow.com/questions/41971698
复制相似问题