C++11 can';t使用libzip C+创建zip文件+;

C++11 can';t使用libzip C+创建zip文件+;,c++11,libzip,C++11,Libzip,我正在尝试使用libzip库创建zip文件 list<string> ListOfFiles; ListOfFiles.push_back("file1"); ListOfFiles.push_back("file2"); ListOfFiles.push_back("file3"); ... createZip(const char* destination) { int err; zip *archive = zip_open(destination, ZIP_CR

我正在尝试使用libzip库创建zip文件

list<string> ListOfFiles; 

ListOfFiles.push_back("file1");
ListOfFiles.push_back("file2");
ListOfFiles.push_back("file3");
... 

createZip(const char* destination)
{
 int err;

 zip *archive = zip_open(destination, ZIP_CREATE, &err);
 cout << "1. " << zip_strerror(archive) << endl;

    for (list<string>::iterator iter = ListOfFiles.begin(), end = ListOfFiles.end(); iter != end; iter++)
    {
        zip_source *source = zip_source_file(archive,iter->c_str(),0,0);             
        cout << "2. " << zip_strerror(archive) << endl;

        index = zip_file_add(archive, iter->c_str(), source, ZIP_FL_OVERWRITE);
        cout << "3. " << zip_strerror(archive) << endl; 
   }

  zip_close(archive);
  cout << "4. " << zip_strerror(archive) << endl;
}
文件列表;
文件列表。推回(“文件1”);
文件列表。推回(“文件2”);
文件列表。推回(“文件3”);
... 
createZip(常量字符*目的地)
{
INTERR;
zip*archive=zip\u open(目标、zip\u创建和错误);

cout需要更多帮助,这里是我的代码

int main() {

    int err = 0;
    ifstream fs, fs1;
    fs.open("my_test.txt", std::ios::binary);
    fs1.open("f 2.pdf", std::ios::binary);
    std::string content((std::istreambuf_iterator<char>(fs)), (std::istreambuf_iterator<char>()));
    std::string content1((std::istreambuf_iterator<char>(fs1)), (std::istreambuf_iterator<char>()));

    zip* z = zip_open("foo.zip", ZIP_CREATE, &err);
    zip_source_t* zs;

    zs = zip_source_buffer(z, content.c_str(), content.length(), 0);

    zip_file_add(z, "test.txt", zs, ZIP_FL_OVERWRITE | ZIP_FL_ENC_UTF_8);
    zs = zip_source_buffer(z, content1.c_str(), content1.length(), 0);
    zip_file_add(z, "test.pdf", zs, ZIP_FL_OVERWRITE | ZIP_FL_ENC_UTF_8);

    zip_close(z);}
intmain(){
int err=0;
iffs,fs1;
open(“my_test.txt”,std::ios::binary);
fs1.open(“f2.pdf”,std::ios::binary);
std::字符串内容((std::istreambuf_迭代器(fs)),(std::istreambuf_迭代器());
std::string content1((std::istreambuf_迭代器(fs1)),(std::istreambuf_迭代器());
zip*z=zip\u open(“foo.zip”,zip\u CREATE,&err);
zip_source_t*zs;
zs=zip\u源缓冲区(z,content.c\u str(),content.length(),0);
zip_文件添加(z,“test.txt”,zs,zip_FL_覆盖,zip_FL_ENC_UTF_8);
zs=zip_源_缓冲区(z,content1.c_str(),content1.length(),0);
zip_文件添加(z,“test.pdf”,zs,zip_FL_覆盖| zip_FL_ENC_UTF_8);
zip_close(z);}

我以二进制模式打开了两个文件(一个txt文件和一个pdf文件),并读取了整个文件。我已将其数据存储到两个
字符串内容中,下一步是创建源缓冲区。第一个参数是zip文件,第二个参数是内容,第三个参数是要写入的内容的长度。最后,我们在zip中添加缓冲区。小心
zip\u file\u add
使用提供的名称创建新文件(my\u test.txt->text.txt和f2.pdf->test.pdf)

表示无效参数表示“源或名称为空”。你能在第2步中再次检查
source
是否为
NULL
吗?否则,在阅读libzip文档的基础上,我觉得你的代码很好。此外,在第1步中,检查
err
是否未设置,以及
archive
是否为
NULL
,然后再让
zip\u strerror
查看它,因为它可能为NULL。据我所知,如果其中一个文件不存在,则最后一个错误是正常的。存档是正常的,当我尝试将文件名直接作为zip_source_文件的参数而不是迭代器时,一切正常。我将在回家后尝试检查
source
是否为NULL