zip_open

(PHP 4 >= 4.1.0, PECL)

zip_open -- Open a ZIP file archive

Описание

resource zip_open ( string filename )

Opens a new zip archive for reading.

Список параметров

filename

The file name of the ZIP archive to open.

Возвращаемые значения

Returns a resource handle for later use with zip_read() and zip_close() or returns FALSE if filename does not exist.

Смотрите также

zip_read()
zip_close()



zip_open
Michael
08-Jun-2006 01:55
I tried the replacement hack above on Windows2k, here
are some fixes needed:
replace:
  return "'".str_replace("'", "'\''", $s)."'";
with:
  return '"'.str_replace("'", "'\''", $s).'"';

replace:
if($line[0]=='-') { $ok=!$ok; continue; }
with:
if($line[2]=='-') { $ok=!$ok; continue; }

replace:
 $contents[] = Array('name' => $fn, 'length' => $length);
with:
   array_push($contents, Array('name' => $fn, 'length' => $length));
ponsho
14-Dec-2005 12:44
For bisqwit at iki dot fi solution of alternative functions there's just one problem when trying to read the file thats because some bug in fread when handling from popen so it just load 8192 bytes here's the function corrected.

<?php

  
function zip_entry_read(&$res, $nbytes)
   {
     while (
$s = fgets($res['fp'],1024)) {
    
$data  .= $s;
     }
     return
$data;
   }
?>
barbarinasv at interfree dot it
05-Oct-2005 01:02
Function zip_entry_read() written by "bisqwit at iki dot fi" has to be modified to read entire files:

<?php
function zip_entry_read(&$res, $nbytes) {
   while (!
feof($res['fp'])) {
      
$contents .= fread($res['fp'], $nbytes);
   }
   return
$contents;
}
?>
robert at cotran dot ca
21-Sep-2005 12:19
The zip_entry_read above is wrong.  Since the file was opened with popen, you have to read the file in chunks, so zip_entry_read should read:

function zip_entry_read(&$res, $nbytes)
{
   $contents = '';
   while (!feof($res['fp'])) {
       $contents .= fread($res['fp'], 8192);
   }
   return $contents;
}

Otherwise, it was a very useful library.  Thanks!
bisqwit at iki dot fi
02-Sep-2005 04:08
If your PHP installation does not have the zip_open function, and you can't install it for whatever reason, you can use these functions instead, if the server has access to the "unzip" utility (most Linux systems do).
So far I have tested these only in Fedora Core 3.
Use at your own risk.

<?php

function ShellFix($s)
{
  return
"'".str_replace("'", "'\''", $s)."'";
}

function
zip_open($s)
{
 
$fp = @fopen($s, 'rb');
  if(!
$fp) return false;
 
 
$lines = Array();
 
$cmd = 'unzip -v '.shellfix($s);
 
exec($cmd, $lines);
 
 
$contents = Array();
 
$ok=false;
  foreach(
$lines as $line
  {
   if(
$line[0]=='-') { $ok=!$ok; continue; }
   if(!
$ok) continue;
  
  
$length = (int)$line;
  
$fn = trim(substr($line,58));
  
  
$contents[] = Array('name' => $fn, 'length' => $length);
  }
 
  return
   Array(
'fp'      => $fp
        
'name'    => $s,
        
'contents' => $contents,
        
'pointer'  => -1);
}                         
function
zip_read(&$fp)
{
  if(!
$fp) return false;
 
 
$next = $fp['pointer'] + 1;
  if(
$next >= count($fp['contents'])) return false;
 
 
$fp['pointer'] = $next;
  return
$fp['contents'][$next];
}
function
zip_entry_name(&$res)
{
  if(!
$res) return false;
  return
$res['name'];
}                         
function
zip_entry_filesize(&$res)
{
  if(!
$res) return false;
  return
$res['length'];
}
function
zip_entry_open(&$fp, &$res)
{
  if(!
$res) return false;

 
$cmd = 'unzip -p '.shellfix($fp['name']).' '.shellfix($res['name']);
 
 
$res['fp'] = popen($cmd, 'r');
  return !!
$res['fp']; 
}
function
zip_entry_read(&$res, $nbytes)
{
  return
fread($res['fp'], $nbytes);
}
function
zip_entry_close(&$res)
{
 
fclose($res['fp']);
  unset(
$res['fp']);
}
function
zip_close(&$fp)
{
 
fclose($fp['fp']);
}
?>
ajbaas at cs dot uu dot nl
13-Oct-2004 12:08
note: if you are using PHP in module mode, the open_zip function will need an absolute path to the file, because it is not aware of it's whereabouts. this is for win32 at least.

<zip_entry_readzip_read>
 Last updated: Tue, 15 Nov 2005