kjdev / php-ext-zstd

Zstd Extension for PHP
MIT License
201 stars 27 forks source link

Decompression from File Handler #41

Open proconsule opened 2 years ago

proconsule commented 2 years ago

I found out that is impossible to decompress form a file handler reading chunks.

I have a very big file that have compressed data starting from an offset. i have a file handler and seek to the start of compressed data it will be very very useful for me (and for many others i think) a function that can get the file handler and read chunks of data outputing the uncompressed data

like

$this->fh = fopen("FILE",'r'); fseek($this->fh,$this->offset); //the start of compressed data

$zstd_object = zstd_uncompress_handler($this->fh);

while(EOF or some condition){ $outdata = $zstd_object->uncompress($chunksize); //just an idea of object function // Data here can be printed or anything else (for a on the fly download and so on) }

This can be done and you think can be usefull? (if so i'll use for sure on my project)

kjdev commented 2 years ago

Since we are implementing the stream function, we can do the following.

$file = __DIR__ . '/data';

// compress
copy(PHP_BINARY, 'compress.zstd://' . $file);

// decompress
$out = '';
$fp = fopen('compress.zstd://' . $file, 'r');
if ($fp) {
  while($in = fread($fp, 4096)) {
    $out .= $in;
  }
  fclose($fp);
}
tpetry commented 1 year ago

But the stream handler does not help when e.g. trying to decompress a very large zstd compressed document with a custom dictionary.