Need help with Gzip compression decompression


I am trying to export a ValueTree as Xml and compressed to a file and the import back decompressed. I get a crash at the gzipDecompressorInputStream stage.

this is the part which does the compression and export;

        MemoryOutputStream SeqBinData;
        ScopedPointer <XmlElement> SeqDataXml (SeqDatabase.createXml()); //SeqDatabase is the ValueTree object
        GZIPCompressorOutputStream gzipOutputStream(&SeqBinData,5,0);
        thefile.replaceWithData (SeqBinData.getData(), SeqBinData.getDataSize()); //theFile is a File object with defined path.

and the import part is…

File ValueTreeFile (filename);
MemoryOutputStream uncompressed;   
GZIPDecompressorInputStream gzInput (ValueTreeFile.createInputStream(), true, GZIPDecompressorInputStream::gzipFormat);
uncompressed.writeFromInputStream (gzInput, -1);    
ScopedPointer<XmlElement> xmlState(XmlDocument::parse(uncompressed.toString()));
ValueTree newValueTree = ValueTree::fromXml(*xmlState);

Apparently I am doing a mistake, so any guiding is much appreciated.


What error are you getting?

Is the ValueTree correctly serialised to XML? Dump it to a String and look at it.

What happens if you decompress the file using a different tool, is the XML correct there?

Is the XML successfully decompressed? Dump it to a String and look at it.

As an aside: Do you really need compressed XML? ValueTree::writeToStream() would be quicker (but not human-readable).


Also if you indent your code by 4 spaces on the forum (or surround with triple backticks) you’ll get syntax highlighting, which makes it much easier to read.


Isn’t there any way to get the forum software to indicate this to users so you guys don’t have to repeatedly type this? :slight_smile:



  • There is a crash at the line;

GZIPDecompressorInputStream gzInput (ValueTreeFile.createInputStream(), true, GZIPDecompressorInputStream::gzipFormat);

  • The ValueTree composition has no flaws, I can do direct ValueTree:writeToStream and binary formant streaming.

  • I will try to decompress the compressed file to see, any tool you do recommend for OSX?

  • I want to use compression because these files can get easily severel MBytes, and when compressed they are tiny in size.

So you don’t see any apparent logical error on the code itself ?


    thefile.replaceWithData (SeqBinData.getData(), SeqBinData.getDataSize()); //theFile is a File object with defined path.

You’re calling flush after writing the data, so it’s not going to be written in it’s complete form.

I had only skimmed your code - I was just trying to give you some suggestions for how you could diagnose the problem yourself.


Discarding the flush didn’t make any changes. Ok, I will investigate more , apparently there is no quick deal with this. thanks anyway.


No, don’t discard the flush! You need to flush before writing the data elsewhere.

JUCE also uses zlib for compression, so you should select GZIPDecompressorInputStream::zlibFormat as the format type. Or better yet, just omit specifying anything! Those format types should be used when decompressing data created by other programs.

GZIPDecompressorInputStream gzInput (ValueTreeFile.createInputStream(), true);