Unzip files to memory and search by files

Hi,
Spent some time in forum trying to find the answer, but hadn’t found.

I have a zip archive. Inside it I have wav and xml files.
I need to find xml by extension and read it. Audio files I need to load into my synth.

How can I do it without unpacking it to a new folder on a disk?

As I understand it must be something like that:

File file(“path_to_zip_file”);

ZipFile zip (file);

for (int i = 0; i < zip.getNumEntries(); ++i)
{
    std::unique_ptr<InputStream> stream (zip.createStreamForEntry (i));
    
}

But what are the next steps?

Thank you

I have not used the Zip class before, but it looks like you want to iterate over the contents using ZipFile::getEntry(int index), and examine the ZipEntry::filename to see while file type it is. I would put the filename in a File object, as it will make it easy to extract the extension, so you can determine file type. Then, using the InputStream, from the createStreamForEntry API, you could use InputStream::readIntoMemoryBlock (MemoryBlock &destBlock, ssize_t maxNumBytesToRead=-1) to read the whole thing in.

2 Likes

Thank you for the answer. Yes, I’m doing something like that:

 for (int i = 0; i < zip.getNumEntries(); ++i)
    {
        std::unique_ptr<InputStream> stream (zip.createStreamForEntry (i));
    
    if (stream != nullptr)
    {
      
        auto entry = zip.getEntry(i);
        auto entryName = entry->filename;
        auto size = entry->uncompressedSize;
        auto block = std::make_unique<MemoryBlock>(MemoryBlock());
        stream->readIntoMemoryBlock(*block, size);
    }
}

But in that case I have a question

I have a structure like:

zip file

  • instrument1 folder
    • info.xml
    • folder with samples
  • instrument2 folder
    • info.xml
    • folder with samples
  • instrument3 folder
    • info.xml
    • folder with samples
  • instrumentX folder
    • info.xml
    • folder with samples

Xml file has information about samples for each instrument.
So I create Instrument object with this info from xml and Array of samples.
I can do it easily if I looking for xml file and then and all wav file from the parent directory to the array.

But with Zip file I can find xml file, but how can I read all wav files from the parent directory of this xml if iterate over the contents?

I’m sorry, I don’t really understand your question…

ok:)
if it’s just folder, not zip file, I can find xml document, parse it and find all wav files from the same directory with this xml file.
Something like that:
auto files = xml.getParentDirectory().findChildFiles(File::findFiles, true , “*.wav”);

But how can I do something like that with zip file? As I understand, I can only find all xml files and add it to array. Than iterate through this array and zip content again and find all wav files with the same parent directory with the xml.

Due to my lack of understanding of how the zipfile content is stored, I can’t say exactly. I don’t see any API’s for navigating folders, so I assume the folder names are part of ZipEntry::filename? In which case you can extract the paths from the name, and use those to match the audio file entries in the same path. I see there is a ZipFile:: sortEntriesByFilename(), which would ensure that your entries in the same folder will be grouped together. As I said previously, you should be able to use the juce::File object to easily extract paths, names, extensions, etc.

(I just saw in the ZipEntry doc: The name of the file, which may also include a partial pathname., so that answers that question)

2 Likes

Thank you!
Will try