php bz2文件压缩,PHP 程序输入,输出流,压缩流及 Linux 命令:gzip,bzip2,zip,rar,tar | 学步园...

// PHP 所有 IO 都是流

// 两个特殊函数

popen()

// 说明: popen() 只向一个程序提供单向的 IO,可以只用 w 活 r 作为打开模式。当为一个程序打开流时,也可以叫做一个 管道,可以使用所有常规的文件函数来从该管道读取或者写入,并且使用 feof() 来检测文件是否还有为读取的数据

$fp = popen('ls -l /', 'r');

while( ! feof( $fp)){

echo fgets( $fp);

}

pclose( $fp);

// 读取根目录文件列表

?>

// popen() 有很大的限制,不能与打开的进程进行任何交互,这点可以用 proc_open() 来代替

// 通过 proc_open(),可以连接所有进程的输入和输出句柄到一个可以读取的管道,或者是可以在脚本中写入管道,或是一个文件。管道被当作文件句柄来处理,只不过不能同时针对读取和写入打开一个文件句柄。

proc_open()

resource proc_open ( string $cmd , array $descriptorspec , array &$pipes [, string $cwd [, array $env [, array $other_options ]]] )

// 对函数声明的参数作下说明:

// $cmd: 要执行的命令(在 php 脚本中启动一个子进程)

// $descriptorspec: 索引数组,数组键表示描述符(描述针对输入或输出的一个文件处理器),键值代表 PHP 怎么传递这个文件处理器给子进程,0 代表标准输入,1 标准输出,2 标准错误

// 数组的每个元素可以是:

// * 一个描述管道的 数组 , 这个数组的第一个元素的管道的类型,第二个是给定管道的选项,有效的管道类型是: pipe{这时数组的第二个元素是 'r' 或 'w'}, file{这时数组的第二个元素是文件名字}

// * 一个代表文件处理器的流{eg, 打开的文件,socket, STDIN}

// 以上这几中情况在下面的例子中都包括了

$fin = fopen( 'readfrom', 'rb');

$fout = fopen( 'writeto', 'wb');

$desc = array( 0 => $fin, 1 => $fout);

$res = proc_open( 'php', $desc, $pipes);

if( $res){

proc_close( $res);

}

// 脚本为 php 启动一个解释器--一个子进程,子进程从 $fin 中读取数据交给 php 解释器处理,结果输出到 $fou

// readfrom 内容

// <?php

// echo 'hello world';

// ?>

// 程序运行后 writeto 的内容

// hello world

?>

$desc = array(

0 => array( 'pipe', 'r'),

1 => array( 'pipe', 'w'),

);

$res = proc_open( 'php', $desc, $pipes);

if( is_resource( $res)){

fputs( $pipes[0], '<?php echo "hello world" ?>');

fclose( $pipes[0]);

while( !feof( $pipes[1] )){

$line = fgets( $pipes[1]);

echo $line;

}

}

?>

$desc = array(

0 => array( 'pipe', 'r'),

1 => array( 'file', 'out.dat', 'w'),

2 => array( 'file', 'error.dat', 'w')

);

$res = proc_open( 'php', $desc, $pipes);

if( is_resource( $res)){

fputs( $pipes[0], '<?php echo "hello world"; ?>');

fclose( $pipes[0]);

proc_close( $res);

}

// 输入输出流

// 通过 php,可以把 stdin,stdout,stderr 作为文件来处理,可以使用 fopen() 调用的协议指定符来访问。对于程序的输入输出,这个指定符就是 php:// .

// 另外两个可用的 IO 流是 php://input 和 php://output

// 通过 php://input 可以读取原始的 POST 数据

$fout = fopen('php://stdout', 'w');

$fin = fopen( 'php://stdin', 'r');

echo "hello sir/n";

fputs( $fout, "please input your name:/n");

$name = fread( $fin, 2000);

echo "hello sir, your name is $name./n";

?>

// php://stdin php://input 是只读的

// php://stdout php://stderr php://output 是只写的

// 压缩流

// PHP 提供了一些于眼所函数相关的封装处理层,使用这些库的流支持,读取和写入一个 gzipped 的或 bzipped 的文件就像读取和写入一个常规文件一样。

// 协议指定符

// compress.zlib://filename.gz

// compress.bzip2://filename.bz2

// gzip 流比标准的 r, w, a, b , + 支持更多的模式指定符。这些附加的修饰符包括从 1 到 9 的压缩级别并且压缩方法 f 表示过滤或者 h 表示只是 huffman 压缩

set_include_path( get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/misc');

$bz_file = 'compress.bzip2://from.bz2';

$gz_file = 'compress.zlib://./misc/to.gz';

$fr = fopen( $bz_file, 'rb', TRUE);

$fw = fopen( $gz_file, 'wb1');

if( is_resource( $fr) && is_resource( $fw) ){

while( !feof( $fr) ){

$data = fread( $fr, 1024);

fwrite( $fw, $data);

}

fclose( $fr);

fclose( $fw);

}

?>

// 上面讲了 php 处理压缩流,接着把 linux 压缩命令做下笔记

--------------

gzip

1, gzip是GNU zip的缩写,

----

几个有意思的:

GNU: GNU is not Unix

Phing: Phing is not GNU make

----

2,gzip 有自己的文件格式

3,通常gzip仅仅用来压缩单个文件。多个文件的压缩归档通常是首先将这些文件合并成一个tar文件,然后使用gzip进行压缩,最后生成的.tar.gz或者.tgz文件,这就是所谓的“tar压缩包”或者“tarball”。

4,压缩与解压

--

gzip file 压缩 file 为 file.gz

gzip -d file.gz 解压 file.gz 为 file

--

5, gzip 有 1-9 个压缩级别,1 的压缩比最差,但是压缩速度最高;默认压缩级别为 6

gzip -2 file 指定压缩级别为 2

6,其他应用

HTTP/1.1 协议允许客户端可以选择要求从服务器下载压缩内容,这个标准本身定义了两种压缩方法:“gzip”(内容用gzip数据流进行封装)以及 “deflate”(内容是原始格式、没有数据头的DEFLATE数据流)。许多HTTP客户端库以及绝大多数当今的浏览器都支持这两种格式。

HTTP Request header

Accetp-Encoding: gzip, deflate

HTTP Response header:

Content-Encoding: gzip

7, zlib是DEFLATE

// bzip2

1, bzip2 比传统的 gzip 或者 ZIP 的压缩效率更高,但是它的压缩速度较慢。从这点来说,它非常类似于最近出现的其它一些压缩算法。与 RAR 或者 ZIP 等其它不同的是,bzip2 只是一个数据压缩工具,而不是归档工具,在这一点上它与 gzip 类似。

2, 压缩与解压

--

bzip2 file 压缩 file 为 file.bz2

bzip2 -d file.bz2 解压 file.bz2 为 file

bunzip2 file.bz2 作用同上

3, 直接查看 bzip2 文件中内容而不打开

bzcat file.bz2

--

// zip

zip

1, ZIP文件格式是一种流行的数据压缩和文档储存的文件格式,原名Deflate,发明者为菲尔·卡茨(Phil Katz),他于1989年1月公布了该格式的资料。ZIP通常使用后缀名“.zip”,它的MIME格式为 application/zip 。目前,ZIP格式属于几种主流的压缩格式之一,其竞争者包括RAR格式以及开放源码的7-Zip格式。从性能上比较,RAR及7-Zip格式较ZIP格式压缩率较高,而7-Zip由于提供了免费的压缩工具而逐渐在更多的领域得到应用。

2, 压缩与解压

----

zip -r folder.zip folder 压缩 folder 文件夹为 folder.zip

zip file.zip file 压缩 file 文件夹为 file.zip

unzip folder.zip 解压 folder.zip 文件夹为 folder

----

// rar

rar

1,需要现安装 rar 包

sudo apt-get install rar

2, 压缩与解压

----

rar a file.rar file 压缩 file 为 file.rar

rar e file.rar 解压 file.rar 为 file

----

// tar

tar

---

1,tar -cf folder.tar folder 将文件夹 folder 归档为 folder.tar

2. tar -xf folder.tar 将归档文件还原为 folder 文件夹

3, tar -czvf folder.tar.gz folder 将文件夹 folder 归档为后使用 gzip 压缩为 folder.tar.gz

4, tar -xzvf folder.tar.gz 将文件 folder.tar.gz 解压还原为 folder 文件夹

5, tar -cjvf folder.tar.bz2 folder 将文件夹 folder 归档为后使用 bzip2 压缩为 folder.tar.bz2

6, tar -xjvf folder.tar.bz2 将文件 folder.tar.bz2 解压还原为 folder 文件夹

---

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值