0

I'm trying to find out the solution to solve a problem; In fact, i'm writing my own tool to make saves using libzip in C++ to compress the files.

Absolutly not finished but i wanted to make some tests, then i do and obtain a "funny" error from the log.

Here's my function:

void save(std::vector<std::string> filepath, std::string savepath){
int err;

savepath += time(NULL);
zip* saveArchive = zip_open(savepath.c_str(), ZIP_CREATE , &err);
if(err != ZIP_ER_OK) throw xif::sys_error("Error while opening the archive", zip_strerror(saveArchive));
for(int i = 0; i < filepath.size(); i++){
    if(filepath[i].find("/") == std::string::npos){}
    if(filepath[i].find(".cfg") == std::string::npos){
        err = (int) zip_file_add(saveArchive, filepath[i].c_str(), NULL, NULL);
        if(err == -1) throw xif::sys_error("Error while adding the files", zip_strerror(saveArchive));
    }

}

if(zip_close(saveArchive) == -1) throw xif::sys_error("Error while closing the archive", zip_strerror(saveArchive));
}

I get a => Error : Error while opening the archive : No error And, of course, i didn't have any .zip written.

If you could help me, thanks to you !

eldten
  • 3
  • 1

1 Answers1

1

The documentation for zip_open says that it only sets *errorp if the open fails. Either test for saveArchive == nullptr or initialize err to ZIP_ER_OK.

P.S. The search for '/' does nothing. Did you mean to put a continue in that block?

The other problematic line is:

    savepath += time(NULL);

If that is the standard time function, that returns a time in seconds since the epoch. That will probably get truncated to a char, and then that char appended to the file name. That will cause strange characters to appear in the filename! I suggest using std::chrono to convert to text.

  • I didn't get this stupid "no error" anymore, but it doesn't create a .zip file too ... The search for '/' is not implemented yet. Thanks for reply. – eldten May 21 '16 at 16:03
  • Step through it in a debugger. Is there anything in the vector? What does add return? – Martin Bonner supports Monica May 21 '16 at 16:14
  • I think that i didn't understand how zip_file_add works. Indeed, function doesn't compress and add my file to the archive but it makes just a path inside. Thanks for helping. – eldten May 21 '16 at 17:20