【问题标题】:ZipArchive zip in linux corrupt in windowsLinux中的ZipArchive zip在Windows中损坏
【发布时间】:2013-04-29 07:46:54
【问题描述】:

我在 linux 中使用 ziparchive 压缩文件,但无法在 windows 中打开 默认的 zip 上传器,我怀疑它是由 addfile 期间的文件路径引起的 例如

$zip-addFile(‘/home/userName/public_html/gallery/license.txt’, ‘gallery/license.txt’);

以下链接的建议提到我可以删除本地路径(因为这不能被 windows 理解),它变成了

$zip-addFile(‘/home/userName/public_html/gallery/license.txt’, ‘license.txt’);
  1. http://www.jacobbates.com/blog/2012/04/24/corrupt-zip-files-in-windows-from-phps-ziparchive/

  2. PHP ZipArchive Corrupt in Windows

但是我需要维护目录结构,我应该如何解决这个问题?

【问题讨论】:

  • 为什么不使用 tar。我从来没有遇到过用它创建的档案的问题。 tar -czf zippedFiles.zip /path/to/files 应该可以工作。
  • @Zefiryn 有时需要 zip,可能是出于压缩目的。

标签: php ziparchive


【解决方案1】:

也许先用addEmptyDir添加目标目录,看下面的代码创建不同的文件:

<?php
error_reporting(E_ALL);
ini_set('display_errors','on');
$zip = new ZipArchive;
if ($zip->open('tmp/test.zip',ZipArchive::CREATE) === TRUE) {
    $zip->addFile('data.php', 'data/data.php');
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
$zip = new ZipArchive;
if ($zip->open('tmp/testdata.zip',ZipArchive::CREATE) === TRUE) {
    if($zip->addEmptyDir('data')) {
        echo 'Created a new root directory';
    } else {
        echo 'Could not create the directory';
    }
    $zip->addFile('data.php', 'data/data.php');
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}

文件的文件大小不同:

-rw-r--r--  1 www-data www-data  633 Apr 29 12:10 testdata.zip
-rw-r--r--  1 www-data www-data  545 Apr 29 12:10 test.zip

解压 test.zip 没有添加空目录:

unzip test.zip
Archive:  test.zip
  inflating: data/data.php     

解压 testdata.zip 并添加一个空目录:

Archive:  testdata.zip
creating: data/
  inflating: data/data.php 

首先创建:数据/

【讨论】:

    【解决方案2】:

    在 Drupal 上下文中使用与您类似的结构时遇到了同样的问题。我通过使用我的 uri 设置 Content-disposition 文件名解决了这个问题

    file_transfer($archive_uri, array(
        'Content-Disposition' => 'attachment; filename="' . $archive_uri . '"',
        'Content-Length'      => filesize($archive_uri))
      );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-08
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      • 2021-12-07
      • 2015-07-25
      • 2018-02-13
      相关资源
      最近更新 更多