[SOLVED] Use ZipFile::Builder with directories

I am trying to save a directory (with two layers of sub-dirs) to a zip file.

I had no luck so far in adding the directory directly via addFile(). If I try, I get stuck in an infinite loop, which toggles between two assertions regarding FileStream::OpenedOk().

I take from this ancient thread that the function takes only “Files”, not directories. (Which is in no way reflected in the documentation, but juce::Files can either be dirs or files).

So I am now trying to run a loop over the dir, which exposes all the files in the two subdirs and add them manually. This works, however, the folder structure is not preserved in this approach.

File soundbank_file("/path/to/dir");
ZipFile::Builder zip_builder;

auto sub_folders = soundbank_file.findChildFiles(File::TypesOfFileToFind::findDirectories, false);

for(int sub_index = 0; sub_index < sub_folders.size(); ++sub_index){

	auto preset_files = sub_folders[sub_index].findChildFiles(File::TypesOfFileToFind::findFiles, false, "*.odin");
	for(int preset_index = 0; preset_index < preset_files.size(); ++preset_index){

		zip_builder.addFile(preset_files[preset_index], 5, preset_files[preset_index].getFullPathName());
	}
}

zip_builder.writeToStream(file_stream, nullptr);

I tried modifying the StoredPathName parameter of addFile, which I guess is for this purpose? The documentation isn’t superclear here either…

Anyone who came across a similar problem and can show the way?
Thanks!

I found the solution mere seconds after posting this…

I had to give the relative path as an argument in the loop. I was giving it an absolute path.

Here’s the updated code:

auto sub_folders = soundbank_file.findChildFiles(File::TypesOfFileToFind::findDirectories, false);

for(int sub_index = 0; sub_index < sub_folders.size(); ++sub_index){
	auto preset_files = sub_folders[sub_index].findChildFiles(File::TypesOfFileToFind::findFiles, false, "*.odin");

	for(int preset_index = 0; preset_index < preset_files.size(); ++preset_index){

		zip_builder.addFile(preset_files[preset_index], 5, preset_files[preset_index].getRelativePathFrom(soundbank_file));
	}
}