Solved

PHP ZipArchive() creates corrupt zip file

Posted on 2012-03-22
5
635 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 108

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 108

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 108

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

757 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now