openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit b387cbeb1cb8eaf92a894a2bcdbc8f909cf0dd47
parent 621d41e980f010710764b285188168b09e39408a
Author: dankert <devnull@localhost>
Date:   Thu,  1 Jun 2006 20:19:17 +0200

ZIP-Archive erzeugen/extrahieren.

Diffstat:
serviceClasses/ArchiveUnzip.class.php | 67++++++++++++++++++++++++++++++++-----------------------------------
serviceClasses/ArchiveZip.class.php | 90+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 122 insertions(+), 35 deletions(-)

diff --git a/serviceClasses/ArchiveUnzip.class.php b/serviceClasses/ArchiveUnzip.class.php @@ -35,24 +35,11 @@ # You are allowed to redistribute this class, if you keep my name and contact e-mail on it. ############################################################## -if(!function_exists('file_put_contents')){ - // If not PHP5, creates a compatible function - Function file_put_contents($file, $data){ - if($tmp = fopen($file, "w")){ - fwrite($tmp, $data); - fclose($tmp); - return true; - } - echo "<b>file_put_contents:</b> Cannot create file $file<br>"; - return false; - } -} - class ArchiveUnzip{ - Function getVersion(){ - return "2.4"; - } + // Public + var $files = array(); + var $value = ''; var $fileName; var $compressedList; // You will problably use only this one! var $centralDirList; // Central dir list... It's a kind of 'extra attributes' for a set of files @@ -66,12 +53,22 @@ class ArchiveUnzip{ var $dirSignatureE= "\x50\x4b\x05\x06"; // end of central dir signature // Public - Function dUnzip2($fileName){ - $this->fileName = $fileName; + Function ArchiveUnzip() + { $this->compressedList = $this->centralDirList = $this->endOfCentral = Array(); } + + function open( $value ) + { + $this->fileName = tempnam('/tmp','unzip'); +// echo $this->fileName; + $fo = fopen( $this->fileName,'w'); + fwrite($fo,$value); + $this->unzipAll(); + } + Function getList($stopOnFile=false){ if(sizeof($this->compressedList)){ @@ -313,18 +310,25 @@ class ArchiveUnzip{ return $this->compressedList; } - Function getExtraInfo($compressedFileName){ + + + Function getExtraInfo($compressedFileName) + { return isset($this->centralDirList[$compressedFileName])? $this->centralDirList[$compressedFileName]: false; } - Function getZipInfo($detail=false){ + + + Function getZipInfo($detail=false) + { return $detail? $this->endOfCentral[$detail]: $this->endOfCentral; } + Function unzip($compressedFileName, $targetFileName=false){ $fdetails = &$this->compressedList[$compressedFileName]; @@ -342,19 +346,17 @@ class ArchiveUnzip{ } if(!$fdetails['uncompressed_size']){ $this->debugMsg(1, "File '<b>$compressedFileName</b>' is empty."); - return $targetFileName? - file_put_contents($targetFileName, ""): - ""; + return ""; } fseek($this->fh, $fdetails['contents-startOffset']); return $this->uncompress( fread($this->fh, $fdetails['compressed_size']), $fdetails['compression_method'], - $fdetails['uncompressed_size'], - $targetFileName - ); + $fdetails['uncompressed_size'] ); } + + Function unzipAll($targetDir=false, $baseDir="", $maintainStructure=true, $chmod=false){ if($targetDir === false) $targetDir = dirname(__FILE__)."/"; @@ -396,18 +398,13 @@ class ArchiveUnzip{ if($this->fh) fclose($this->fh); } - Function __destroy(){ - $this->close(); - } // Private (you should NOT call these methods): Function uncompress($content, $mode, $uncompressedSize, $targetFileName=false){ switch($mode){ case 0: // Not compressed - return $targetFileName? - file_put_contents($targetFileName, $content): - $content; + return $content; case 1: $this->debugMsg(2, "Shrunk mode is not supported... yet?"); return false; @@ -425,9 +422,7 @@ class ArchiveUnzip{ return false; case 8: // Deflate - return $targetFileName? - file_put_contents($targetFileName, gzinflate($content, $uncompressedSize)): - gzinflate($content, $uncompressedSize); + return gzinflate($content, $uncompressedSize); case 9: $this->debugMsg(2, "Enhanced Deflating is not supported... yet?"); return false; @@ -439,6 +434,8 @@ class ArchiveUnzip{ return false; } } + + Function debugMsg($level, $string){ if($this->debug) if($level == 1) diff --git a/serviceClasses/ArchiveZip.class.php b/serviceClasses/ArchiveZip.class.php @@ -0,0 +1,89 @@ +<?php + + +/** + * This source is taken from http://www.zend.com/zend/spotlight/creating-zip-files1.php + * Thank you! + */ +class ArchiveZip +{ + var $datasec = array(); + var $ctrl_dir = array(); + var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00"; + var $old_offset = 0; + + + function add_file($data, $name) + { + $name = str_replace("\\", "/", $name); + $unc_len = strlen($data); + $crc = crc32($data); + $zdata = gzcompress($data); + $zdate = substr ($zdata, 2, -4); + $c_len = strlen($zdata); + + + + $fr = "\x50\x4b\x03\x04"; + $fr .= "\x14\x00"; + $fr .= "\x00\x00"; + $fr .= "\x08\x00"; + $fr .= "\x00\x00\x00\x00"; + $fr .= pack("V",$crc); + $fr .= pack("V",$c_len); + $fr .= pack("V",$unc_len); + $fr .= pack("v", strlen($name) ); + $fr .= pack("v", 0 ); + $fr .= $name; + $fr .= $zdata; + $fr .= pack("V",$crc); + $fr .= pack("V",$c_len); + $fr .= pack("V",$unc_len); + + $this -> datasec[] = $fr; + + + + $new_offset = strlen(implode("", $this->datasec)); + + $cdrec = "\x50\x4b\x01\x02"; + $cdrec .="\x00\x00"; + $cdrec .="\x14\x00"; + $cdrec .="\x00\x00"; + $cdrec .="\x08\x00"; + $cdrec .="\x00\x00\x00\x00"; + $cdrec .= pack("V",$crc); + $cdrec .= pack("V",$c_len); + $cdrec .= pack("V",$unc_len); + $cdrec .= pack("v", strlen($name) ); + $cdrec .= pack("v", 0 ); + $cdrec .= pack("v", 0 ); + $cdrec .= pack("v", 0 ); + $cdrec .= pack("v", 0 ); + $cdrec .= pack("V", 32 ); + $cdrec .= pack("V", $this -> old_offset ); + + $this -> old_offset = $new_offset; + + $cdrec .= $name; + $this -> ctrl_dir[] = $cdrec; + } + + + function file() { + $data = implode("", $this -> datasec); + $ctrldir = implode("", $this -> ctrl_dir); + + return + $data. + $ctrldir. + $this -> eof_ctrl_dir. + pack("v", sizeof($this -> ctrl_dir)). + pack("v", sizeof($this -> ctrl_dir)). + pack("V", strlen($ctrldir)). + pack("V", strlen($data)). + "\x00\x00"; + } +} + +?>+ \ No newline at end of file