8 function ZLibStream($type)
11 $this->s = array( FALSE, FALSE );
12 $this->filter = FALSE;
15 static function create($type)
20 if (($thiz = new ZLibStream($type)) == FALSE)
23 for ($i = 0 ; $i < 2 ; $i++)
25 if (($thiz->s = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP)) == FALSE)
28 for ($i = 0 ; $i < 2 ; $i++)
29 stream_set_blocking ( $thiz->s[$i], 0); // 0 -> not blocking
31 if ($type == 'gzip') {
32 $params = array('level' => 6, 'window' => -15, 'memory' => 9);
34 if (($thiz->filter = stream_filter_append($thiz->s[1], "zlib.deflate", STREAM_FILTER_READ, $params)) == FALSE) {
37 $thiz->head = "\037\213\010\000\000\000\000\000\000\003";
39 else if ($type == 'deflate') {
40 if (($thiz->filter = stream_filter_append($thiz->s[1], "zlib.deflate", STREAM_FILTER_READ)) == FALSE) {
49 if ($this->filter != FALSE) {
50 stream_filter_remove($this->filter);
53 for ($i = 0 ; $i < 2 ; $i++) {
54 if ($this->s[$i] != FALSE)
60 too many actors, an explanation is needed to clarify:
62 - fwrite: all data MUST be passed to write with success
63 - fflush: probably reduntant
64 - fread: all reads after successfull writes must go well,
67 function compress_chunk($s_in)
69 $s_in_l = mb_strlen($s_in, 'ASCII');
71 if ($this->head != FALSE) {
79 for ($to_be_proc = $s_in_l, $max_fail = 0 ; $to_be_proc > 0 && $max_fail < 2 ; $max_fail++) {
80 if ($to_be_proc > 0) {
82 if (($ct = fwrite($this->s[0], $s_in)) == FALSE)
87 fflush($this->s[0]); // maybe reduntant but light so ...
89 while (($ret = fread($this->s[1], 8192)) != FALSE) {
100 static function compress($enc, $s)
102 // fprintf(STDERR, "compress: [%s][%s]\n", $enc, $s);
104 if ($enc == 'gzip') {
105 return (gzencode($s, -1, FORCE_GZIP));
107 else if ($enc == 'deflate') {
108 return (gzencode($s, -1, FORCE_DEFLATE));
113 } // class ZLibStream
117 function zlibstream_test()
119 $cont = array( "pippo", "pluto", "paperino");
121 for ($f = 0 ; $f < 2 ; $f++) {
122 if (($zls = ZLibStream::create('gzip')) == FALSE) {
123 printf("ZLibStream Creation failed\n");
127 if (($fp = fopen("../../test/zlibstream".$f.".gz", "w")) == FALSE) {
128 printf("ZLibStream test output file failed\n");
132 for ($i = 0 ; $i < 9 ; $i++) {
135 $comp = $zls->compress_chunk($cont[$idx]);
146 // zlibstream_test();