超越PHP PHP动态 | 经典文章 | CLASS | 相关下载 | 常见问题 | FORUM | WIKI | 在线手册
Site search:    
<bcsubbzclose>
Last updated: Fri, 22 Jun 2007

V. Bzip2 压缩函数库

简介

Bzip2 函数用来透明的读写 bzip2 (.bz2) 压缩文件。

需求

此模块使用 Julian Seward 写的 bzip2 库。此模块需要 bzip2/libbzip2 版本 >= 1.0.x。

安装

Bzip2 support in PHP is not enabled by default. You will need to use the --with-bz2[=DIR] configuration option when compiling PHP to enable bzip2 support.

运行时配置

该扩展模块未定义任何设置指令。

资源类型

此扩展定义了一个资源类型: 一个文件指针,指向正在被操作的 bz2 文件。

预定义常量

该扩展模块未定义任何常量。

该例子打开一临时文件,并写入一测试字符串,然后打印文件内容。

例子 1. Bzip2 示例

<?php

$filename
= "/tmp/testfile.bz2";
$str = "This is a test string.\n";

// 打开写入文件
$bz = bzopen($filename, "w");

// 写入字符串到文件
bzwrite($bz, $str);

// 关闭文件
bzclose($bz);

// 打开读取文件
$bz = bzopen($filename, "r");

// 读取 10 个字符
echo bzread($bz, 10);

// 输出直道文件结尾 (或下1024字节) 并关闭它。
echo bzread($bz);

bzclose($bz);

?>
目录
bzclose -- Close a bzip2 file
bzcompress -- Compress a string into bzip2 encoded data
bzdecompress -- Decompresses bzip2 encoded data
bzerrno -- Returns a bzip2 error number
bzerror -- Returns the bzip2 error number and error string in an array
bzerrstr -- Returns a bzip2 error string
bzflush -- Force a write of all buffered data
bzopen -- Opens a bzip2 compressed file
bzread -- Binary safe bzip2 file read
bzwrite -- Binary safe bzip2 file write



add a note add a note User Contributed Notes
Bzip2 压缩函数库
ec10 at gmx dot net
21-May-2004 02:34
/**
* @return bool
* @param string $in
* @param string $out
* @desc compressing the file with the bzip2-extension
*/
function bzip2 ($in, $out)
{
if (!file_exists ($in) || !is_readable ($in))
return false;
if ((!file_exists ($out) && !is_writeable (dirname ($out)) || (file_exists($out) && !is_writable($out)) ))
return false;

$in_file = fopen ($in, "rb");
$out_file = bzopen ($out, "wb");

while (!feof ($in_file)) {
$buffer = fgets ($in_file, 4096);
bzwrite ($out_file, $buffer, 4096);
}

fclose ($in_file);
bzclose ($out_file);

return true;
}

/**
* @return bool
* @param string $in
* @param string $out
* @desc uncompressing the file with the bzip2-extension
*/
function bunzip2 ($in, $out)
{
if (!file_exists ($in) || !is_readable ($in))
return false;
if ((!file_exists ($out) && !is_writeable (dirname ($out)) || (file_exists($out) && !is_writable($out)) ))
return false;

$in_file = bzopen ($in, "rb");
$out_file = fopen ($out, "wb");

while ($buffer = bzread ($in_file, 4096)) {
fwrite ($out_file, $buffer, 4096);
}

bzclose ($in_file);
fclose ($out_file);

return true;
}

<bcsubbzclose>
 Last updated: Fri, 22 Jun 2007
view source | feedback | send page | sitemap | aboutus   
Copyright ® 2002-2003 PHPE.NET. All rights reserved
Last updated:2002-11-22