CakeFest 2024: The Official CakePHP Conference

gzseek

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

gzseekBusca em um ponteiro de arquivo gz

Descrição

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

Define o indicador de posição de arquivo para o ponteiro de arquivo dado no byte de deslocamento dado na sequência de arquivo. Equivalente a chamar (em C) gzseek(zp, offset, SEEK_SET).

Se o arquivo estiver aberto para leitura, esta função é emulada mas pode ser extremamente lenta. Se o arquivo estiver aberto para escrita, apenas buscas para frente são suportadas; gzseek() então comprime uma sequência de zeros até a nova posição inicial.

Parâmetros

stream

O ponteiro de arquivo gz. Ele deve ser válido e deve apontar para um arquivo aberto com sucesso por gzopen().

offset

O deslocamento buscado.

whence

whence values are:

  • SEEK_SET - Define a posição igual a offset bytes.
  • SEEK_CUR - Define a posição para a localização atual mais offset.

Se whence não for especificado, assume-se que é SEEK_SET.

Valor Retornado

Em caso de sucesso, retorna 0; caso contrário, retorna -1. Note que buscar além do EOF não é considerado um erro.

Exemplos

Exemplo #1 Exemplo de gzseek()

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

Veja Também

  • gztell() - Indica a posição de leitura/gravação em um ponteiro para arquivo-gz
  • gzrewind() - Retorna ao início a posição de um ponteiro para um arquivo-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