PHP
downloads | documentation | faq | getting help | mailing lists | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

bzclose> <bcompiler_write_included_filename
Last updated: Sun, 25 Nov 2007

view this page in

Bzip2 压缩函数

简介

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

需求

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

安装

PHP 的 bzip2 支持默认未打开。编译 PHP 时需要 --with-bz2[=DIR] 配置选项来激活 bzip2 支持。

运行时配置

本扩展模块在 php.ini 中未定义任何配置选项。

资源类型

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

预定义常量

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

范例

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

Example#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($bz10);

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

bzclose($bz);

?>

Table of Contents

  • 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
20-May-2004 11:34
<?php
/**
 * @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;
}
?>

bzclose> <bcompiler_write_included_filename
Last updated: Sun, 25 Nov 2007
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites