我正在尝试将 libzip 与zip_source_buffer
, 一起使用,将数据从本地缓冲区写入 zipfile。
std::vector<uchar> buf;
struct zip * z = zip_open(zipfilename.c_str(),ZIP_CREATE | ZIP_EXCL,NULL);
if (!z) {
messagelabel->setText("failed to open zip file "+QString::fromStdString(zipfilename));
return "";
}
//messagelabel->setText(QString::fromStdString(zipfilename));
//messagelabel->setText(QString::number(ulong(z)));
int errors = 0;
for (int i=0;i<16;i++) {
imencode(format, frame[i],buf);
struct zip_source *s;
s = zip_source_buffer(z,buf.data(),buf.size(),0);
std::string nameinzip = ("band"+to_string(i)+format);
if (zip_add(z,nameinzip.c_str(),s) < 0) {
messagelabel->setText("failed to add "+QString::fromStdString(nameinzip)+" to zip file");
errors++;
break;
}
}
if (errors == 0) {
struct zip_source *s;
std::string caltext = caltotext(calfile);
cout << caltext;
s = zip_source_buffer(z,caltext.data(),caltext.size(),0);
std::string nameinzip = "calinfo.csv";
if (zip_add(z,nameinzip.c_str(),s) < 0) {
messagelabel->setText("failed to add "+QString::fromStdString(nameinzip)+" to zip file");
errors++;
}
}
if (zip_close(z) < 0) {
messagelabel->setText("failed to close zip file "+QString::fromStdString(zipfilename));
errors++;
}
不幸的是,我在我的 zip 文件中发现了损坏的数据,并且在尝试调试结果时,我的程序在明显不相关的代码中出现了段错误(以前可以工作)。
然后我尝试在 valgrind 中运行我的程序并从zip_close
. 我究竟做错了什么?