• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • 位置: php 中文手册 -> php 外部扩展库

    bzip2库(压缩)

    安装

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

    本扩展定义了一种资源类型:用于识别和操作 bz2 文件的指针。

    For ubuntu users:
    Install the library using
         sudo apt-get install libbz2-dev
    and then add this to your configure command
         --with-bz2
    this will automatically detect the default directory.
    The mistake I did was installing php7.1-bz2 instead of above given lib.
    Wanna say thanks?
    Please feed stray animals :)
    

    例子

    该范例打开一个临时文件并写入了一个测试字符串,然后输出该文件里的内容。

    <?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);
    ?>