Zip: uncompress to memory block instead of file?


#1

I am wondering if it’s possible to uncompress zip file to a memory block instead of to the disk. It appears that all of the methods regarding decompression are meant for writing to disk, not to memory.

are we supposed to do something like this:

ZipFile someFile("somePath.zip");
auto stream = someFile.createStreamForEntry(0); //create input stream from loaded zipfile
MemoryOutputStream mos;
mos.writeFromInputStream( *stream, stream->getTotalLength() ); //create an output stream
MemoryBlock mb( mos.getData(), mos.getDataSize() ); //write output stream to memoryBlock
String plainText = mb.toString(); //write memoryBlock to string object

#2

you can use InputStream::readIntoMemoryBlock? or i’ve misunderstood your request?


#3

Well, i’m retrieving some compressed strings from a server and am trying to decompress them without writing them to disk first. that’s where i’m having the difficulties.


#4

Struggling to see what the problem is… ZipFile has a constructor that takes an input stream rather than a file, and you can read all the entries as streams, so there’s really no need to use files anywhere if you don’t want to.


#5

I had to switch over to using the GZIP classes as the php running on my host’s servers doesn’t have Zip, but it does have gzip/zlib.

perhaps you can tell me what i’m doing wrong here.

Here’s the example from php.net

<?php
$str = 'hello world'; 
$enc = zlib_encode($str, ZLIB_ENCODING_RAW);
echo bin2hex($enc);

which spits out: cb48cdc9c95728cf2fca490100

in xcode i’m going:

MemoryBlock mb;
mb.loadFromHexString("cb48cdc9c95728cf2fca490100");
MemoryInputStream mis(mb, false);

GZIPDecompressorInputStream gzip(mis);
MemoryBlock mb2(gzip.getTotalLength() );
gzip.read(mb2.getData(), gzip.getTotalLength());

String s2;
s2.createStringFromData(mb2.getData(), mb2.getSize());
DBG( s2 ); //should echo "hello world"

When I debug it, gzip.getTotalLength() return -1, so it never extracts it back to “hello world”. any ideas @kraken or @jules ?


#6

i got it somewhat working when I switched to ZLIB_ENCODING_GZIP in the php and used Format::gzipFormat in Juce. I also had to pass in the known length of the decompressed string.