Gzdeflate () and a lot of data

I created a class for creating ZIP files in PHP. An alternative to ZipArchive provided that the server is not allowed. Something to use with these free servers.

It already works, builds ZIP structures with PHP and uses gzdeflate () to generate compressed data.

The problem is that gzdeflate () requires me to load the entire file into memory, and I want the class to work with a limit of up to 32 MB. Currently, it stores files larger than 16 MB without compression at all.

I assume that I need to compress the data in blocks, 16 MB to 16 MB, but I do not know how to combine the result of two gzdeflate ().

I tested it, and it seems that it requires some math for the last 16 bits, it seems buff->last16bits = (buff->last16bits & newblock->first16bits) | 0xfffe, it works, but not for all samples ...

Question: How to combine two DEFLATEd streams without unpacking it?

+3
source share
2 answers

To accomplish such tasks, PHP stream filters are used. stream_filter_appendcan be used when reading or writing to streams. for example

    $fp = fopen($path, 'r');
    stream_filter_append($fp, 'zlib.deflate', STREAM_FILTER_READ);

Now freadwill return you deflated data.

+1
source

This may or may not help. It looks like gzwrite will let you write files without loading them completely into memory. This example on the PHP manual page shows how you can compress a file using gzwrite and fopen.

http://us.php.net/manual/en/function.gzwrite.php

function gzcompressfile($source,$level=false){
    // $dest=$source.'.gz';
    $dest='php://stdout'; // This will stream the compressed data directly to the screen.
    $mode='wb'.$level;
    $error=false;
    if($fp_out=gzopen($dest,$mode)){
        if($fp_in=fopen($source,'rb')){
            while(!feof($fp_in))
                gzwrite($fp_out,fread($fp_in,1024*512));
            fclose($fp_in);
            }
          else $error=true;
        gzclose($fp_out);
        }
      else $error=true;
    if($error) return false;
      else return $dest;
}
0
source

Source: https://habr.com/ru/post/1717265/


All Articles