deflate_add

Incrementally deflate data

说明

string deflate_add ( resource $context , string $data [, int $flush_mode = ZLIB_SYNC_FLUSH ] )

Incrementally deflates data in the specified context.

参数

context
A context created with deflate_init.

data
A chunk of data to compress.

flush_mode
One of ZLIB_BLOCK, ZLIB_NO_FLUSH, ZLIB_PARTIAL_FLUSH, ZLIB_SYNC_FLUSH (default), ZLIB_FULL_FLUSH, ZLIB_FINISH. Normally you will want to set ZLIB_NO_FLUSH to maximize compression, and ZLIB_FINISH to terminate with the last chunk of data. See the » zlib manual for a detailed description of these constants.

返回值

Returns a chunk of compressed data, 或者在失败时返回 FALSE.

错误/异常

If invalid arguments are given, an error of level E_WARNING is generated.

参见

  • deflate_init

deflate_init

Initialize an incremental deflate context

说明

resource deflate_init ( int $encoding [, array $options = array() ] )

Initializes an incremental deflate context using the specified encoding.

Note that the window option here only sets the window size of the algorithm, differently from the zlib filters where the same parameter also sets the encoding to use; the encoding must be set with the encoding parameter.

Limitation: there is currently no way to set the header information on a GZIP compressed stream, which are set as follows: GZIP signature (\x1f\x8B); compression method (\x08 == DEFLATE); 6 zero bytes; the operating system set to the current system (\x00 = Windows, \x03 = Unix, etc.)

参数

encoding
One of the ZLIB_ENCODING_* constants.

options
An associative array which may contain the following elements:

level
The compression level in range -1..9; defaults to -1.

memory
The compression memory level in range 1..9; defaults to 8.

window
The zlib window size (logarithmic) in range 8..15; defaults to 15.

strategy
One of ZLIB_FILTERED, ZLIB_HUFFMAN_ONLY, ZLIB_RLE, ZLIB_FIXED or ZLIB_DEFAULT_STRATEGY (the default).

dictionary
A string or an array of strings of the preset dictionary (default: no preset dictionary).

返回值

Returns a deflate context resource (zlib.deflate) on success, 或者在失败时返回 FALSE.

错误/异常

If an invalid option is passed to options or the context couldn't be created, an error of level E_WARNING is generated.

参见

  • deflate_add
  • inflate_init

gzclose

Close an open gz-file pointer

说明

bool gzclose ( resource $zp )

Closes the given gz-file pointer.

参数

zp
The gz-file pointer. It must be valid, and must point to a file successfully opened by gzopen.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 gzclose example

<?php
$gz = gzopen('somefile.gz','w9');
gzputs ($gz, 'I was added to somefile.gz');
gzclose($gz);
?>

参见

  • gzopen

gzcompress

Compress a string

说明

string gzcompress ( string $data [, int $level = -1 [, int $encoding = ZLIB_ENCODING_DEFLATE ]] )

This function compresses the given string using the ZLIB data format.

For details on the ZLIB compression algorithm see the document "» ZLIB Compressed Data Format Specification version 3.3" (RFC 1950).

Note:

This is not the same as gzip compression, which includes some header data. See gzencode for gzip compression.

参数

data
The data to compress.

level
The level of compression. Can be given as 0 for no compression up to 9 for maximum compression.

If -1 is used, the default compression of the zlib library is used which is 6.

encoding
One of ZLIB_ENCODING_* constants.

返回值

The compressed string or FALSE if an error occurred.

范例

示例 #1 gzcompress example

<?php
$compressed = gzcompress('Compress me', 9);
echo $compressed;
?>

更新日志

版本说明
5.4.0Added encoding parameter.

参见

  • gzdeflate
  • gzinflate
  • gzuncompress
  • gzencode

gzdecode

Decodes a gzip compressed string

说明

string gzdecode ( string $data [, int $length ] )

This function returns a decoded version of the input data.

参数

data
The data to decode, encoded by gzencode.

length
The maximum length of data to decode.

返回值

The decoded string, or FALSE if an error occurred.

参见

  • gzencode

gzdeflate

Deflate a string

说明

string gzdeflate ( string $data [, int $level = -1 [, int $encoding = ZLIB_ENCODING_RAW ]] )

This function compresses the given string using the DEFLATE data format.

For details on the DEFLATE compression algorithm see the document "» DEFLATE Compressed Data Format Specification version 1.3" (RFC 1951).

参数

data
The data to deflate.

level
The level of compression. Can be given as 0 for no compression up to 9 for maximum compression. If not given, the default compression level will be the default compression level of the zlib library.

encoding
One of ZLIB_ENCODING_* constants.

返回值

The deflated string or FALSE if an error occurred.

范例

示例 #1 gzdeflate example

<?php
$compressed = gzdeflate('Compress me', 9);
echo $compressed;
?>

更新日志

版本说明
5.4.0Added encoding parameter.

参见

  • gzinflate
  • gzcompress
  • gzuncompress
  • gzencode

gzencode

Create a gzip compressed string

说明

string gzencode ( string $data [, int $level = -1 [, int $encoding_mode = FORCE_GZIP ]] )

This function returns a compressed version of the input data compatible with the output of the gzip program.

For more information on the GZIP file format, see the document: » GZIP file format specification version 4.3 (RFC 1952).

参数

data
The data to encode.

level
The level of compression. Can be given as 0 for no compression up to 9 for maximum compression. If not given, the default compression level will be the default compression level of the zlib library.

encoding_mode
The encoding mode. Can be FORCE_GZIP (the default) or FORCE_DEFLATE.

Prior to PHP 5.4.0, using FORCE_DEFLATE results in a standard zlib deflated string (inclusive zlib headers) after a gzip file header but without the trailing crc32 checksum.

In PHP 5.4.0 and later, FORCE_DEFLATE generates RFC 1950 compliant output, consisting of a zlib header, the deflated data, and an Adler checksum.

返回值

The encoded string, or FALSE if an error occurred.

更新日志

版本说明
5.4.0FORCE_DEFLATE now generates RFC 1950 compliant output.

范例

The resulting data contains the appropriate headers and data structure to make a standard .gz file, e.g.:

示例 #1 Creating a gzip file

<?php
$data = implode("", file("bigfile.txt"));
$gzdata = gzencode($data, 9);
$fp = fopen("bigfile.txt.gz", "w");
fwrite($fp, $gzdata);
fclose($fp);
?>

参见

gzeof

Test for EOF on a gz-file pointer

说明

int gzeof ( resource $zp )

Tests the given GZ file pointer for EOF.

参数

zp
The gz-file pointer. It must be valid, and must point to a file successfully opened by gzopen.

返回值

Returns TRUE if the gz-file pointer is at EOF or an error occurs; otherwise returns FALSE.

范例

示例 #1 gzeof example

<?php
$gz = gzopen('somefile.gz', 'r');
while (!gzeof($gz)) {
  echo gzgetc($gz);
}
gzclose($gz);
?>

gzfile

Read entire gz-file into an array

说明

array gzfile ( string $filename [, int $use_include_path = 0 ] )

This function is identical to readgzfile, except that it returns the file in an array.

参数

filename
The file name.

use_include_path
You can set this optional parameter to 1, if you want to search for the file in the include_path too.

返回值

An array containing the file, one line per cell, empty lines included, and with newlines still attached.

范例

示例 #1 gzfile example

<?php
$lines = gzfile('somefile.gz');
foreach ($lines as $line) {
    echo $line;
}
?>

参见

  • readgzfile
  • gzopen

gzgetc

Get character from gz-file pointer

说明

string gzgetc ( resource $zp )

Returns a string containing a single (uncompressed) character read from the given gz-file pointer.

参数

zp
The gz-file pointer. It must be valid, and must point to a file successfully opened by gzopen.

返回值

The uncompressed character or FALSE on EOF (unlike gzeof).

范例

示例 #1 gzgetc example

<?php
$gz = gzopen('somefile.gz', 'r');
while (!gzeof($gz)) {
  echo gzgetc($gz);
}
gzclose($gz);
?>

参见

  • gzopen
  • gzgets

gzgets

Get line from file pointer

说明

string gzgets ( resource $zp [, int $length ] )

Gets a (uncompressed) string of up to length - 1 bytes read from the given file pointer. Reading ends when length - 1 bytes have been read, on a newline, or on EOF (whichever comes first).

参数

zp
The gz-file pointer. It must be valid, and must point to a file successfully opened by gzopen.

length
The length of data to get.

返回值

The uncompressed string, or FALSE on error.

范例

示例 #1 gzgets example

<?php
$handle = gzopen('somefile.gz', 'r');
while (!gzeof($handle)) {
   $buffer = gzgets($handle, 4096);
   echo $buffer;
}
gzclose($handle);
?>

参见

  • gzopen
  • gzgetc
  • gzwrite

gzgetss

Get line from gz-file pointer and strip HTML tags

Warning

This function has been DEPRECATED as of PHP 7.3.0. Relying on this function is highly discouraged.

说明

string gzgetss ( resource $zp , int $length [, string $allowable_tags ] )

Identical to gzgets, except that gzgetss attempts to strip any HTML and PHP tags from the text it reads.

参数

zp
The gz-file pointer. It must be valid, and must point to a file successfully opened by gzopen.

length
The length of data to get.

allowable_tags
You can use this optional parameter to specify tags which should not be stripped.

返回值

The uncompressed and stripped string, or FALSE on error.

范例

示例 #1 gzgetss example

<?php
$handle = gzopen('somefile.gz', 'r');
while (!gzeof($handle)) {
   $buffer = gzgetss($handle, 4096);
   echo $buffer;
}
gzclose($handle);
?>

参见

  • gzopen
  • gzgets
  • strip_tags

gzinflate

Inflate a deflated string

说明

string gzinflate ( string $data [, int $length = 0 ] )

This function inflates a deflated string.

参数

data
The data compressed by gzdeflate.

length
The maximum length of data to decode.

返回值

The original uncompressed data or FALSE on error.

The function will return an error if the uncompressed data is more than 32768 times the length of the compressed input data or more than the optional parameter length.

范例

示例 #1 gzinflate example

<?php
$compressed   = gzdeflate('Compress me', 9);
$uncompressed = gzinflate($compressed);
echo $uncompressed;
?>

参见

  • gzdeflate
  • gzcompress
  • gzuncompress
  • gzencode

gzopen

Open gz-file

说明

resource gzopen ( string $filename , string $mode [, int $use_include_path = 0 ] )

Opens a gzip (.gz) file for reading or writing.

gzopen can be used to read a file which is not in gzip format; in this case gzread will directly read from the file without decompression.

参数

filename
The file name.

mode
As in fopen (rb or wb) but can also include a compression level (wb9) or a strategy: f for filtered data as in wb6f, h for Huffman only compression as in wb1h. (See the description of deflateInit2 in zlib.h for more information about the strategy parameter.)

use_include_path
You can set this optional parameter to 1, if you want to search for the file in the include_path too.

返回值

Returns a file pointer to the file opened, after that, everything you read from this file descriptor will be transparently decompressed and what you write gets compressed.

If the open fails, the function returns FALSE.

范例

示例 #1 gzopen Example

<?php
$fp = gzopen("/tmp/file.gz", "r");
?>

参见

  • gzclose

gzpassthru

Output all remaining data on a gz-file pointer

说明

int gzpassthru ( resource $zp )

Reads to EOF on the given gz-file pointer from the current position and writes the (uncompressed) results to standard output.

Note:

You may need to call gzrewind to reset the file pointer to the beginning of the file if you have already written data to it.

小贴士

If you just want to dump the contents of a file to the output buffer, without first modifying it or seeking to a particular offset, you may want to use the readgzfile function, which saves you the gzopen call.

参数

zp
The gz-file pointer. It must be valid, and must point to a file successfully opened by gzopen.

返回值

The number of uncompressed characters read from gz and passed through to the input, or FALSE on error.

范例

示例 #1 gzpassthru example

<?php
$fp = gzopen('file.gz', 'r');
gzpassthru($fp);
gzclose($fp);
?>

gzputs

别名 gzwrite

说明

此函数是该函数的别名: gzwrite.

gzread

Binary-safe gz-file read

说明

string gzread ( resource $zp , int $length )

gzread reads up to length bytes from the given gz-file pointer. Reading stops when length (uncompressed) bytes have been read or EOF is reached, whichever comes first.

参数

zp
The gz-file pointer. It must be valid, and must point to a file successfully opened by gzopen.

length
The number of bytes to read.

返回值

The data that have been read.

范例

示例 #1 gzread example

<?php
// get contents of a gz-file into a string
$filename = "/usr/local/something.txt.gz";
$zd = gzopen($filename, "r");
$contents = gzread($zd, 10000);
gzclose($zd);
?>

参见

  • gzwrite
  • gzopen
  • gzgets
  • gzgetss
  • gzfile
  • gzpassthru

gzrewind

Rewind the position of a gz-file pointer

说明

bool gzrewind ( resource $zp )

Sets the file position indicator of the given gz-file pointer to the beginning of the file stream.

参数

zp
The gz-file pointer. It must be valid, and must point to a file successfully opened by gzopen.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

参见

  • gzseek
  • gztell

gzseek

Seek on a gz-file pointer

说明

int gzseek ( resource $zp , int $offset [, int $whence = SEEK_SET ] )

Sets the file position indicator for the given file pointer to the given offset byte into the file stream. Equivalent to calling (in C) gzseek(zp, offset, SEEK_SET).

If the file is opened for reading, this function is emulated but can be extremely slow. If the file is opened for writing, only forward seeks are supported; gzseek then compresses a sequence of zeroes up to the new starting position.

参数

zp
The gz-file pointer. It must be valid, and must point to a file successfully opened by gzopen.

offset
The seeked offset.

whence
whence values are:

  • SEEK_SET - Set position equal to offset bytes.
  • SEEK_CUR - Set position to current location plus offset.

If whence is not specified, it is assumed to be SEEK_SET.

返回值

Upon success, returns 0; otherwise, returns -1. Note that seeking past EOF is not considered an error.

范例

示例 #1 gzseek example

<?php
$gz = gzopen('somefile.gz', 'r');
gzseek($gz,2);
echo gzgetc($gz);
gzclose($gz);
?>

参见

  • gztell
  • gzrewind

gztell

Tell gz-file pointer read/write position

说明

int gztell ( resource $zp )

Gets the position of the given file pointer; i.e., its offset into the uncompressed file stream.

参数

zp
The gz-file pointer. It must be valid, and must point to a file successfully opened by gzopen.

返回值

The position of the file pointer or FALSE if an error occurs.

参见

  • gzopen
  • gzseek
  • gzrewind

gzuncompress

Uncompress a compressed string

说明

string gzuncompress ( string $data [, int $length = 0 ] )

This function uncompress a compressed string.

参数

data
The data compressed by gzcompress.

length
The maximum length of data to decode.

返回值

The original uncompressed data or FALSE on error.

The function will return an error if the uncompressed data is more than 32768 times the length of the compressed input data or more than the optional parameter length.

范例

示例 #1 gzuncompress example

<?php
$compressed   = gzcompress('Compress me', 9);
$uncompressed = gzuncompress($compressed);
echo $uncompressed;
?>

参见

  • gzcompress
  • gzinflate
  • gzdeflate
  • gzencode

gzwrite

Binary-safe gz-file write

说明

int gzwrite ( resource $zp , string $string [, int $length ] )

gzwrite writes the contents of string to the given gz-file.

参数

zp
The gz-file pointer. It must be valid, and must point to a file successfully opened by gzopen.

string
The string to write.

length
The number of uncompressed bytes to write. If supplied, writing will stop after length (uncompressed) bytes have been written or the end of string is reached, whichever comes first.

Note:

Note that if the length argument is given, then the magic_quotes_runtime configuration option will be ignored and no slashes will be stripped from string.

返回值

Returns the number of (uncompressed) bytes written to the given gz-file stream.

范例

示例 #1 gzwrite example

<?php
$string = 'Some information to compress';
$gz = gzopen('somefile.gz','w9');
gzwrite($gz, $string);
gzclose($gz);
?>

参见

  • gzread
  • gzopen

inflate_add

Incrementally inflate encoded data

说明

string inflate_add ( resource $context , string $encoded_data [, int $flush_mode = ZLIB_SYNC_FLUSH ] )

Incrementally inflates encoded data in the specified context.

Limitation: header information from GZIP compressed data are not made available.

参数

context
A context created with inflate_init.

encoded_data
A chunk of compressed data.

flush_mode
One of ZLIB_BLOCK, ZLIB_NO_FLUSH, ZLIB_PARTIAL_FLUSH, ZLIB_SYNC_FLUSH (default), ZLIB_FULL_FLUSH, ZLIB_FINISH. Normally you will want to set ZLIB_NO_FLUSH to maximize compression, and ZLIB_FINISH to terminate with the last chunk of data. See the » zlib manual for a detailed description of these constants.

返回值

Returns a chunk of uncompressed data, 或者在失败时返回 FALSE.

错误/异常

If invalid parameters are given, inflating the data requires a preset dictionary, but none is specified, the compressed stream is corrupt or has an invalid checksum, an error of level E_WARNING is generated.

参见

  • inflate_init

inflate_get_read_len

Get number of bytes read so far

说明

int inflate_get_read_len ( resource $resource )

Warning

本函数还未编写文档,仅有参数列表。

参数

resource

返回值

Returns number of bytes read so far 或者在失败时返回 FALSE.

inflate_get_status

Get decompression status

说明

int inflate_get_status ( resource $resource )

Usually returns either ZLIB_OK or ZLIB_STREAM_END.

参数

resource

返回值

Returns decompression status 或者在失败时返回 FALSE.

inflate_init

Initialize an incremental inflate context

说明

resource inflate_init ( int $encoding [, array $options = array() ] )

Initialize an incremental inflate context with the specified encoding.

参数

encoding
One of the ZLIB_ENCODING_* constants.

options
An associative array which may contain the following elements:

level
The compression level in range -1..9; defaults to -1.

memory
The compression memory level in range 1..9; defaults to 8.

window
The zlib window size (logarithmic) in range 8..15; defaults to 15.

strategy
One of ZLIB_FILTERED, ZLIB_HUFFMAN_ONLY, ZLIB_RLE, ZLIB_FIXED or ZLIB_DEFAULT_STRATEGY (the default).

dictionary
A string or an array of strings of the preset dictionary (default: no preset dictionary).

返回值

Returns an inflate context resource (zlib.inflate) on success, 或者在失败时返回 FALSE.

错误/异常

If an invalid encoding or option is passed to options, or the context couldn't be created, an error of level E_WARNING is generated.

参见

  • inflate_add
  • deflate_init

readgzfile

Output a gz-file

说明

int readgzfile ( string $filename [, int $use_include_path = 0 ] )

Reads a file, decompresses it and writes it to standard output.

readgzfile can be used to read a file which is not in gzip format; in this case readgzfile will directly read from the file without decompression.

参数

filename
The file name. This file will be opened from the filesystem and its contents written to standard output.

use_include_path
You can set this optional parameter to 1, if you want to search for the file in the include_path too.

返回值

Returns the number of (uncompressed) bytes read from the file on success, 或者在失败时返回 FALSE

错误/异常

失败时抛出**E_WARNING**警告。

参见

  • gzpassthru
  • gzfile
  • gzopen

zlib_decode

Uncompress any raw/gzip/zlib encoded data

说明

string zlib_decode ( string $data [, string $max_decoded_len ] )

Uncompress any raw/gzip/zlib encoded data.

Warning

本函数还未编写文档,仅有参数列表。

参数

data

max_decoded_len

返回值

Returns the uncompressed data, 或者在失败时返回 FALSE.

参见

  • zlib_encode

zlib_encode

Compress data with the specified encoding

说明

string zlib_encode ( string $data , int $encoding [, int $level = -1 ] )

Compress data with the specified encoding.

Warning

本函数还未编写文档,仅有参数列表。

参数

data
The data to compress.

encoding
The compression algorithm. Either ZLIB_ENCODING_RAW, ZLIB_ENCODING_DEFLATE or ZLIB_ENCODING_GZIP.

level

返回值

范例

示例 #1 zlib_encode example

<?php
$str = 'hello world';
$enc = zlib_encode($str, ZLIB_ENCODING_DEFLATE);
echo bin2hex($enc);
?>

以上例程会输出:

789ccb48cdc9c95728cf2fca4901001a0b045d

参见

  • zlib_decode

zlib_get_coding_type

Returns the coding type used for output compression

说明

string zlib_get_coding_type ( void )

Returns the coding type used for output compression.

返回值

Possible return values are gzip, deflate, or FALSE.

参见

目录