CakeFest 2024: The Official CakePHP Conference

gzseek

(PHP 4, PHP 5, PHP 7, PHP 8)

gzseekgz ファイルポインタの位置を移動する

説明

gzseek(resource $stream, int $offset, int $whence = SEEK_SET): int

与えられたファイルポインタが指すファイルのファイル位置記述子を、 ファイルストリーム上の与えられた offset バイトにセットします。 gzseek(zp, offset, SEEK_SET) を (C 言語において) コールするのと同じです。

ファイルが読み込み用にオープンされた場合、この関数は、 エミュレーションされますが、極端に遅くなる可能性があります。 ファイルを書き込み用にオープンした場合、 前方への移動のみがサポートされます。この場合、gzseek() は、新しい開始位置までゼロの並びのデータを圧縮します。

パラメータ

stream

gz ファイルポインタを指定します。これは有効なファイルポインタであり、 かつ、gzopen() によりオープンできたファイルを指している必要があります。

offset

移動するオフセットを指定します。

whence

whence の値は次のいずれかです。

  • SEEK_SET - offset バイト目に設定します。
  • SEEK_CUR - 現在位置から offset ぶん進んだ位置に設定します。

whence を省略した場合は SEEK_SET とみなします。

戻り値

成功した場合、0を返します。それ以外の場合は、-1を返します。 移動がEOFを超える場合にもエラーは発生しないことに注意してください。

例1 gzseek() の例

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

参考

  • gztell() - gz ファイルポインタの読み込み/書き込み位置を返します
  • gzrewind() - gz ファイルポインタの示す位置を元に戻す

add a note

User Contributed Notes 2 notes

up
0
liuhaifeng at example dot com
11 years ago
Since seek after the end is not considered an error, I doubt that "while (gzseek ($fh, $eof) == 0) $eof += $d;" will get into infinite loop.
up
0
dperham at wgate dot com
18 years ago
PHP/4.3.9
contrary to the notes, gzseek() returns -1 if I try to seek past the end of the file. here is a function that will return the last seekable position, and put the file pointer there.

/** sets the file pointer at the end of the file
* and returns the number of bytes in the file.
*/
function gzend($fh)
{
$d = 1<<14;
$eof = $d;
while ( gzseek($fh, $eof) == 0 ) $eof += $d;
while ( $d > 1 )
{
$d >>= 1;
$eof += $d * (gzseek($fh, $eof)? -1 : 1);
}
return $eof;
}
To Top