folkertdev/elm-flate/1.0.1

NOTE: the latest version is 2.0.6

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
    |> ByteArray.fromBytes
    --> Array.fromList [1,3,0,252,-1,102,111,111]




deflateWithOptions (Static NoCompression) data
    |> ByteArray.fromBytes
    --> Array.fromList [75,203,207,7,0]




deflateWithOptions (Dynamic (WithWindowSize LZ77.maxWindowSize)) data
    |> ByteArray.fromBytes
    --> Array.fromList [5,192,33,1,0,0,0,128,-96,183,86,254,55,137,1]
deflateGZipWithOptions : Encoding -> Bytes -> Bytes
deflateZlibWithOptions : Encoding -> Bytes -> Bytes
= Raw
| Dynamic Compression
| Static Compression

The type of encoding used

  • Raw: puts the raw bytes into the output. The output is a bit larger, but it is now a valid zip/gzip/zlib compressed byte stream.
  • Static: uses a default huffman table to shorten the input. Good for very short messages, or messages with a lot of simple repetition, because the table is not included.
  • Dynamic: calculates an optimal huffman table for the specific input it is given. This table must be included in the output, but the result can be much smaller than with the static table.

default: Dynamic

= NoCompression
| WithWindowSize Int

The type of compression used

  • NoCompression: the data will not be compressed. The encoder still can give a smaller file. Use this option if superquick encoding and/or decoding is needed, and output size is less of a concern.
  • WithWindowSize: use LZ77 compression with the given window size.

default: WithWindowSize LZ77.maxWindowSize

Checksums

adler32 : Bytes -> Int

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

crc32 : Bytes -> Int

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