gzip

    MoonBit gzip: provide simple gzip and gunzip algorithms.

    gzip
    gunzip
    algorithm
    moonbit
    Download zip
    Author
    Version
    0.34.13
    License
    Apache-2.0
    Last updated
    3 days ago
    Downloads
    6K

    Dependencies

    #gmlewis/gzip

    check

    This is a simplified gzip/gunzip algorithm based on Go's implementation: https://cs.opensource.google/go/go/+/refs/tags/go1.23.0:src/compress/gzip/gzip.go which has the copyright notice:

    // Copyright 2010 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file.

    #Status

    The code has been updated to support compiler:

    $ moon version --all moon 0.1.20260827 (d0aaa07 2026-08-27) ~/.moon/bin/moon moonc v0.10.11+6ff76a5f9 (2026-08-28) ~/.moon/bin/moonc moonrun 0.1.20260827 (d0aaa07 2026-08-27) ~/.moon/bin/moonrun

    CompressionLevel

    pub(all) struct CompressionLevel(Int) derive(Compare, Eq)

    pub(all) struct Header {
    comment : String
    extra : Array[Byte]
    mod_time :
    PlainDateTime
    ?
    name : String
    os : Byte
    }

    The gzip file stores a header giving metadata about the compressed file. That header is exposed as the fields of the [Writer] and [Reader] structs.

    Strings must be UTF-8 encoded and may only contain Unicode code points U+0001 through U+00FF, due to limitations of the GZIP file format.

    Header::new

    fn Header::new() -> Header

    Reader

    pub(all) struct Reader {
    header : Header
    // private fields
    }

    A Reader is an [IOReadCloser] that can be read to retrieve uncompressed data from a gzip-format compressed file.

    In general, a gzip file can be a concatenation of gzip files, each with its own header. Reads from the Reader return the concatenation of the uncompressed data of each. Only the first header is recorded in the Reader fields.

    Gzip files store a length and checksum of the uncompressed data. The Reader will return an [err_checksum] when [Reader.Read] reaches the end of the uncompressed data if it does not have the expected length or checksum. Clients should treat data returned by [Reader.Read] as tentative until they receive the [ioeof] marking the end of the data.
    impl Closer for Reader
    impl Reader for Reader

    Reader::multistream

    fn Reader::multistream(self : Reader, ok : Bool) -> Unit

    Multistream controls whether the reader supports multistream files.

    If enabled (the default), the [Reader] expects the input to be a sequence of individually gzipped data streams, each with its own header and trailer, ending at EOF. The effect is that the concatenation of a sequence of gzipped files is treated as equivalent to the gzip of the concatenation of the sequence. This is standard behavior for gzip readers.

    Calling Multistream(false) disables this behavior; disabling the behavior can be useful when reading file formats that distinguish individual gzip data streams or mix gzip data streams with other data streams. In this mode, when the [Reader] reaches the end of the data stream, [Reader.Read] returns [ioeof]. The underlying reader must implement [io.ByteReader] in order to be left positioned just after the gzip stream. To start the next stream, call self.Reset(r) followed by self.Multistream(false). If there is no next stream, self.Reset(r) will return [ioeof].

    Reader::new

    NewReader creates a new [IOReader] reading the given reader.

    It is the caller's responsibility to call Close on the [Reader] when done.

    The [Reader.Header] fields will be valid in the [Reader] returned.

    Reader::reset

    Reset discards the [Reader] z's state and makes it equivalent to the result of its original state from [NewReader], but reading from r instead. This permits reusing a [Reader] rather than allocating a new one.

    Writer

    pub(all) struct Writer {
    header : Header
    w : &
    Writer

    level : CompressionLevel
    wrote_header : Bool
    closed : Bool
    buf : Array[Byte]
    compressor : &
    WriteCloser

    digest : UInt
    size : UInt
    err :
    IOError
    ?
    }

    Writes to a Writer are compressed and written to w.
    impl Closer for Writer
    impl Writer for Writer

    Writer::flush

    flush flushes any pending compressed data to the underlying writer.

    It is useful mainly in compressed network protocols, to ensure that a remote reader has enough data to reconstruct a packet. Flush does not return until the data has been written. If the underlying writer returns an error, Flush returns that error.

    In the terminology of the zlib library, Flush is equivalent to Z_SYNC_FLUSH.

    Writer::new

    Writer::new returns a new [IOWriter] using the optional compression level (or default_compression). Writes to the returned writer are compressed and written to w.

    It is the caller's responsibility to call Close on the [Writer] when done. Writes may be buffered and not flushed until Close.

    Callers that wish to set the fields in Writer.Header must do so before the first call to Write, Flush, or Close.

    best_speed

    let best_speed : CompressionLevel

    err_checksum

    let err_checksum :
    IOError

    err_checksum is returned when reading GZIP data that has an invalid checksum.

    err_header

    err_header is returned when reading GZIP data that has an invalid header.

    err_unexpected_eof

    let err_unexpected_eof :
    IOError

    Source Files

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io