Welcome Guest, Not a member yet? Register   Sign In
Unzipping a file created by Zip library
#1

[eluser]taewoo[/eluser]
Hi all.
I've written app that generates a zip file of json file (the file is HUGE, thus to save time d/l'ing) that another server downloads, unzip, and reads.

The server that downloads the zip has this code:

Code:
function saveRemoteFile($remote_file, $local_file)
{
    $handle = @fopen($remote_file, "r");
    if(!$handle)
    {
        throw new Exception("Can't retrieve $remote_file");
    }
    
    $out_handle = fopen($local_file, "w+");
    if(!$out_handle)
    {
        throw new Exception("Can't open $local_file for write");
    }
    
    while (!feof($handle))
    {
        $binary_line=fread($handle, 8192);
        fwrite($out_handle, $binary_line, 8192);
    }
    
    fclose($handle);
    fclose($out_handle);
}



This is the helper function i've written... and this doesn't seem to work in a sense that it's padding the downloaded zip file with erroneous characters. If i run "unzip data.zip", the command line error I get is

Code:
Archive:  data.zip
warning [data.zip]:  265 extra bytes at beginning or within zipfile
  (attempting to process anyway)
  inflating: data.txt

If I try to unzip this file with PHP:
Code:
$zip = zip_open("data.zip");
if (is_resource($zip)) {
  echo "OK";
  while ($zip_entry = zip_read($zip)) {
    echo $zip_entry."\n";
    if (zip_entry_open($zip, $zip_entry, "r")) {
      $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
      echo $buf;
      zip_entry_close($zip_entry);
    }
  }
  zip_close($zip);
}

.... the WHILE loop does not execute.

If I do a "wget" to retrieve the file, i don't have any problems, which concludes me to think that it's fwrite that's causing the problem.

Any thoughts on this?
#2

[eluser]taewoo[/eluser]
I tried it with CURL. This doesn't work either

Code:
$out_handle = fopen($local_file, "wb");
    
    $ch = curl_init();
//curl_setopt ($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FILE, $out_handle);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$page = curl_exec($ch);
if (!$page) {
    echo "<br />cURL error number:" .curl_errno($ch);
    echo "<br />cURL error:" . curl_error($ch);
    exit;
}
curl_close($ch);

but the error is slightly different
Code:
Archive:  data.zip
  End-of-central-directory signature not found.  Either this file is not
  a zipfile, or it constitutes one disk of a multi-part archive.  In the
  latter case the central directory and zipfile comment will be found on
  the last disk(s) of this archive.
note:  data.zip may be a plain executable, not an archive
unzip:  cannot find zipfile directory in one of data.zip or
        data.zip.zip, and cannot find data.zip.ZIP, period.
#3

[eluser]taewoo[/eluser]
anyone?




Theme © iAndrew 2016 - Forum software by © MyBB