2
votes

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.

2
If you have to repair it to open it. It sounds like the original archive is corrupt or using some kind of unusual or incomplete. Are you sure this is zip and not something like gzip? - datasage
I am not sure if it is realy corrupted. WinRar is very robust so it'll open even slightly corrupted files. It seems data are ok, only php can't read size of compressed file correctly. And the file is ZIP64 - Mroz
It will also detect an open gzip files. Can you read the first 4 bytes of the file and post it? it will help me determine the exact format of the file (Magic bytes). - datasage
50 4B 03 04 (15 characters long comment) - Mroz
50 4B 03 04 are the magic bytes for a zip file (not gzip). If you are working on a *nix environment, you could pass this through command line and see if it gets extracted. But without digging into the file its hard to know why its not working. - datasage

2 Answers

1
votes

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

0
votes

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);

?>