`
mengdejun
  • 浏览: 401925 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

php生成zip压缩文件

    博客分类:
  • Php
阅读更多
<?php
class PHPZip {
	function Zip($dir, $zipfilename) {
		if (@function_exists ( 'gzcompress' )) {
			$curdir = getcwd ();
			if (is_array ( $dir )) {
				$filelist = $dir;
			} else {
				$filelist = $this->GetFileList ( $dir );
			}
			
			if ((! empty ( $dir )) && (! is_array ( $dir )) && (file_exists ( $dir )))
				chdir ( $dir );
			else
				chdir ( $curdir );
			
			if (count ( $filelist ) > 0) {
				foreach ( $filelist as $filename ) {
					if (is_file ( $filename )) {
						$fd = fopen ( $filename, "r" );
						$content = fread ( $fd, filesize ( $filename ) );
						fclose ( $fd );
						
						if (is_array ( $dir ))
							$filename = basename ( $filename );
						$this->addFile ( $content, $filename );
					}
				}
				$out = $this->file ();
				
				chdir ( $curdir );
				$fp = fopen ( $zipfilename, "w" );
				fwrite ( $fp, $out, strlen ( $out ) );
				fclose ( $fp );
			}
			return 1;
		} else
			return 0;
	}
	
	function GetFileList($dir) {
		if (file_exists ( $dir )) {
			$args = func_get_args ();
			$pref = $args [1];
			
			$dh = opendir ( $dir );
			while ( $files = readdir ( $dh ) ) {
				if (($files != ".") && ($files != "..")) {
					if (is_dir ( $dir . $files )) {
						$curdir = getcwd ();
						chdir ( $dir . $files );
						$file = array_merge ( $file, $this->GetFileList ( "", "$pref$files/" ) );
						chdir ( $curdir );
					} else
						$file [] = $pref . $files;
				}
			}
			closedir ( $dh );
		}
		return $file;
	}
	
	var $datasec = array ();
	var $ctrl_dir = array ();
	var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
	var $old_offset = 0;
	
	/**
	 * Converts an Unix timestamp to a four byte DOS date and time format (date
	 * in high two bytes, time in low two bytes allowing magnitude comparison).
	 *
	 * @param  integer  the current Unix timestamp
	 *
	 * @return integer  the current date in a four byte DOS format
	 *
	 * @access private
	 */
	function unix2DosTime($unixtime = 0) {
		$timearray = ($unixtime == 0) ? getdate () : getdate ( $unixtime );
		
		if ($timearray ['year'] < 1980) {
			$timearray ['year'] = 1980;
			$timearray ['mon'] = 1;
			$timearray ['mday'] = 1;
			$timearray ['hours'] = 0;
			$timearray ['minutes'] = 0;
			$timearray ['seconds'] = 0;
		} // end if
		

		return (($timearray ['year'] - 1980) << 25) | ($timearray ['mon'] << 21) | ($timearray ['mday'] << 16) | ($timearray ['hours'] << 11) | ($timearray ['minutes'] << 5) | ($timearray ['seconds'] >> 1);
	} // end of the 'unix2DosTime()' method
	

	/**
	 * Adds "file" to archive
	 *
	 * @param  string   file contents
	 * @param  string   name of the file in the archive (may contains the path)
	 * @param  integer  the current timestamp
	 *
	 * @access public
	 */
	function addFile($data, $name, $time = 0) {
		$name = str_replace ( '\\', '/', $name );
		
		$dtime = dechex ( $this->unix2DosTime ( $time ) );
		$hexdtime = '\x' . $dtime [6] . $dtime [7] . '\x' . $dtime [4] . $dtime [5] . '\x' . $dtime [2] . $dtime [3] . '\x' . $dtime [0] . $dtime [1];
		eval ( '$hexdtime = "' . $hexdtime . '";' );
		
		$fr = "\x50\x4b\x03\x04";
		$fr .= "\x14\x00"; // ver needed to extract
		$fr .= "\x00\x00"; // gen purpose bit flag
		$fr .= "\x08\x00"; // compression method
		$fr .= $hexdtime; // last mod time and date
		

		// "local file header" segment
		$unc_len = strlen ( $data );
		$crc = crc32 ( $data );
		$zdata = gzcompress ( $data );
		$c_len = strlen ( $zdata );
		$zdata = substr ( substr ( $zdata, 0, strlen ( $zdata ) - 4 ), 2 ); // fix crc bug
		$fr .= pack ( 'V', $crc ); // crc32
		$fr .= pack ( 'V', $c_len ); // compressed filesize
		$fr .= pack ( 'V', $unc_len ); // uncompressed filesize
		$fr .= pack ( 'v', strlen ( $name ) ); // length of filename
		$fr .= pack ( 'v', 0 ); // extra field length
		$fr .= $name;
		
		// "file data" segment
		$fr .= $zdata;
		
		// "data descriptor" segment (optional but necessary if archive is not
		// served as file)
		$fr .= pack ( 'V', $crc ); // crc32
		$fr .= pack ( 'V', $c_len ); // compressed filesize
		$fr .= pack ( 'V', $unc_len ); // uncompressed filesize
		

		// add this entry to array
		$this->datasec [] = $fr;
		$new_offset = strlen ( implode ( '', $this->datasec ) );
		
		// now add to central directory record
		$cdrec = "\x50\x4b\x01\x02";
		$cdrec .= "\x00\x00"; // version made by
		$cdrec .= "\x14\x00"; // version needed to extract
		$cdrec .= "\x00\x00"; // gen purpose bit flag
		$cdrec .= "\x08\x00"; // compression method
		$cdrec .= $hexdtime; // last mod time & date
		$cdrec .= pack ( 'V', $crc ); // crc32
		$cdrec .= pack ( 'V', $c_len ); // compressed filesize
		$cdrec .= pack ( 'V', $unc_len ); // uncompressed filesize
		$cdrec .= pack ( 'v', strlen ( $name ) ); // length of filename
		$cdrec .= pack ( 'v', 0 ); // extra field length
		$cdrec .= pack ( 'v', 0 ); // file comment length
		$cdrec .= pack ( 'v', 0 ); // disk number start
		$cdrec .= pack ( 'v', 0 ); // internal file attributes
		$cdrec .= pack ( 'V', 32 ); // external file attributes - 'archive' bit set
		

		$cdrec .= pack ( 'V', $this->old_offset ); // relative offset of local header
		$this->old_offset = $new_offset;
		
		$cdrec .= $name;
		
		// optional extra field, file comment goes here
		// save to central directory
		$this->ctrl_dir [] = $cdrec;
	} // end of the 'addFile()' method
	

	/**
	 * Dumps out file
	 *
	 * @return  string  the zipped file
	 *
	 * @access public
	 */
	function file() {
		$data = implode ( '', $this->datasec );
		$ctrldir = implode ( '', $this->ctrl_dir );
		
		return $data . $ctrldir . $this->eof_ctrl_dir . pack ( 'v', sizeof ( $this->ctrl_dir ) ) . // total # of entries "on this disk"
pack ( 'v', sizeof ( $this->ctrl_dir ) ) . // total # of entries overall
pack ( 'V', strlen ( $ctrldir ) ) . // size of central dir
pack ( 'V', strlen ( $data ) ) . // offset to start of central dir
"\x00\x00"; // .zip file comment length
	} // end of the 'file()' method


} // end of the 'PHPZip' class
?>

 

<?php
	require'PHPZip.php';
	$z = new PHPZip(); //新建立一个zip的类
	//$z->Zip("","z.zip");当前文件夹压缩
	$z->Zip(array("PHPZip.php"),"phpx.zip")
?>

 

分享到:
评论

相关推荐

    php生成zip压缩文件的方法详解

    本篇文章是对php生成zip压缩文件的方法进行了详细的分析介绍,需要的朋友参考下

    用PHP生成zip文件.

    用PHP生成zip文件的类文件. 示例如: $phpZip = new PHPZip(); $phpZip-&gt;Zip("c:\abc", 'testzip.zip');

    PHP生成压缩文,不带要压缩文件的根目录

    PHP生成压缩文,不带要压缩文件的根目录,去掉根目录,有时可能需要这样

    PHP生成、下载excel、zip压缩文件_excelphp图片_excel_zip_php_

    PHP导出包含图片的excel文件;PHP生成压缩文件

    PHP生成压缩文件开发实例

    1.生成压缩文件,压缩文件名格式: 2.压缩文件存放在根目录 /upload/zipfile/年月/自定义的压缩文件名.zip 3.点击下载压缩包,系统开始对压缩文件打包,打包完成后自动开始下载 4.为了防止暴露压缩包文件路径,...

    PHP生成zip压缩包的常用方法示例

    本文实例讲述了PHP生成zip压缩包的常用方法。分享给大家供大家参考,具体如下: 压缩一个文件 我们将一个文件生成一个压缩包。 &lt;?php $path = c:/wamp/www/log.txt; $filename = test.zip; $zip = new Zip...

    php在线压缩解压(phpZip) 1.1.zip

    又一款与PHP解压缩相匹配的打包(压缩)工具,其特点是可以在远程服务器上方便、快速的压缩文件,并且提供下载链接,方便下载,是网站备份的好工具。其他用途有待开发!(声明:此程序为开源程序,本人只是对其二次...

    PHPZip类实现php生成zip压缩包

    PHPZip类,生成zip格式的压缩包,可以直接在服务器上生成压缩包,也可以生成压缩并下载,也可以直接在线解压以及获得压缩包的相应信息

    PHP在线打包在线压缩ZIP工具 v1.1.zip

    PHP在线打包在线压缩ZIP工具使用方法: 下载到本地之后,上传PHPZip.php文件到你的服务器上,对其进行访问。 默认密码:xibo123 修改密码方法:在地址栏访问你服务器上PHPZip.php文件,在其后面加上?pwd=密码代码...

    php ZipArchive压缩函数详解实例

    用ZipArchive压缩文件,这个是php的扩展类,自php5.2...php /* 生成zip 压缩文件 */function create_zip($files = array(),$destination = ”,$overwrite = false) { //if the zip file already exists and overwrite

    PHP生成和在线浏览类库.zip

    &lt;?... defined('BASEPATH')) exit('No direct script access allowed');.../* Location: ./application/config/constants.php */这是一个PHP生成和在线浏览类库,需要的朋友可以下载使用。

    ZipFile.php(PHP压缩类,打包下载)

    PHPZip类,生成zip格式的压缩包,生成压缩并下载,简单的zip压缩类

    PHP生成压缩文件实例

    1.生成压缩文件,压缩文件名格式: 2.压缩文件存放在根目录 /upload/zipfile/年月/自定义的压缩文件名.zip 3.点击下载压缩包,系统开始对压缩文件打包,打包完成后自动开始下载 4.为了防止暴露压缩包文件路径,需要...

    PHP压缩文件

    程序比较简单,适合购买了空间,但空间服务商不提供解压和压缩服务的用户。使用需开启zip扩展

    Laravel 中创建 Zip 压缩文件并提供下载的实现方法

    事实上,这不是关于 Laravel 的,而是和 PHP 的关联更多,我们准备使用从 PHP 5.2 以来就存在的 ZipArchive 类 ,如果要使用,需要确保php.ini 中的 ext-zip 扩展开启。 任务 1: 存储用户的发票文件到 storage/...

    php的zip解压缩类pclzip使用示例

    PclZip简介PclZip是一个很强大的压缩与解压缩zip文件的PHP类,PclZip library能够压缩与解压缩Zip格式的压缩档(WinZip、PKZIP);...生成zip文件用法一: 复制代码 代码如下:&lt; ?phpinclude_once(‘pc

Global site tag (gtag.js) - Google Analytics