Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php linux中的ZipArchive zip在windows中损坏_Php_Ziparchive - Fatal编程技术网

Php linux中的ZipArchive zip在windows中损坏

Php linux中的ZipArchive zip在windows中损坏,php,ziparchive,Php,Ziparchive,我在linux中使用ziparchive压缩文件,在windows中无法打开 默认的zip上传程序,我怀疑这是由addfile期间的文件路径引起的 e、 g 下面链接的建议提到我可以删除本地路径(因为windows无法理解),这将成为 $zip-addFile(‘/home/userName/public_html/gallery/license.txt’, ‘license.txt’); 但是我需要维护目录结构,我应该如何解决这个问题?可能首先使用addEmptyDir添加目标目录。请参

我在linux中使用ziparchive压缩文件,在windows中无法打开 默认的zip上传程序,我怀疑这是由addfile期间的文件路径引起的 e、 g

下面链接的建议提到我可以删除本地路径(因为windows无法理解),这将成为

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

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

    可能首先使用
    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';
    }
    
    解压缩test.zip未添加空目录:

    unzip test.zip
    Archive:  test.zip
      inflating: data/data.php     
    
    Archive:  testdata.zip
    creating: data/
      inflating: data/data.php 
    
    解压testdata.zip并添加一个空目录:

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

    在Drupal上下文中,creating:data/first

    的结构与您的类似,是否存在同样的问题。我通过使用我的uri设置
    内容配置
    文件名解决了这个问题

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

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