解压缩到变量

时间:2013-04-30 19:33:37

标签: php zip pclzip

我需要处理压缩文件的内容,但我无法更改托管程序的服务器上的权限。

这意味着我无法将zip文件下载到服务器,因此我需要将文件内容读入变量而不将其写入文件系统。

我可以获取此类变量的字符串内容并将解压缩的内容转换为新变量吗?

到目前为止,我已经研究过使用zip php扩展和pclzip库,但两者都需要使用实际文件。

这就是我想在伪代码中做的事情:

$contentsOfMyZipFile = ZipFileToString();
$myUnzippedContents = libUnzip($contentsOfMyZipFile);

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

查看this示例。

<?php
$open = zip_open($file);

if (is_numeric($open)) {
    echo "Zip Open Error #: $open";
} else {
while($zip = zip_read($open)) {
    zip_entry_open($zip);
    $text = zip_entry_read($zip , zip_entry_filesize($zip));
    zip_entry_close($zip);
}
print_r($text);
?>

答案 1 :(得分:0)

我在我的项目中使用它:

 function unzip_file( $data ) {
    //save in tmp zip data
    $zipname = "/tmp/file_xxx.zip";

    $handle = fopen($zipname, "w");
    fwrite($handle, $data);
    fclose($handle);

    //then open and read it.
    $open = zip_open($zipname);

    if (is_numeric($open)) {
        echo "Zip Open Error #: $open";
    } else {
        while ($zip = zip_read($open)) {
            zip_entry_open($zip);
            $text = zip_entry_read($zip, zip_entry_filesize($zip));
            zip_entry_close($zip);
        }
    }
    /*delete tmp file and return variable with data(in this case plaintext)*/
    unlink($zipname);
    return $text;
 }

我希望它可以帮到你。