Solved

PHP ZipArchive() creates corrupt zip file

Posted on 2012-03-22
5
688 Views
Last Modified: 2014-05-03
I have upgraded the server and since found that my zip is no longer working.

I currently am using: Apache 2.2.22 , PHP 5.3.10

Configure Command
'./configure' '--disable-fileinfo' '--disable-pdo' '--enable-bcmath' '--enable-calendar' '--enable-ftp' '--enable-gd-native-ttf' '--enable-libxml' '--enable-magic-quotes' '--enable-mbstring' '--enable-sockets' '--enable-zip' '--prefix=/usr' '--with-curl=/opt/curlssl/' '--with-curlwrappers' '--with-freetype-dir=/usr' '--with-gd' '--with-gettext' '--with-imap=/opt/php_with_imap_client/' '--with-imap-ssl=/usr' '--with-jpeg-dir=/usr' '--with-kerberos' '--with-libdir=lib64' '--with-libxml-dir=/opt/xml2/' '--with-mcrypt=/opt/libmcrypt/' '--with-mysql=/usr' '--with-mysql-sock=/var/lib/mysql/mysql.sock' '--with-openssl=/usr' '--with-openssl-dir=/usr' '--with-pcre-regex=/opt/pcre' '--with-pic' '--with-png-dir=/usr' '--with-xpm-dir=/usr' '--with-zlib' '--with-zlib-dir=/usr'


A simple script:

eg:
$zip = new ZipArchive();
$filename = "test112.zip";

if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
    exit("cannot open <$filename>\n");
}

$zip->addFile("Lighthouse.jpg","file1.jpg");
$zip->addFile("Penguins.jpg","file2.jpg");

echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";
$zip->close();

------------
(using all: Windows 7 zip, WinZip, WinRAR)

The zip file opens and the filesize looks ok in the two files, but when I open the images, it says they are corrupt.

I have tried both ZipArchive and also pclZipLib... both work with the same results.

Thanks
0
Comment
Question by:amakaram
  • 3
  • 2
5 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 37755004
I am also using PHP 5.3.10.  Here is my ZIP information:
Zip       enabled
Extension Version       $Id: php_zip.c 321634 2012-01-01 13:15:04Z felipe $
Zip version       1.9.1
Libzip version       0.9.0

If you will post the example files here I will be glad to try them on my server.
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37755047
Using this code, I successfully Zipped up a directory full of GIF images.  Windows 7 could not open the Zip Archive, but it worked fine with WinRAR.
<?php // RAY_zip_archive.php
error_reporting(E_ALL);
date_default_timezone_set('America/New_York');

// THE URL ARGUMENT IS THE NAME OF THE DIRECTORY TO ZIP
$dir = (!empty($_GET["dir"])) ? $_GET["dir"] : NULL;

// BUT FOR MY TESTS IT IS HARDCODED
$dir = 'RAY_aslimages';

if (!$dir) die("PLEASE PROVIDE dir= IN THE URL");

// A GOOD PATH ON MY SERVER
$path
= getcwd()
. DIRECTORY_SEPARATOR
. $dir
;
if (!is_dir($path)) die("FAIL: NOT VALID PATH $path");

// THIS IS THE LIST OF scandir() RESPONSES THAT WE DO NOT WANT
$unwanted
= array
( '.'
, '..'
, 'teste2.txt'
)
;

// INSTANTIATE THE OBJECT
$zip = new ZipArchive();

// CREATE A DATE-TIME ARCHIVE NAME
$archive
= date('Ymd\THis')
. '_archive_'
. $dir
. '.zip'
;

// TRY TO CREATE THE ARCHIVE
if ($zip->open($archive, ZIPARCHIVE::CREATE)!==TRUE) die("FAIL: ZIP->OPEN <$archive>");

// GET THE LIST OF FILES
if (!$files = scandir($path))   die("FAIL: scandir() $path");
foreach ($files as $file)
{
    // SKIP THE UNWANTED AND ADD THE OTHERS TO THE ARCHIVE
    if ( in_array($file, $unwanted) )                    continue;
    if ( !is_file($path . DIRECTORY_SEPARATOR . $file) ) continue;
    $zip->addFile($path . DIRECTORY_SEPARATOR . $file );
}

if (!$zip->close()) die("FAIL: ZIP->CLOSE");

$fs  = filesize($archive);
$fsn = number_format($fs);


// PREPARE A LINK
$link
= '<a target="_blank" href="'
. $archive
. '">'
. "DOWNLOAD $archive $fsn BYTES"
. '</a>'
;
// echo $link;

// SEND THE ZIP ARCHIVE
header("Content-Type: archive/zip");
header("Content-Disposition: attachment; filename=$dir".".zip");
header("Content-Length: $fs");
readfile($archive);

Open in new window

0
 

Author Comment

by:amakaram
ID: 37756851
Well Ray_Paseur, I have good news...

As I was getting the script and the images I was testing with, this EE uploader was failing them... upon closer look, the images were corrupt on the server... they originally were ftp'd from one server to the other and I guess I did not check the originals. (cant really believe that, but have no proof to the contrary)

Of course this was a small clip from a larger sript and I went back to the section of the script that moves the files in the first place... updated FTP_ASCII with FTI_BINARY and it worked.

...(ftp_fget($conn_id, $handle, $FilesMyrow["CurrentFileName"], FTP_BINARY, 0)...

Again, not sure why this would change on the new server... they have been ASCII for the last 2 years.

Let me know what you would like to do with points...

Thanks for your help!
Lighthouse.jpg
Penguins.jpg
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 37756941
Glad to hear that.  I tested with your image files and got the same result as before - WinRaR did a fine job.

Since it's not a ZIP related issue, I suggest you delete the question.  Not worried about the points - I have enough points to orbit Saturn.

All the best, ~Ray
0
 

Author Comment

by:amakaram
ID: 37757057
Well thank you.

Your responses were correct... but the problem *embarrassed smile* was mine!

Will delete shortly.
0

Featured Post

Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question