<?php
/**
*
*  Copyright:  (c) 2004 - 2008 [AOKOSOFT] Read8
*  Web Site:   http://www.aokosoft.com http://www.read8.net
*  Powered by: [AOKOSOFT] Read8 Group
*  Support:    http://bbs.read8.net
*  License:    http://www.read8.net/license
*
*/

if(!defined('IN_Read8')) exit('Access Denied');

class zip{
	var $filename;
	var $overwrite;
	var $zipSignature = "\x50\x4b\x03\x04"; // local file header signature
	var $dirSignature = "\x50\x4b\x01\x02"; // central dir header signature
	var $dirSignatureE= "\x50\x4b\x05\x06"; // end of central dir signature
	var $files_count  = 0;
	var $fh;
	Function zip($filename){
		$this->filename  = $filename;
	}
	Function addDir($dirname, $fileComments=''){
		$this->addFile(false, $dirname.'/', $fileComments);
	}
	Function addFile($filename, $cfilename, $fileComments='', $data=false){
		if(!($fh = &$this->fh))	$fh = fopen($this->filename,'wb');

		// $filename can be a local file OR the data wich will be compressed
		if(substr($cfilename, -1)=='/'){
			$details['uncsize'] = 0;
			$data = '';
		}
		elseif(file_exists($filename)){
			$details['uncsize'] = filesize($filename);
			$data = rfor($filename);
		}
		else{
			$details['uncsize'] = strlen($filename);
			// DATA is given.. use it! :|
		}

		// if data to compress is too small, just store it
		if($details['uncsize'] < 256){
			$details['comsize'] = $details['uncsize'];
			$details['vneeded'] = 10;
			$details['cmethod'] = 0;
			$zdata = &$data;
		}
		else{ // otherwise, compress it
			$zdata = gzcompress($data);
			$zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug (thanks to Eric Mueller)
			$details['comsize'] = strlen($zdata);
			$details['vneeded'] = 10;
			$details['cmethod'] = 8;
		}
		$details['bitflag'] = 0;
		$details['crc_32']  = crc32($data);
		$lastmod_timeS  = str_pad(decbin(gmdate('s')>=32?gmdate('s')-32:gmdate('s')), 5, '0', STR_PAD_LEFT);
		$lastmod_timeM  = str_pad(decbin(gmdate('i')), 6, '0', STR_PAD_LEFT);
		$lastmod_timeH  = str_pad(decbin(gmdate('H')), 5, '0', STR_PAD_LEFT);
		$lastmod_dateD  = str_pad(decbin(gmdate('d')), 5, '0', STR_PAD_LEFT);
		$lastmod_dateM  = str_pad(decbin(gmdate('m')), 4, '0', STR_PAD_LEFT);
		$lastmod_dateY  = str_pad(decbin(gmdate('Y')-1980), 7, '0', STR_PAD_LEFT);
		$details['modtime'] = bindec("$lastmod_timeH$lastmod_timeM$lastmod_timeS");
		$details['moddate'] = bindec("$lastmod_dateY$lastmod_dateM$lastmod_dateD");
		$details['offset'] = ftell($fh);
		fwrite($fh, $this->zipSignature);
		fwrite($fh, pack('s', $details['vneeded'])); // version_needed
		fwrite($fh, pack('s', $details['bitflag'])); // general_bit_flag
		fwrite($fh, pack('s', $details['cmethod'])); // compression_method
		fwrite($fh, pack('s', $details['modtime'])); // lastmod_time
		fwrite($fh, pack('s', $details['moddate'])); // lastmod_date
		fwrite($fh, pack('V', $details['crc_32']));  // crc-32
		fwrite($fh, pack('I', $details['comsize'])); // compressed_size
		fwrite($fh, pack('I', $details['uncsize'])); // uncompressed_size
		fwrite($fh, pack('s', strlen($cfilename)));   // file_name_length
		fwrite($fh, pack('s', 0));  // extra_field_length
		fwrite($fh, $cfilename);    // file_name
		fwrite($fh, $zdata);
		$details['external_attributes']  = (substr($cfilename, -1)=='/'&&!$zdata)?16:32; // Directory or file name
		$details['comments']             = $fileComments;
		$this->appendCentralDir($cfilename, $details);
		$this->files_count++;
	}
	Function setExtra($filename, $property, $value){
		$this->centraldirs[$filename][$property] = $value;
	}
	Function save($zipComments=''){
		if(!($fh = &$this->fh))	$fh = fopen($this->filename,'w');
		$cdrec = "";
		foreach($this->centraldirs as $filename=>$cd){
			$cdrec .= $this->dirSignature;
			$cdrec .= "\x0\x0";                  // version made by
			$cdrec .= pack('v', $cd['vneeded']); // version needed to extract
			$cdrec .= "\x0\x0";                  // general bit flag
			$cdrec .= pack('v', $cd['cmethod']); // compression method
			$cdrec .= pack('v', $cd['modtime']); // lastmod time
			$cdrec .= pack('v', $cd['moddate']); // lastmod date
			$cdrec .= pack('V', $cd['crc_32']);  // crc32
			$cdrec .= pack('V', $cd['comsize']); // compressed filesize
			$cdrec .= pack('V', $cd['uncsize']); // uncompressed filesize
			$cdrec .= pack('v', strlen($filename)); // file comment length
			$cdrec .= pack('v', 0);                // extra field length
			$cdrec .= pack('v', strlen($cd['comments'])); // file comment length
			$cdrec .= pack('v', 0); // disk number start
			$cdrec .= pack('v', 0); // internal file attributes
			$cdrec .= pack('V', $cd['external_attributes']); // internal file attributes
			$cdrec .= pack('V', $cd['offset']); // relative offset of local header
			$cdrec .= $filename;
			$cdrec .= $cd['comments'];
		}
		$before_cd = ftell($fh);
		fwrite($fh, $cdrec);
		fwrite($fh, $this->dirSignatureE);
		fwrite($fh, pack('v', 0)); // number of this disk
		fwrite($fh, pack('v', 0)); // number of the disk with the start of the central directory
		fwrite($fh, pack('v', $this->files_count)); // total # of entries "on this disk"
		fwrite($fh, pack('v', $this->files_count)); // total # of entries overall
		fwrite($fh, pack('V', strlen($cdrec)));     // size of central dir
		fwrite($fh, pack('V', $before_cd));         // offset to start of central dir
		fwrite($fh, pack('v', strlen($zipComments))); // .zip file comment length
		fwrite($fh, $zipComments);
		fclose($fh);
	}
	Function appendCentralDir($filename,$properties){
		$this->centraldirs[$filename] = $properties;
	}
}
?>