ZipArchive zip in linux corrupt in windows

房东的猫 提交于 2019-12-25 07:07:34

问题


I use ziparchive to zip my files in linux, and can't open in windows default zip uploader, I suspect it is caused by the file path during addfile e.g.

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

Suggestion from links below mention that I coudl remove the local path(as this can't be understood by windows), which becomes becomes

$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

But I need to maintain the directory structure, how should I address this problem?


回答1:


maybe first add the target directory with addEmptyDir see the code below this create different files:

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

The files got different file sizes:

-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

unzip test.zip no empty dir added:

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

unzip testdata.zip with an empty dir added:

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

does creating: data/ first




回答2:


Had the same problem with a structure similar to yours on a Drupal context. I solved this by setting the Content-disposition filename with my uri

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


来源:https://stackoverflow.com/questions/16273603/ziparchive-zip-in-linux-corrupt-in-windows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!