【问题标题】:Unzipping to a variable解压到变量
【发布时间】:2013-04-30 19:33:36
【问题描述】:

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

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

我可以抓取该变量的字符串内容并将解压缩的内容放入一个新变量中吗?

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

这是我想用伪代码做的:

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

有什么想法吗?

【问题讨论】:

  • 有趣的函数名:)
  • 你能写到输出吗?喜欢fopen('php://output','w');

标签: php zip pclzip


【解决方案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);
?>

【讨论】:

  • 谢谢,但我需要 $file... 我的问题是我无法在文件系统中写入。原始 zippend 文件位于不同的 SFTP 服务器上。我能够将它的内容放入一个变量中,但是,我无法写入文件系统......
  • 是的,您需要string $filename。我不确定这是否可能。看看here。您可能想向您的服务器管理员询问权限,或使用 php 以外的语言。
【解决方案2】:

我在我的项目中使用它:

 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;
 }

希望对你有帮助。

【讨论】:

  • 这并没有真正的帮助,因为 OP 指出他无法将数据写入文件系统
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-03
  • 1970-01-01
  • 2018-11-19
  • 2017-08-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多