PHP: zziplib -> unzip leaves corrupt files?`

  • frizzo / 216 / Tues, 05 May 2009 17:39:00 GMT / Comments (5)
  • Hello,

    I'm using the zziplib to unpack a large (10M) file that contains about 200 pdf's.

    When I run the script, it unzips everything apparently without a problem. But then when I go and view one of the pdf's it gives me an error that the file is corrupt; acrobat cannot load it.

    I have verified that the pdf and the zip file are indeed OK -- by unzipping them into another directory from the command line with unzip.

    The code I used comes pretty much straight from the php manual ... Does anyone have some insight on this, or can someone direct me to another option to work with zip files??

    Thanks
    Frizzo

    $zipfile = "batch1.zip";
    $zip = zip_open($zipfile);
    while ($zip_entry = zip_read($zip)) {
    $m_filename = zip_entry_name($zip_entry);
    if (zip_entry_open($zip, $zip_entry, "r")) {
    $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
    zip_entry_close($zip_entry);
    save_zip("/usr2/home/test/" . $m_filename, $buf);
    }
    }
    zip_close($zip);

    function save_zip($filename, $data){
    if (!$fp = fopen ($filename, "w")){
    die("could not open file" . getcwd());
    }
    fputs($fp, $data);
    fclose($fp);
    }
  • Keywords:

    zziplib, unzip, leaves, corrupt, files, `, php

  • http://programming.itags.org/php/355226/«« Last Thread - Next Thread »»
    1. Are you on a Windoze or unix host?

      I assume you are on a unix host so I suggest you use one of the exec() functions to execute unzip instead of doing it yourself.

      If you are on a Windoze host use "wb" to write the file.

      HalfaBee

      halfabee | Thurs, 15 Nov 2007 23:32:00 GMT |

    2. Thanks for the reply

      Interesting... I hadn't thought of that --

      I'm on a unix box --

      The only thing is, the way the script needs to work is that it inserts each pdf into a blob and does not use the filesystem at all -- the zip file is also uploaded thru a form. I've been doing it like mentioned in above code for testing porposes to try and pinpoint the problem.

      But I suppose I could unzip it into a temp dir with exec() and then go through each file, insert it to the db, and then delete.

      One other thing::

      On one of the pdfs in the zip, the filesize of the uncorrupted version is 43787. The filesize of the corrupted one that zziplib (or maybe my save_zip function?) produces is 49152. When I add ini_set("magic_quotes_runtime", 0), the filesize goes down to 45056, but it is still corrupted.

      What is the best and proper way to ensure that zziplib is extracting and then saving the files w/o adding any additional stuff?

      If I add a stripslashes() onto the save_zip around $data, the server spins forever and no errors are thrown. This is weird!!!

      frizzo | Thurs, 15 Nov 2007 23:33:00 GMT |

    3. Quite frankly I wouldn't store the pdf's in the database, just a link to the file system.
      It only adds extra overload to the DB if you have to fetch the info.
      It is easier just to do a readfile() to send the pdf.

      HalfaBee

      halfabee | Thurs, 15 Nov 2007 23:34:00 GMT |

    4. Tell me about it!! I totally agree with you.

      But unfortunately that's the way the system has been set up.

      So basically I'd really like to know what is causing these files to get corrupted.

      Any ideas??

      frizzo | Thurs, 15 Nov 2007 23:35:00 GMT |

    5. Unfortuately my host doesn't have the zip functions compiled.
      I will have a look tonight and see if my PC version of php has it.

      Do a phpinfo() and see what configuration things are set.
      You may need a custom php.ini to stop things happening.

      HalfaBee

      halfabee | Thurs, 15 Nov 2007 23:36:00 GMT |