diff options
-rw-r--r-- | inc/ZipLib.class.php | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/inc/ZipLib.class.php b/inc/ZipLib.class.php index 3b39ad1b6..84164dd4a 100644 --- a/inc/ZipLib.class.php +++ b/inc/ZipLib.class.php @@ -349,5 +349,91 @@ class ZipLib return ap_mkdir($d); } //--CS end + + + function ExtractStr($zn, $name) { + $ok = 0; + $zip = @fopen($zn,'rb'); + if(!$zip) return(NULL); + $cdir = $this->ReadCentralDir($zip,$zn); + $pos_entry = $cdir['offset']; + + for ($i=0; $i<$cdir['entries']; $i++) + { + @fseek($zip, $pos_entry); + $header = $this->ReadCentralFileHeaders($zip); + $header['index'] = $i; + $pos_entry = ftell($zip); + @rewind($zip); + fseek($zip, $header['offset']); + if ($name == $header['stored_filename'] || $name == $header['filename']) { + $str = $this->ExtractStrFile($header, $zip); + fclose($zip); + return $str; + } + + } + fclose($zip); + return null; + } + + function ExtractStrFile($header,$zip) { + $hdr = $this->readfileheader($zip); + $binary_data = ''; + if (!($header['external']==0x41FF0010) && !($header['external']==16)) + { + if ($header['compression']==0) + { + while ($size != 0) + { + $read_size = ($size < 2048 ? $size : 2048); + $buffer = fread($zip, $read_size); + $binary_data .= pack('a'.$read_size, $buffer); + $size -= $read_size; + } + return $binary_data; + } else { + $size = $header['compressed_size']; + if ($size == 0) { + return ''; + } + //Just in case + if ($size > ($this->_ret_bytes(ini_get('memory_limit'))/2)) { + die("Compressed file is to huge to be uncompress in memory."); + } + while ($size != 0) + { + $read_size = ($size < 2048 ? $size : 2048); + $buffer = fread($zip, $read_size); + $binary_data .= pack('a'.$read_size, $buffer); + $size -= $read_size; + } + $str = gzinflate($binary_data, $header['size']); + if ($header['crc'] == crc32($str)) { + return $str; + } else { + die("Crc Error"); + } + } + } + return NULL; + } + + function _ret_bytes($val) { + $val = trim($val); + $last = $val{strlen($val)-1}; + switch($last) { + case 'k': + case 'K': + return (int) $val * 1024; + break; + case 'm': + case 'M': + return (int) $val * 1048576; + break; + default: + return $val; + } + } } |