压缩 php代码,在线压缩解压缩PHP代码

这篇博客分享了如何使用PHP实现在线压缩和解压缩ZIP文件的代码,包括选择文件或目录进行压缩,以及解压缩ZIP文件到指定目录。通过示例代码,读者可以了解压缩和解压缩的具体实现细节。
摘要由CSDN通过智能技术生成

分享给大家PHP在线压缩和解压缩的代码

压缩的:

复制代码

$password = "dedecms";?>

Dedecms ZIP压缩程序

Dedecms 在线压缩ZIP文件程序

使用方法:选定要压缩的文件或目录(包含子目录),即可开始压缩。

压缩的结果保留原来的目录结构。

if(!$_REQUEST["myaction"]):

?>

验证密码:

if($_REQUEST['password'] != $password) die("输入的密码不正确,请重新输入。");

echo "选择要压缩的文件或目录:
";

$fdir = opendir('./');

while($file=readdir($fdir)){

if($file=='.'|| $file=='..') continue;

echo " ";

if(is_file($file)){

echo "文件: $file
";

}else{

echo "目录: $file
";

}

}

?>

压缩文件保存到目录:

(留空为本目录,必须有写入权限)

压缩文件名称:

(.zip)

function selrev() {

with(document.myform) {

for(i=0;i

thiselm = elements[i];

if(thiselm.name.match(/dfile\[]/)) thiselm.checked = !thiselm.checked;

}

}

}

var $datastr_len   = 0;

var $dirstr_len = 0;

var $filedata = ''; //该变量只被类外部程序访问

var $gzfilename;

var $fp;

var $dirstr='';

/*

返回文件的修改时间格式.

只为本类内部函数调用.

*/

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;

}        return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) |

($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1);

} /*

初始化文件,建立文件目录,

并返回文件的写入权限.

*/

function startfile($path = 'shenbin.zip'){

$this->gzfilename=$path;

$mypathdir=array();

do{

$mypathdir[] = $path = dirname($path);

}while($path != '.');

@end($mypathdir);

do{

$path = @current($mypathdir);

@mkdir($path);

}while(@prev($mypathdir));  if($this->fp=@fopen($this->gzfilename,"w")){

return true;

}

return false;

} /*

添加一个文件到 zip 压缩包中.

*/

function addfile($data, $name){

$name     = str_replace('\\', '/', $name);

if(strrchr($name,'/')=='/') return $this->adddir($name);

$dtime    = dechex($this->unix2DosTime());

$hexdtime = '\x' . $dtime[6] . $dtime[7]

. '\x' . $dtime[4] . $dtime[5]

. '\x' . $dtime[2] . $dtime[3]

. '\x' . $dtime[0] . $dtime[1];

eval('$hexdtime = "' . $hexdtime . '";');        $unc_len = strlen($data);

$crc     = crc32($data);

$zdata   = gzcompress($data);

$c_len   = strlen($zdata);

$zdata   = substr(substr($zdata, 0, strlen($zdata) - 4), 2);

//新添文件内容格式化:

$datastr  = "\x50\x4b\x03\x04";

$datastr .= "\x14\x00";            // ver needed to extract

$datastr .= "\x00\x00";            // gen purpose bit flag

$datastr .= "\x08\x00";            // compression method

$datastr .= $hexdtime;             // last mod time and date

$datastr .= pack('V', $crc);             // crc32

$datastr .= pack('V', $c_len);           // compressed filesize

$datastr .= pack('V', $unc_len);         // uncompressed filesize

$datastr .= pack('v', strlen($name));    // length of filename

$datastr .= pack('v', 0);                // extra field length

$datastr .= $name;

$datastr .= $zdata;

$datastr .= pack('V', $crc);                 // crc32

$datastr .= pack('V', $c_len);               // compressed filesize

$datastr .= pack('V',

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值