php ZIP压缩类实例分享
php ZIP压缩类实例分享
<?php
$zipfiles =array("/root/pooy/test1.txt","/root/pooy/test2.txt");
$z = new PHPZip();
//$randomstr = random(8);
$zipfile = TEMP."/photocome_".$groupid.".zip";
$z->Zip($zipfiles, $zipfile); //添加文件列表
PHP的ZIP压缩类:
<p><?php
#
# PHPZip v1.2 by Sext (sext@neud.net) 2002-11-18
# (Changed: 2003-03-01)
#
# Makes zip archive
#
# Based on "Zip file creation class", uses zLib
#
#
class PHPZip
{
function Zip($dir, $zipfilename)
{
if (@function_exists('gzcompress'))
{
$curdir = getcwd();
if (is_array($dir))
{
$filelist = $dir;
}
else
{
$filelist = $this -> GetFileList($dir);
}</p>
<p> if ((!empty($dir))&&(!is_array($dir))&&(file_exists($dir))) chdir($dir);
else chdir($curdir);</p>
<p> if (count($filelist)>0)
{
foreach($filelist as $filename)
{
if (is_file($filename))
{
$fd = fopen ($filename, "r");
$content = fread ($fd, filesize ($filename));
fclose ($fd);</p>
<p> if (is_array($dir)) $filename = basename($filename);
$this -> addFile($content, $filename);
}
}
$out = $this -> file();</p>
<p> chdir($curdir);
$fp = fopen($zipfilename, "w");
fwrite($fp, $out, strlen($out));
fclose($fp);
}
return 1;
}
else return 0;
}</p>
<p> function GetFileList($dir)
{
if (file_exists($dir))
{
$args = func_get_args();
$pref = $args[1];</p>
<p> $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;
}</p>
<p> var $datasec = array();
var $ctrl_dir = array();
var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
var $old_offset = 0;</p>
<p> /**
* 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);</p>
<p> if ($timearray['year'] < 1980) {
$timearray['year'] = 1980;
$timearray['mon'] = 1;
$timearray['mday'] = 1;
$timearray['hours'] = 0;
$timearray['minutes'] = 0;
$timearray['seconds'] = 0;
} // end if</p>
<p> 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</p>
<p> /**
* 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);</p>
<p> $dtime = dechex($this->unix2DosTime($time));
$hexdtime = '\x' . $dtime[6] . $dtime[7]
<a >Western union point</a> . '\x' . $dtime[4] . $dtime[5]
. '\x' . $dtime[2] . $dtime[3]
. '\x' . $dtime[0] . $dtime[1];
eval('$hexdtime = "' . $hexdtime . '";');</p>
<p> $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</p>
<p> // "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;</p>
<p> // "file data" segment
$fr .= $zdata;</p>
<p> // "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</p>
<p> // add this entry to array
$this -> datasec[] = $fr;
$new_offset = strlen(implode('', $this->datasec));</p>
<p> // 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</p>
<p> $cdrec .= pack('V', $this -> old_offset ); // relative offset of local header
$this -> old_offset = $new_offset;</p>
<p> $cdrec .= $name;</p>
<p> // optional extra field, file comment goes here
// save to central directory
$this -> ctrl_dir[] = $cdrec;
} // end of the 'addFile()' method</p>
<p> /**
* Dumps out file
*
* @return string the zipped file
*
* @access public
*/
function file()
{
$data = implode('', $this -> datasec);
$ctrldir = implode('', $this -> ctrl_dir);</p>
<p> 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</p>
<p>} // end of the 'PHPZip' class
?></p>
文章来自:http://bbs.it-home.org/forum-php-1.html
php ZIP压缩类实例分享的更多相关文章
- php 文件上传类 实例分享
最近在研究php上传的内容,找到一个不错的php上传类,分享下. <?php /** * 文件上传类 * class: uploadFile * edit: www.jbxue.com */ c ...
- C#zip压缩类
改造了网上的代码,可以压缩文件夹.指定文件列表(文件和文件夹的混合列表,需要指定子文件夹下需要压缩的文件),注释很详细不做解释 public class ZipHelper { /// <sum ...
- zip压缩类
using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressi ...
- 一个zip压缩类,欢迎吐槽
package com.utils; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import j ...
- C#实现Zip压缩解压实例
原文地址:https://www.cnblogs.com/GoCircle/p/6544678.html 本文只列举一个压缩帮助类,使用的是有要添加一个dll引用ICSharpCode.SharpZi ...
- java将文件打包成ZIP压缩文件的工具类实例
package com.lanp; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...
- C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志
C#实现多级子目录Zip压缩解压实例 参考 https://blog.csdn.net/lki_suidongdong/article/details/20942977 重点: 实现多级子目录的压缩, ...
- Java操作zip压缩和解压缩文件工具类
需要用到ant.jar(这里使用的是ant-1.6.5.jar) import java.io.File; import java.io.FileInputStream; import java.io ...
- C#实现Zip压缩解压实例【转】
本文只列举一个压缩帮助类,使用的是有要添加一个dll引用ICSharpCode.SharpZipLib.dll[下载地址]. 另外说明一下的是,这个类压缩格式是ZIP的,所以文件的后缀写成 .zip. ...
随机推荐
- kubernetes之kube-ApiServer代码分析
一.概述: kube-ApiServer相当于是k8集群的一个入口,不论通过kubectl还是使用remote api 直接控制,都要经过apiserver.apiserver说白了就是一个serve ...
- 学习DTD和Schema的几个例子
可以使用DTD(文档类型定义)来规定xml文档构建模块,可用起对xml文件进行验证.具体用法转:http://www.w3school.com.cn/dtd/dtd_intro.asp.同样也可以用S ...
- Linux 常用命令学习
sed 大法: cat file | sed 's/string/replace_str/' file 按大小分割文件 split -b 100m filename 设置vi不自动转换tab: set ...
- Get请求出现乱码的解决方案
Get请求出现乱码,模拟一般出现的场景.场景一:超链接<a href=”url?name=张三&age=18”>场景二:window.opon(“url?name=张三&a ...
- XML实例入门2
工具:notepad++.VS2008(MSXML6.0) 来自msdn的例子(经过修改,因为升级到MSXML6.0,有些关键字不太一样了), 需要文件books.xml,books.vsd(博客只支 ...
- asp.net mvc 注册中的邮箱激活功能实现(二)
邮件发送功能封装 /// <summary> /// 发送注册邮件 /// </summary> /// <param name ...
- 【Chromium中文文档】进程模型
进程模型 转载请注明出处:https://ahangchen.gitbooks.io/chromium_doc_zh/content/zh//General_Architecture/Process_ ...
- 开发框架CIIP
github开源:企业级应用快速开发框架CIIP WEB+WIN+移动端 简介 CIIP是基于XAF开发的开源信息系统框架.CIIP最常见的应用场景是基于数据库的企业级应用程序,例如供应链系统,E ...
- 为YUM设置代理的方法
http://momo-fedora.blog.163.com/blog/static/1161345982009329611312/
- perl 解json数组
<pre name="code" class="cpp">http://11.36.10.82:4000/api/bus?bus=307&f ...