folkertdev / elm-flate / Flate

An implementation of DEFLATE compression.

The deflate format is used in common file formats like gzip, png, and woff.

import Bytes exposing (Bytes)
import Bytes.Encode as Encode
import Bytes.Decode as Decode

text : String
text =
    "Dyn flam dôve nea"

decodeAsString : Bytes -> Maybe String
decodeAsString buffer =
    let
        decoder = Decode.string (Bytes.width buffer)
    in
        Decode.decode decoder buffer

inflate (deflate (Encode.encode (Encode.string text)))
    |> Maybe.andThen decodeAsString
    --> Just "Dyn flam dôve nea"

deflate : Bytes -> Bytes

Deflate a sequence of bytes. This is an alias for:

deflateWithOptions
    (Dynamic (WithWindowSize LZ77.maxWindowSize))

That means good compression, but can be slow for large data. The README elaborates on deflate performance. deflateWithOptions allows you to pick different deflate options.

inflate : Bytes -> Maybe Bytes

Inflate (decode) data encoded with DEFLATE

GZip

deflateGZip : Bytes -> Bytes

Deflate with the gzip format

Note: the gzip header and trailer are not customizable in the current version.

inflateGZip : Bytes -> Maybe Bytes

Inflate data compressed with gzip. gzip adds some extra data at the front and the back. This decoder will take care of that and also check the checksum if specified.

Note: this only gives back the inflated data block. The header and trailer parts are discarded, but checksums (if specified) will be checked.

Zlib

deflateZlib : Bytes -> Bytes

Deflate with the zlib format

Note: the zlib header and trailer are not customizable in the current version.

inflateZlib : Bytes -> Maybe Bytes

Inflate data compressed with zlib. zlib adds some extra data at the front and the back. This decoder will take care of that and also check the checksum if specified.

Note: this only gives back the inflated data block. The header and trailer parts are discarded, but checksums (if specified) are checked.

Lowlevel Primitives

deflateWithOptions : Encoding -> Bytes -> Bytes

Deflate a sequence of bytes

import LZ77
import Bytes

data : Bytes.Bytes
data =
    Encode.encode (Encode.string "foo")

deflateWithOptions Raw data
    --> [1,3,0,252,-1,102,111,111]


deflateWithOptions (Static NoCompression) data
    --> [75,203,207,7,0]


deflateWithOptions (Dynamic (WithWindowSize LZ77.maxWindowSize)) data
    --> [5,192,33,1,0,0,0,128,-96,183,86,254,55,137,1]

deflateGZipWithOptions : Encoding -> Bytes -> Bytes

deflateZlibWithOptions : Encoding -> Bytes -> Bytes


type Encoding
    = Raw
    | Dynamic Compression
    | Static Compression

The type of encoding used

default: Dynamic


type Compression
    = NoCompression
    | WithWindowSize Basics.Int

The type of compression used

default: WithWindowSize LZ77.maxWindowSize

Checksums

Checksums are simple hashes that are used to make check that the decoded data is the same as the encoded data. The gzip and zlib formats calculate the checksum over the data they encode, and put the value into the output. When decoding, the checksum is also calculated for the decoded data. The two values must be the same.

adler32 : Bytes -> Basics.Int

The adler32 checksum. Used in zlib. Faster than crc32, but also less reliable (larger chance of collisions).

crc32 : Bytes -> Basics.Int

The crc32 checksum. Used in gzip. Slower than adler32, but also more reliable (smaller chance of collisions).