ZLIB
Loading...
Searching...
No Matches
gzlib.c File Reference

Functions common to reading and writing gzip files. More...

#include "gzguts.h"

Macros

#define LSEEK   lseek
 

Functions

gzFile ZEXPORT gzopen (const char *path, const char *mode)
 Open the gzip (.gz) file at path for reading and decompressing, or compressing and writing.
 
gzFile ZEXPORT gzopen64 (const char *path, const char *mode)
 
gzFile ZEXPORT gzdopen (int fd, const char *mode)
 Associate a gzFile with the file descriptor fd.
 
int ZEXPORT gzbuffer (gzFile file, unsigned size)
 Set the internal buffer size used by this library's functions for file to size.
 
int ZEXPORT gzrewind (gzFile file)
 Rewind file.
 
z_off64_t ZEXPORT gzseek64 (gzFile file, z_off64_t offset, int whence)
 Set the starting position to offset relative to whence for the next gzread or gzwrite on file.
 
z_off_t ZEXPORT gzseek (gzFile file, z_off_t offset, int whence)
 Set the starting position to offset relative to whence for the next gzread or gzwrite on file.
 
z_off64_t ZEXPORT gztell64 (gzFile file)
 Return the starting position for the next gzread or gzwrite on file.
 
z_off_t ZEXPORT gztell (gzFile file)
 Return the starting position for the next gzread or gzwrite on file.
 
z_off64_t ZEXPORT gzoffset64 (gzFile file)
 Return the current compressed (actual) read or write offset of file.
 
z_off_t ZEXPORT gzoffset (gzFile file)
 Return the current compressed (actual) read or write offset of file.
 
int ZEXPORT gzeof (gzFile file)
 Return true (1) if the end-of-file indicator for file has been set while reading, false (0) otherwise.
 
const char *ZEXPORT gzerror (gzFile file, int *errnum)
 Return the error message for the last error which occurred on file.
 
void ZEXPORT gzclearerr (gzFile file)
 Clear the error and end-of-file flags for file.
 
void ZLIB_INTERNAL gz_error (gz_statep state, int err, const char *msg)
 Create an error message in allocated memory and set state->err and state->msg accordingly.
 
unsigned ZLIB_INTERNAL gz_intmax ()
 

Detailed Description

Functions common to reading and writing gzip files.

Copyright (C) 2004-2019 Mark Adler For conditions of distribution and use, see copyright notice in zlib.h

Function Documentation

◆ gz_error()

void ZLIB_INTERNAL gz_error ( gz_statep  state,
int  err,
const char *  msg 
)

Create an error message in allocated memory and set state->err and state->msg accordingly.

Free any previous error message already there. Do not try to free or allocate space if the error is Z_MEM_ERROR (out of memory). Simply save the error message as a static string. If there is an allocation failure constructing the error message, then convert the error to out of memory.

◆ gzoffset()

z_off_t ZEXPORT gzoffset ( gzFile  file)

Return the current compressed (actual) read or write offset of file.

This offset includes the count of bytes that precede the gzip stream, for example when appending or when using gzdopen() for reading. When reading, the offset does not include as yet unused buffered input. This information can be used for a progress indicator. On error, gzoffset() returns -1.

◆ gzoffset64()

z_off64_t ZEXPORT gzoffset64 ( gzFile  file)

Return the current compressed (actual) read or write offset of file.

This offset includes the count of bytes that precede the gzip stream, for example when appending or when using gzdopen() for reading. When reading, the offset does not include as yet unused buffered input. This information can be used for a progress indicator. On error, gzoffset64() returns -1.

◆ gzopen()

gzFile ZEXPORT gzopen ( const char *  path,
const char *  mode 
)

Open the gzip (.gz) file at path for reading and decompressing, or compressing and writing.

The mode parameter is 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", 'R' for run-length encoding as in "wb1R", or 'F' for fixed code compression as in "wb9F". (See the description of deflateInit2 for more information about the strategy parameter.) 'T' will request transparent writing or appending with no compression and not using the gzip format.

"a" can be used instead of "w" to request that the gzip stream that will be written be appended to the file. "+" will result in an error, since reading and writing to the same gzip file is not supported. The addition of "x" when writing will create the file exclusively, which fails if the file already exists. On systems that support it, the addition of "e" when reading or writing will set the flag to close the file on an execve() call.

These functions, as well as gzip, will read and decode a sequence of gzip streams in a file. The append function of gzopen() can be used to create such a file. (Also see gzflush() for another way to do this.) When appending, gzopen does not test whether the file begins with a gzip stream, nor does it look for the end of the gzip streams to begin appending. gzopen will simply append a gzip stream to the existing file.

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. When reading, this will be detected automatically by looking for the magic two- byte gzip header.

Returns
NULL if the file could not be opened, if there was insufficient memory to allocate the gzFile state, or if an invalid mode was specified (an 'r', 'w', or 'a' was not provided, or '+' was provided). errno can be checked to determine if the reason gzopen failed was that the file could not be opened.

◆ gzseek()

z_off_t ZEXPORT gzseek ( gzFile  file,
z_off_t  offset,
int  whence 
)

Set the starting position to offset relative to whence for the next gzread or gzwrite on file.

The offset represents a number of bytes in the uncompressed data stream. The whence parameter is defined as in lseek(2); the value SEEK_END is not supported.

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.

gzseek returns the resulting offset location as measured in bytes from the beginning of the uncompressed stream, or -1 in case of error, in particular if the file is opened for writing and the new starting position would be before the current position.

◆ gzseek64()

z_off64_t ZEXPORT gzseek64 ( gzFile  file,
z_off64_t  offset,
int  whence 
)

Set the starting position to offset relative to whence for the next gzread or gzwrite on file.

The offset represents a number of bytes in the uncompressed data stream. The whence parameter is defined as in lseek(2); the value SEEK_END is not supported.

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; gzseek64 then compresses a sequence of zeroes up to the new starting position.

Returns
the resulting offset location as measured in bytes from the beginning of the uncompressed stream, or -1 in case of error, in particular if the file is opened for writing and the new starting position would be before the current position.

◆ gztell()

z_off_t ZEXPORT gztell ( gzFile  file)

Return the starting position for the next gzread or gzwrite on file.

This position represents a number of bytes in the uncompressed data stream, and is zero when starting, even if appending or reading a gzip stream from the middle of a file using gzdopen().

gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)

◆ gztell64()

z_off64_t ZEXPORT gztell64 ( gzFile  file)

Return the starting position for the next gzread or gzwrite on file.

This position represents a number of bytes in the uncompressed data stream, and is zero when starting, even if appending or reading a gzip stream from the middle of a file using gzdopen().

gztell64(file) is equivalent to gzseek64(file, 0L, SEEK_CUR)