现在的位置: 首页 > 综合 > 正文

php 数据文件的压缩

2018年01月30日 ⁄ 综合 ⁄ 共 918字 ⁄ 字号 评论关闭
<?php
/***************************************************************************************************
数据压缩:仅支持单文件压缩
程序编写:caowlong
参       数:1、$src_file 源文件即要压缩的文件名
                  2、$dest_file 目标文件即压缩后完成的文件名,此参数不用写扩展名,默认压缩文件处于此同一目录
函数功能:将指定文件进行压缩
返 回 值:返回“ 你要压缩的名.源文件扩展名.zip ”这样的形式
****************************************************************************************************/
function create_gzip_file($src_file, $dest_file) {
$fd = fopen ($src_file, "r");
$data = fread ($fd, filesize ($src_file));
fclose ($fd);
//压缩源文件
$gz_data = gzencode($data);
$fp = fopen($dest_file.ext($src_file).".zip", "w");
fwrite($fp, $gz_data);
fclose($fp);
}
//返回扩展名
function ext($filename){
$i=strrpos($filename,".");
return substr($filename,$i);
}

//例子:
//create_gzip_file("index.php","mj"); 即将index.php文件压缩并取名为mj.php.zip

/*
此函数也能压缩单个文件,但解压时点击 “解压到 read.txt”如果报错,请点击“解压到当前文件夹”
$data = implode("", file("read.txt"));
$gzdata = gzencode($data, 9);
$fp = fopen("read.txt.gz", "w");
fwrite($fp, $gzdata);
fclose($fp);
*/

?>

抱歉!评论已关闭.