Gzipped file to XML & back to gzip - help needed

I’ve spent most of today trying to work out how to open a gzipped file and load to an XMLDocument, any examples I’ve seen don’t seem to be relevant anymore as createInputStream no longer seems to be available for Files. It says to use FileInputStream instead in the docs, however the GZIPDecompressorInputStream won’t accept FileInputStream, it needs to be InputStream which i really cant get my head around…

I’m fine working with XML in juce, I just need to decompress on load and compress on save, I’m surprised it seems to be so complicated, I thought it would just be something like a member function of File.

I’m going to look at using an external library now, but I’d really prefer keep it within juce.

The FileInputStream IS an InputStream. You can just pass it to GZIPDecompressorInputStream::GZIPDecompressorInputStream()

I’ve made some progress, unzip is working but only works using gzip and not zlib or deflate, selecting the latter 2 prevents my app from compiling. At least it’s actually unzipping the file though.

juce::String xpnThread::unzipToString(juce::File fn)
{  
    juce::FileInputStream compressedInput(fn);
    if (compressedInput.openedOk())
    {
        juce::GZIPDecompressorInputStream unzipper(&compressedInput,false,juce::GZIPDecompressorInputStream::Format::gzipFormat,-1);
        return unzipper.readEntireStreamAsString();
    }
    return "";
}

as for save to zip, that doesn’t work at all, the zip is corrupt and can’t be opened. As for methods zlib is the only one works at all, gzip and deflate just hang the app & the GUI never appears when calling flush.

void xpnThread::zipSavedFile(juce::File destinationFn, juce::String ext)
{
    juce::FileInputStream uncompressedInput(destinationFn);
    if (uncompressedInput.openedOk())
    {
        juce::FileOutputStream compressedOutput(destinationFn.withFileExtension(ext));
        juce::GZIPCompressorOutputStream zipped(&compressedOutput, -1, false, juce::GZIPDecompressorInputStream::Format::zlibFormat);
        zipped.writeFromInputStream(uncompressedInput, -1);
        zipped.flush();
        compressedOutput.flush();
    }
}

I’ve spent more or less 2 days on trying to get this working, I’m losing the will to live.

I’ve just realized what the issue is, I was expecting this to behave like gzip in that it adds the file to a compressed archive, but it doesn’t, it just compresses the data… It’s zipfile I needed…

void xpnThread::zipSavedFile(juce::File destinationFn, juce::String ext)
{
    juce::ZipFile::Builder zf;
    zf.addFile(destinationFn, 5);
    juce::FileOutputStream compressedOutput(destinationFn.withFileExtension(ext));
    zf.writeToStream(compressedOutput, NULL);
}

so simple, why didn’t I notice this yesterday :man_facepalming:

I can now move forward.

1 Like