ZipStream-PHP: Resulting Zip can't be extracted in Mac OSX Archive Utility and Windows WinRAR
I’m using Zipstream to create a streamed zip download to browser in a Symfony 2.8 + PHP 7 on Ubuntu web application. Files are streamed from AWS s3 bucket via aws-sdk-php which I used to create an S3Client with valid credentials against which I register the stream wrapper (s3Client->registerStreamWrapper()😉.
In web interface, user clicks “Download” button which initiates the above and a zip downloads via browser to user’s local machine. However, the zip cannot be opened by:
- Archive Utility (native app) on Mac OSX (El Capitan) with error "Unable to extract ‘test.zip’. Error 2 No such file or directory.)
- WinRAR on Windows with error “Archive is corrupted”.
I can open the file via 7-zip on Windows and TheUnarchiver on Mac, but this is not a suitable solution as users can’t be expected to guess or know to download a specific third party archive application to make a downloaded zip file usable.
I have seen a previous issue related to this from a few years ago requiring other third party bundles be updated and a related SO post (https://stackoverflow.com/questions/5573211/dynamically-created-zip-files-by-zipstream-in-php-wont-open-in-osx) dealing with the same issue back in 2011. I have updated my bundles and attempted the suggestions in that SO post to fix the issue without success.
Sample Code:
//$s3Client instantiated with valid credentials and passed to function
$this->s3Client = $s3Client;
$this->s3Client->registerStreamWrapper();
$opt = array(
'comment' => 'test zip file.',
'content_type' => 'application/octet-stream'
);
$zip = new ZipStream\ZipStream($filename, $opt);
/*
simple case with simple test file.
*/
$keys = array(
"zipsimpletestfolder/file1.txt"
);
foreach ($keys as $key) {
// Get the file name in S3 key so we can save it to the zip file
// using the same name.
$fileName = basename($key);
$bucket = 'mg-test';
$s3path = "s3://" . $bucket . "/" . $key;
if ($streamRead = fopen($s3path, 'r')) {
$zip->addFileFromStream($fileName, $streamRead);
} else {
die('Could not open stream for reading');
}
}
In ZipStream class I see the following version settings for making and extract:
...
// build file header
$fields = array( // (from V.A of APPNOTE.TXT)
array(
'V',
0x04034b50
), // local file header signature
array(
'v',
0x000A //(6 << 8) + 3)
), // version needed to extract
//FIXED as mentioned in http://linlog.skepticats.com/entries/2012/02/Streaming_ZIP_files_in_PHP.php
//and http://stackoverflow.com/questions/5573211/dynamically-created-zip-files-by-zipstream-in-php-wont-open-in-osx
...
Can you please assist with solving this issue?
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 16 (16 by maintainers)
@maennchen yeah, I’m happy to contribute that.
Doh! The response is sending html response in the zip stream. Yeah, I think its how i’m handling the response in symfony with ZipStream also firing off the download. Yeah verified issue is solved this side. I’ll sort this out and get back in my box when it works.
Thanks for the assistance & great response (excuse the pun).
@danielforer To correctly quote code, you have to put 3 ticks before and after.