API Reference
Module
GZip.GZip — Module
GZip.jl: A Julia interface for gzip functions in zlib
This module provides a wrapper for the gzip related functions of (zlib), a free, general-purpose, legally unencumbered, lossless data-compression library. These functions allow the reading and writing of gzip files.
Defaults to the zlib-ng backend for faster compression and decompression. Use backend=GZip.ZLIB to use standard zlib instead. Files are cross-compatible between backends.
Notes
- This interface is only for gzipped files, not the streaming zlib compression interface. Internally, it depends on/uses the streaming interface, but the gzip related functions are higher level functions pertaining to gzip files only.
GZipStreamis an implementation ofIOand can be used virtually anywhereIOis used.- This implementation mimics the
IOStreamimplementation, and should be a drop-in replacement forIOStream, with some caveats:seekendandtruncateare not availablereaduntilis available, but is not very efficient. (Butreadlineworks fine.)
In addition to open, gzopen, and gzdopen, the following IO/IOStream functions are supported:
close()flush()seek()skip()position()eof()read()readuntil()readline()write()peek()isreadable()iswritable()
gzheader reads gzip header metadata without decompressing.
Due to limitations in zlib, seekend and truncate are not available.
Opening and Closing
GZip.gzopen — Function
gzopen(fname::AbstractString, [gzmode::AbstractString, buf_size::Integer]; backend=ZLIBNG)::GZipStreamOpens a file with mode (default "r"), setting internal buffer size to buf_size (default Z_DEFAULT_BUFSIZE=8192), and returns a the file as a GZipStream.
gzmode must contain one of:
| mode | Description |
|---|---|
| r | read |
| w | write, create, truncate |
| a | write, create, append |
In addition, gzmode may also contain
| mode | Description |
|---|---|
| x | create the file exclusively (fails if file exists) |
| 0-9 | compression level |
and/or a compression strategy:
| mode | Description |
|---|---|
| f | filtered data |
| h | Huffman-only compression |
| R | run-length encoding |
| F | fixed code compression |
Note that + is not allowed in gzmode. If an error occurs, gzopen throws a GZError.
Use backend=GZip.ZLIB to use the standard zlib backend instead of the default zlib-ng.
GZip.gzdopen — Function
gzdopen(fd, [gzmode, buf_size]; backend=ZLIBNG)Create a GZipStream object from an integer file descriptor. See gzopen for gzmode and buf_size descriptions.
Header Metadata
GZip.gzheader — Function
gzheader(filename::AbstractString) -> GZipHeaderRead the gzip header metadata from a .gz file without decompressing. Returns a GZipHeader struct with modification time, original filename, comment, OS identifier, and extra field data.
h = gzheader("data.gz")
h.name # original filename, or nothing
h.mtime # modification time as Unix timestamp
h.comment # file comment, or nothingGZip.GZipHeader — Type
GZipHeaderGzip file header metadata (RFC 1952).
Fields:
mtime::UInt32— modification time as Unix timestamp (0 = not set)os::UInt8— operating system identifierxfl::UInt8— extra flags (2 = best compression, 4 = fastest)name::Union{String,Nothing}— original filenamecomment::Union{String,Nothing}— file commentextra::Union{Vector{UInt8},Nothing}— extra field datais_text::Bool— hint that content is ASCII text
Low-level I/O
GZip.gzgetc — Function
gzgetc(s::GZipStream)Read a single byte from the stream. Throws EOFError at end of file.
GZip.gzgets — Function
gzgets(s::GZipStream, buf)Read a line from the stream into buf, stopping at newline or end of file.
GZip.gzread — Function
gzread(s::GZipStream, p::Ptr, len::Integer)Read up to len bytes from the stream into the buffer at pointer p. Returns the number of bytes read. Uses gzfread which supports >4GB reads on 64-bit systems.
GZip.gzungetc — Function
gzungetc(c::Integer, s::GZipStream)Push a byte back onto the stream for subsequent reading.
GZip.gzputc — Function
gzputc(s::GZipStream, c::Integer)Write a single byte to the stream.
GZip.gzwrite — Function
gzwrite(s::GZipStream, p::Ptr, len::Integer)Write len bytes from pointer p to the stream. Returns the number of bytes written. Uses gzfwrite which supports >4GB writes on 64-bit systems.
GZip.gzbuffer — Function
gzbuffer(backend::GZBackend, gz_file, gz_buf_size::Integer)Set the internal buffer size for the gzip file. Must be called before any read or write.
Types
GZip.GZipStream — Type
GZipStream <: IO
GZipStream(name, gz_file, [buf_size]; backend=ZLIBNG)Subtype of IO which wraps a gzip stream. Returned by gzopen and gzdopen. Parameterized by the backend (ZlibBackend or ZlibNGBackend).
GZip.GZBackend — Type
GZBackendAbstract type for gzip compression backends. Concrete subtypes are ZlibBackend and ZlibNGBackend.
GZip.ZlibBackend — Type
ZlibBackend <: GZBackendStandard zlib backend (Zlib_jll). Use backend=GZip.ZLIB to select.
GZip.ZlibNGBackend — Type
ZlibNGBackend <: GZBackendDefault backend using zlib-ng (ZlibNG_jll), a high-performance fork of zlib.
GZip.ZLIBNG — Constant
ZLIBNGThe default zlib-ng backend. Pass as backend=GZip.ZLIBNG to gzopen/gzdopen.
GZip.ZFileOffset — Type
ZFileOffsetInteger type used for file offsets in zlib, determined from the library's compile flags.
Library Versions
GZip.libversion — Function
libversion(backend::GZBackend) -> String
libversion(s::GZipStream) -> StringVersion string of the compression library behind backend, or behind the backend s was opened with.
julia> GZip.libversion(GZip.ZLIBNG) == GZip.GZLIBNG_VERSION
trueGZip.GZLIB_VERSION — Constant
GZLIB_VERSIONVersion string of the zlib library in use, e.g. "1.2.13".
This describes the ZLIB backend only. For the version behind the default zlib-ng backend see GZLIBNG_VERSION, or use libversion to ask a particular backend or stream.
GZip.GZLIBNG_VERSION — Constant
GZLIBNG_VERSIONVersion string of the zlib-ng library in use, e.g. "2.3.3".
zlib-ng carries its own version series, unrelated to zlib's; the default ZLIBNG backend is the one this describes.
GZip.ZLIB_VERSION — Constant
ZLIB_VERSIONVersion of the zlib library as a tuple of integers, e.g. (1, 2, 13), for comparisons such as ZLIB_VERSION >= (1, 2, 4).
GZip.ZLIBNG_VERSION — Constant
ZLIBNG_VERSIONVersion of the zlib-ng library as a tuple of integers, e.g. (2, 3, 3).
Errors
GZip.GZError — Type
GZError <: Exceptiongzip error number and string. Possible error values:
| Error number | String |
|---|---|
Z_OK | No error |
Z_ERRNO | Filesystem error (consult errno()) |
Z_STREAM_ERROR | Inconsistent stream state |
Z_DATA_ERROR | Compressed data error |
Z_MEM_ERROR | Out of memory |
Z_BUF_ERROR | Input buffer full/output buffer empty |
Z_VERSION_ERROR | zlib library version is incompatible with caller version |
GZip.ZError — Type
ZError <: Exceptionzlib error, containing an error code and message string.