Library Moduleswift-png 4.4.9LZ77

LZ77

This module contains a pure-Swift implementation of the LZ77 algorithm. It supports the bare deflate (zlib) archive format, as well as the more-popular gzip format.

README.md
import LZ77

Module information

Declarations
52
Symbols
56

Coverage

44.2 percent of the declarations in LZ77 are fully documented3.8 percent of the declarations in LZ77 are indirectly documented51.9 percent of the declarations in LZ77 are completely undocumented

Declarations

50.0 percent of the declarations in LZ77 are initializers, type members, or enum cases23.1 percent of the declarations in LZ77 are instance members25.0 percent of the declarations in LZ77 are structures1.9 percent of the declarations in LZ77 are typealiases

Interfaces

98.1 percent of the declarations in LZ77 are unrestricted1.9 percent of the declarations in LZ77 are underscored
Module stats and coverage details

Gzip compression

The simplest way to compress data is to use the Gzip.archive(bytes:level:hint:) convenience method.

let text:String = "hello barbie"

let archive:[UInt8] = Gzip.archive(
    bytes: [UInt8].init(text.utf8)[...],
    level: 9)
BasicGzip.swift:3

You can extract data from a gzip archive with the Gzip.extract(from:) convenience method.

let utf8:[UInt8] = try Gzip.extract(from: archive[...])
BasicGzip.swift:9

Gzip streaming

High-volume use cases may require streaming data in and out of an archive. You can use the Gzip.Inflator and Gzip.Deflator types to do this.

You inflate gzip streams by pushing buffers to Gzip.Inflator.push(_:) while calling Gzip.Inflator.pull(_:) repeatedly to extract the decompressed data. This method returns nil if the requested amount of data is not yet available.

It is also possible to wait until you have pushed all the buffers and then call Gzip.Inflator.pull once to obtain all of the decompressed data, although in this situation it would make more sense to use Gzip.extract(from:) instead.

var inflator:Gzip.Inflator = .init()
try inflator.push(original[...])

let utf8:[UInt8] = inflator.pull()
let text:String = .init(decoding: utf8, as: Unicode.UTF8.self)
StreamingGzip.swift:22

Inflator methods

Compressing data to a stream follows a similar pattern, except you need to tell Gzip.Deflator.push(_:last:) which input buffer is the final buffer in the stream.

var deflator:Gzip.Deflator = .init(level: 13, exponent: 15, hint: 128 << 10)
    deflator.push(utf8[...], last: true)
StreamingGzip.swift:31

The compressed data comes out of Gzip.Deflator.pull in blocks. Depending on how you are synchronizing the input and output streams, it may also be more performant to call Gzip.Deflator.pop instead, which avoids buffer flushes but is more complicated to synchronize.

let _:Void? = System.File.Destination.open(path: "\(path).txt.gz")
{
    while let part:[UInt8] = deflator.pull()
    {
        $0.write(part)
    }
}
StreamingGzip.swift:35

Deflator methods

Zlib compression

DEFLATE, or “zlib” compression is a nearly-identical form of compression to gzip, which uses the same algorithm but with a different header format. It has slightly less overhead than gzip, but is also less widely supported.

  • enum LZ77

    A namespace for LZ77-related functionality.