Home >Backend Development >PHP Tutorial >php zlib压缩和解压缩swf文件的代码_PHP

php zlib压缩和解压缩swf文件的代码_PHP

WBOY
WBOYOriginal
2016-06-01 12:24:251123browse

使用php就不一样了,php包含了zlib的链接库,可以直接使用其相关功能,下面是我写的压缩和结压缩swf文件的例子:
//没有加入判断swf文件是否已经压缩,入需要可以根据文件的第一个字节是'F'或者'C'来判断
压缩swf文件:
//--------------------------------------------------------------------------------------------------
//文件名
$filename = "test.swf";
//打开文件
$rs = fopen($filename,"r");
//读取文件的数据
$str = fread($rs,filesize($filename));
//设置swf头文件
$head = substr($str,1,8);
$head = "C".$head;
//获取swf文件内容
$body = substr($str,8);
//压缩文件内容,使用最高压缩级别9
$body = gzcompress($body, 9);
//合并文件头和内容
$str = $head.$body;
//关闭读取的文件流
fclose($rs);
//创建一个新的文件
$ws = fopen("create.swf","w");
//写文件
fwrite($ws,$str);
//关闭文件留
fclose($ws);
//----------------------------------------------------------------------------------------------------
?>
解压缩swf文件:
//----------------------------------------------------------------------------------------------------
//文件名
$filename = "test.swf";
//打开文件
$rs = fopen($filename,"r");
//读取文件的数据
$str = fread($rs,filesize($filename));
//设置swf头文件
$head = substr($str,1,8);
$head = "F".$head;
//获取swf文件内容
$body = substr($str,8);
//解压缩文件内容
$body = gzuncompress($body);
//合并文件头和内容
$str = $head.$body;
//关闭读取的文件流
fclose($rs);
//创建一个新的文件
$ws = fopen("create.swf","w");
//写文件
fwrite($ws,$str);
//关闭文件留
fclose($ws);
//----------------------------------------------------------------------------------------------------
?>
怎么样?是不是很简单?呵呵,php给我们的不止是简单“`

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn