问题
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’);
http://www.jacobbates.com/blog/2012/04/24/corrupt-zip-files-in-windows-from-phps-ziparchive/
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