I'm currently trying to open a zip archive, create another zip archive in another folder, and copy each file in the source archive over to the new zip archive file-by-file. Right now, I have that sort of working, but I can't open the zip archives on my Mac. It keeps telling me "Error 2 - No such file or directory". To me, it sounds like a handle or pointer in libzip isn't getting closed after I'm done using it.
Here's my code:
void ProcessZipArchive(std::string filename)
{
int err = 0;
std::string entryFilename;
std::string outputArchivePath = directoryPath + OUTPUT_NAME + "/" + filename;
// open the original archive path
inputArchive = zip_open((directoryPath + filename).c_str(), 0, &err);
if(!inputArchive || err != ZIP_ER_OK)
{
printf("ERROR: an error occurred opening zip archive (%i)\n", err);
return;
}
// create the output archive
outputArchive = zip_open(outputArchivePath.c_str(), ZIP_CREATE, &err);
if(!outputArchive || err != ZIP_ER_OK)
{
printf("ERROR: an error occurred creating output zip archive: %s (%i)\n", outputArchivePath.c_str(), err);
return;
}
// get the number of files
int numFiles = (int)zip_get_num_entries(inputArchive, ZIP_FL_UNCHANGED);
if(numFiles == -1)
{
printf("bad archive (-1)\n");
return;
}
// iterate all files
printf(" numFiles: %i\n", numFiles);
for(int i=0;i<numFiles;++i)
{
// get the current entry's name, and check if it's a PNG image
entryFilename = zip_get_name(inputArchive, i, ZIP_FL_ENC_RAW);
if(compressTexture && GetFileExtension(entryFilename) == "png")
{
// do special processing here...
} else
CopyFile(entryFilename, i);
}
// close the archive
if(inputArchive)
{
err = zip_close(inputArchive);
if(err != 0)
{
zip_error_get(inputArchive, &err, NULL);
printf("ERROR: could not close input archive (%i) %s\n", err, zip_strerror(inputArchive));
} else
inputArchive = nullptr;
}
// close the output archive
if(outputArchive)
{
err = zip_close(outputArchive);
if(err != 0)
{
int sysErr = 0;
zip_error_get(outputArchive, &err, &sysErr);
printf("ERROR: could not close output archive (%i) %s\n", sysErr, zip_strerror(outputArchive));
} else
outputArchive = nullptr;
}
printf("\n\n");
}
void CopyFile(std::string filename, int index)
{
// create the source
zip_source *source = zip_source_zip(outputArchive, inputArchive, index, 0, 0, -1);
if(!source)
{
printf("ERROR: could not create source\n");
return;
}
// write the file to the output archive
int outputIndex = (int)zip_file_add(outputArchive, filename.c_str(), source, ZIP_FL_ENC_UTF_8);
printf(" created new file: %s (%i)\n", filename.c_str(), outputIndex);
}
I'm pretty sure the issue with my zip_source object. I'm using zip_source_zip to copy a file from the original archive to the new archive. If I replace zip_source_zip with zip_source_buffer, and just supply a random 4-byte buffer, everything works. I can open the archives, and all the files are 4 bytes large haha.