I am trying to use libzip with zip_source_buffer
, to write data from local buffers to a 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++;
}
Unfortunately, I discovered corrupt data inside my zip file and while trying to debug the results my program segfaulted in apparently unrelated code (that had worked before).
I then tried running my program in valgrind and got memory errors from zip_close
. What am I doing wrong?