简体   繁体   English

gzdeflate()和大量数据

[英]gzdeflate() and large amount of data

I've been building a class to create ZIP files in PHP. 我一直在构建一个用PHP创建ZIP文件的类。 An alternative to ZipArchive assuming it is not allowed in the server. ZipArchive的替代方案,假设服务器中不允许这样做。 Something to use with those free servers. 与这些免费服务器一起使用的东西。

It is already sort of working, build the ZIP structures with PHP, and using gzdeflate() to generate the compressed data. 它已经开始工作,使用PHP构建ZIP结构,并使用gzdeflate()生成压缩数据。

The problem is, gzdeflate() requires me to load the whole file in the memory, and I want the class to work limitated to 32MB of memory. 问题是,gzdeflate()要求我将整个文件加载到内存中,并且我希望该类的工作限制为32MB内存。 Currently it is storing files bigger than 16MB with no compression at all. 目前它存储的文件大于16MB而根本没有压缩。

I imagine I should make it compress data in blocks, 16MB by 16MB, but I don't know how to concatenate the result of two gzdeflate(). 我想我应该用块压缩数据,16MB乘16MB,但我不知道如何连接两个gzdeflate()的结果。

I've been testing it and it seems like it requires some math in the last 16-bits, sort of buff->last16bits = (buff->last16bits & newblock->first16bits) | 0xfffe 我一直在测试它,看起来它需要在最后的16位中使用一些数学,有点像buff->last16bits = (buff->last16bits & newblock->first16bits) | 0xfffe buff->last16bits = (buff->last16bits & newblock->first16bits) | 0xfffe , it works, but not for all samples... buff->last16bits = (buff->last16bits & newblock->first16bits) | 0xfffe ,它有效,但不适用于所有样品......

Question: How to concatenate two DEFLATEd streams without decompressing it? 问题:如何在不解压缩的情况下连接两个DEFLATEd流?

PHP stream filters are used to perform such tasks. PHP流过滤器用于执行此类任务。 stream_filter_append can be used while reading from or writing to streams. stream_filter_append可以在读取或写入流时使用。 For example 例如

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

Now fread will return you deflated data. 现在fread将返回你泄密的数据。

This may or may not help. 这可能有所帮助,也可能无效。 It looks like gzwrite will allow you to write files without having them completely loaded in memory. 它看起来像gzwrite将允许您编写文件而不必将它们完全加载到内存中。 This example from the PHP Manual page shows how you can compress a file using gzwrite and fopen. PHP手册页面中的这个示例显示了如何使用gzwrite和fopen压缩文件。

http://us.php.net/manual/en/function.gzwrite.php 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM