Mroz
Mroz

Reputation: 568

PHP zip file extraction - wrong size of entry

I am using online invoice system, and their API allows me to download PDF invoice in form of string which is zipped pdf encoded in base64. I decode this string with base64_decode(), then I save it as file: file_put_contents('temp/soubor.zip', $data);

Now if I want to open this zip file in windows it is ok. But I want to extract it via php and if I call:

$zip = new ZipArchive;
if ($zip->open('temp/soubor.zip') === TRUE) {
    print_r($zip->statIndex(0));
    $zip->close();
} 

I get

Array ( [name] => zipEntryName [index] => 0 [crc] => 1906707552 [size] => -1 [mtime] => 1358774308 [comp_size] => -1 [comp_method] => 8 )

Everything is fine except size - which is -1, and that's a huge problem because it won't extract anything.

And now interesting thing: If I open the zip file in winRar, choose repair archive, and open repaired zip file in my script, I get correct size and file can be correctly extracted. btw archived file is just 260kB.

Upvotes: 2

Views: 808

Answers (2)

Nickolay Olshevsky
Nickolay Olshevsky

Reputation: 14160

Looks like malformed archive, probably it is build 'on fly' from data stream with unknown size, and not all ZIP packets are correctly written to output (or, stream is not flushed so central directory is not written). You can check detailed zip file information by running zipinfo: http://www.info-zip.org/mans/zipinfo.html

Upvotes: 1

Tommaso Belluzzo
Tommaso Belluzzo

Reputation: 23695

Either you are saving your zip in a bad way of the received buffer contains malformed data. Try this yo save your file instead of file_put_contents():

<?PHP

    $handle = fopen('temp/soubor.zip', 'w');
    fwrite($handle, base64_decode($zipdatabase64));
    fclose($handle);

?>

Upvotes: 0

Related Questions