Error Creating Zip File from Folder w/ Subdirectories

Hi all-

I’ve been using JUCE for a little while and am currently trying to create a function that creates a zip file from a directory using the Zip_File class. Everything runs smoothly if the directory only contains single files or if I only search for single files and not subdirectories. However, if the directory contains subdirectories and I try to include them in the zipped file, I get the following assertion in FileInputStream::read when I try to write the ZipFile::Builder object to a stream :

Here is my code for trying to zip up the directory, which takes a file to be zipped and outputs a file with a predetermined path:

[code]void eftFile::compress(File tempFolder, File outputZip)
{
ZipFile::Builder zipBuilder;
Array tempFiles;
//find files
tempFolder.findChildFiles(tempFiles, File::findFilesAndDirectories, true, “*”);

//add files to Builder object                 
for (int i = 0; i < tempFiles.size(); i++)
{
    zipBuilder.addFile(tempFiles[i], 9);
}

//save our zip file
double *progress = nullptr;
if (outputZip.exists())
{
    outputZip.deleteFile();
}
FileOutputStream os (outputZip);
zipBuilder.writeToStream(os, progress);

}[/code]

The assertion arises when I do writeToStream. I know that there is something very obvious here that I am missing but I haven’t been able to solve it so far, I’m not too familiar with how zip files work. If anyone could let me know what I’m doing wrong or point me in the right direction that would be great, thanks!

-Tom

Looks like you’re passing directories to the addFile method.

Ah, yes, I thought that might be it. Is there an easy way of maintaining file hierarchy with directories if I’m creating a zip then, so that when I unzip the file later on the directories will still be intact?

I thought it already preserved the structure?

Yep, I stupidly wasn’t specifying the stored path name in Builder::addFile. Works perfectly now, thanks very much!

Here’s what I have now should anyone ever need it:

[code]void eftFile::compress(File tempFolder, File outputZip)
{
ZipFile::Builder zipBuilder;
Array tempFiles;
tempFolder.findChildFiles(tempFiles, File::findFiles, true, “*”);

//add files
for (int i = 0; i < tempFiles.size(); i++)
{
    zipBuilder.addFile(tempFiles[i], 0, tempFiles[i].getRelativePathFrom(outputZip));
}

//save our zip file
double *progress = nullptr;
if (outputZip.exists())
{
    outputZip.deleteFile();
}
FileOutputStream os (outputZip);
zipBuilder.writeToStream(os, progress);

}[/code]

2 Likes