0
votes

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?

2
Why not using tar. I never had problems with archives created with it. tar -czf zippedFiles.zip /path/to/files should work.Zefiryn
@Zefiryn sometimes zip is required, maybe for compression purpose.user1487718

2 Answers

0
votes

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

0
votes

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