How to save two binaries to a single file

When saving my app to file, I’m doing something like this:

void getStateInformation(juce::MemoryBlock& destData) override
{
    auto vtToSave = getRootValueTree().createCopy();

    auto memoryStream = juce::MemoryOutputStream(destData, false);

    vtToSave.writeToStream(memoryStream);
}

This works fine, but now I’m in a situation where I need to save some more (non jucy) binary data to the same file. How can I do that? The only things I can think of are hacks like copying the two together and then encoding the first few bytes as a std::size_t to indicate where the separation takes places. Is there a better way?

What is the consumer of the restored information? ie. can you add the other info TO your valuetree before writing it out? and then remove it from the VT when loading, and before handing the VT to the rest of the code?

I don’t think it’s a “hack” to add more information to an otherwise unformatted memory chunk. It’s designed as a simple block of memory at the native SDK level (e.g., VST3). The fact that this code only writes your value tree to the memory block is simply a design choice for simplicity of saving the parameters. We store an XML data structure in that block, which contains a lot of stuff besides the parameters. The value true is just written/read in one of the XML nodes. But using a length specifier and reading that much data into a local buffer for storing/restoring the parameters, and then storing other data either before or after it in the chunk seems like a perfectly valid approach to me.

Use a ZipFile::Builder and store the two pieces of data as separate files inside a zip archive?

2 Likes

That’s the one! I knew there must be a tool for something like this. Thanks for the help folks.

Actually, I just realized that var can hold a binary, so appending the binary to the ValueTree like @cpr2323 suggested is probably the simplest thing here. But it’s still good to know that ZipFile exists. I’m constantly amazed by how many different, useful classes there are in JUCE.

I generally save my VT’s as XML, so I was thinking that you would bas64 encode the data, and store it as a string. But, since you are saving the VT as binary, it may very well work that you don’t have to encode it.