需要PHP脚本解压缩并循环浏览压缩文件

需要PHP脚本解压缩并循环浏览压缩文件

本文介绍了需要PHP脚本解压缩并循环浏览压缩文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个相当简单的脚本来打开和解析gzip压缩的几个xml文件.我还需要对ZIP文件执行相同的基本操作.看起来应该很简单,但我一直无法在任何地方找到类似的代码.

I am using a fairly straight-forward script to open and parse several xml files that are gzipped. I also need to do the same basic operation with a ZIP file. It seems like it should be simple, but I haven't been able to find what looked like equivalent code anywhere.

这是我已经在做的简单版本:

Here is the simple version of what I am already doing:

$import_file = "source.gz";

$sfp = gzopen($import_file, "rb");  /////  OPEN GZIPPED data
while ($string = gzread($sfp, 4096)) {    //Loop through the data

    /// Parse Output And Do Stuff with $string
}
gzclose($sfp);

对于压缩文件,会做同样的事情吗?

What would do the same thing for a zipped file?

推荐答案

如果您的PHP 5> = 5.2.0,PECL zip> = 1.5.0,则可以使用ZipArchive库:

If you have PHP 5 >= 5.2.0, PECL zip >= 1.5.0 then you may use the ZipArchive libraries:

$zip = new ZipArchive;
if ($zip->open('source.zip') === TRUE)
{
     for($i = 0; $i < $zip->numFiles; $i++)
     {
        $fp = $zip->getStream($zip->getNameIndex($i));
        if(!$fp) exit("failed\n");
        while (!feof($fp)) {
            $contents = fread($fp, 8192);
            // do some stuff
        }
        fclose($fp);
     }
}
else
{
     echo 'Error reading zip-archive!';
}

这篇关于需要PHP脚本解压缩并循环浏览压缩文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 07:52