Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Corrupt image when extract from zip

Tags:

php

nginx

I trying download a zip file using curl from one virtual host to another, in a same server. Zip file contains *.php and *.jpg files.

The problem is: sometimes JPG files get corrupt, like this:

jpg file

Here is my code :

$out = fopen(ABSPATH.'/templates/default.zip','w+'); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_FILE, $out); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_URL, 'http://share.example.com/templates/default.zip'); 
curl_exec($ch); 
curl_close($ch); 


$zip = new ZipArchive;
if ($zip->open(ABSPATH.'/templates/default.zip') === TRUE) 
{
    if($zip->extractTo(ABSPATH.'/templates'))
    {
        echo 'OK';
    }

    $zip->close();
} 

//$zip->close();

I don't understand what happen to my jpg. I also tried using pclzip.lib.php, but no luck. How to solve this problem ?

Thanks in advance

like image 916
Zul Avatar asked Jun 03 '12 10:06

Zul


People also ask

Can zipping a file cause corruption?

ZIP files are a convenient and secure way by which you can transmit large files over the internet. However, these files can get corrupted during the transfer process. In order to prevent this, you should invest in a good data recovery program for ZIP files.

Do Zip files lose image quality?

When you extract the files in a Zip file created by WinZip, the result will be exact, byte for byte duplicates of the original files. There is no loss of fidelity, no loss of image quality, and no change in data associated with zipping or unzipping.

Can you restore corrupt Zip files?

Step 1 Launch File Repair Tool and select the "Repair ZIP File" option start to repairing the process. Step 2 Search corrupt ZIP file in local drive and select the specific corrupt ZIP files. Step 3 All corrupt ZIP files list in the result section and click the "Repair" button.


1 Answers

Have you tried downloading the file via curl and unzipping it normally (i.e. without php)? To figure out whether the download causes the problem or the unzip.

You might also try to replace one of both parts using shell_exec (wget instead of curl, unzip instead of ZipArchive). I mean just for debugging, not for production maybe.

like image 114
Christopher K. Avatar answered Oct 12 '22 22:10

Christopher K.