Import std.compress.deflate
std.compress.deflate
Deflate, Zlib, and Gzip compression.
Deflate (RFC 1951), Zlib (RFC 1950), and Gzip (RFC 1952), over zlib-ng. Each one's Compress and Decompress work at once, and its Compressor and Decompressor take bytes in turn and give what came out of them so far, for data that comes in parts. Levels go from 0, stored as it is, to 9, the smallest, and are 6 by default. A Gzip Decompress reads members one after another, as gunzip does, and a Gzip Compress writes one member, with no name, time, or OS in its header.
Examples
// Each format's Compressor and Decompressor take bytes in turn.
Local compressor = ZlibCompressor(9)
Local packed = compressor.Add("a part, and ".Bytes)
packed.AddAll(compressor.Add("the rest".Bytes))
packed.AddAll(compressor.Finish())
Local decompressor = ZlibDecompressor()
Local text = Try decompressor.Add(packed)
text.AddAll(Try decompressor.Finish())
Expect(String.FromUtf8(text) == "a part, and the rest")
Local raw = DeflateCompressor()
Local deflated = raw.Add("raw".Bytes)
deflated.AddAll(raw.Finish())
Local inflate = DeflateDecompressor()
Local back = Try inflate.Add(deflated)
back.AddAll(Try inflate.Finish())
Expect(back == "raw".Bytes)std/compress/deflate.fb
Types
Deflate
struct
Public Struct Deflate
Deflate's raw bytes, which zip entries and PNG's data are in.
Examples
Local data = "hello, hello, hello, hello".Repeated(100).Bytes
Local packed = Deflate.Compress(data)
Expect(packed.Count < data.Count)
Local unpacked = Try Deflate.Decompress(packed)
Expect(unpacked == data)std/compress/deflate.fb, line 64
-
@PanicsAtCaller Public Static Method Compress(data: UInt8[], level: Int = 6): UInt8[]static method #The
data, compressed at thelevel. Panics when thelevelisn't from 0 to 9. -
@PanicsAtCaller Public Static Method Decompress(data: UInt8[], limit: Int = MOST_BYTES): UInt8[] Throws IOErrorstatic method #The bytes the
datais compressed from, or an IOError when it isn't Deflate's, ends early, or would come to more thanlimitbytes. Panics when thelimitis negative.
Zlib
struct
Public Struct Zlib
Zlib's bytes: Deflate's, with a header and an Adler-32 checksum.
Examples
Local data = "hello, hello, hello, hello".Repeated(100).Bytes
Local packed = Zlib.Compress(data)
Expect(packed.Count < data.Count)
Local unpacked = Try Zlib.Decompress(packed)
Expect(unpacked == data)std/compress/deflate.fb, line 96
-
@PanicsAtCaller Public Static Method Compress(data: UInt8[], level: Int = 6): UInt8[]static method #The
data, compressed at thelevel. Panics when thelevelisn't from 0 to 9. -
@PanicsAtCaller Public Static Method Decompress(data: UInt8[], limit: Int = MOST_BYTES): UInt8[] Throws IOErrorstatic method #The bytes the
datais compressed from, or an IOError when it isn't Zlib's, ends early, or would come to more thanlimitbytes. Panics when thelimitis negative.
Gzip
struct
Public Struct Gzip
Gzip's bytes, of .gz files and HTTP's gzip: Deflate's, with a header and a CRC-32 checksum.
Examples
Local data = "the same words, the same words, the same words".Bytes
Local gz = Gzip.Compress(data, 9)
Expect(gz[0] == 0x1F && gz[1] == 0x8B)
Local unpacked = Try Gzip.Decompress(gz)
Expect(unpacked == data)std/compress/deflate.fb, line 127
-
@PanicsAtCaller Public Static Method Compress(data: UInt8[], level: Int = 6): UInt8[]static method #The
data, compressed at thelevel, as one member. Panics when thelevelisn't from 0 to 9. -
@PanicsAtCaller Public Static Method Decompress(data: UInt8[], limit: Int = MOST_BYTES): UInt8[] Throws IOErrorstatic method #The bytes the
datais compressed from, each member's after the last's, or an IOError when it isn't Gzip's, ends early, or would come to more thanlimitbytes. Panics when thelimitis negative.
DeflateCompressor
class
Public Class DeflateCompressor Implements Coder
Deflate's bytes made in turn: Add gives what compressing the bytes it takes has made so far, and Finish what's left, which ends them, and starts again.
std/compress/deflate.fb, line 151
-
A new DeflateCompressor at the
level. Panics when it isn't from 0 to 9. -
Compresses
data, and gives the Deflate bytes it has made so far, which can be none, as it holds some back to compress with what comes next. -
The rest of the bytes, which end the stream, and starts again, for another.
ZlibCompressor
class
Public Class ZlibCompressor Implements Coder
Zlib's bytes made in turn, as DeflateCompressor makes Deflate's.
std/compress/deflate.fb, line 172
-
A new ZlibCompressor at the
level. Panics when it isn't from 0 to 9. -
Compresses
data, and gives the Zlib bytes it has made so far, which can be none, as it holds some back to compress with what comes next. -
The rest of the bytes, which end the stream, and starts again, for another.
GzipCompressor
class
Public Class GzipCompressor Implements Coder
Gzip's bytes made in turn, as DeflateCompressor makes Deflate's, each Finish ending a member.
Examples
Local compressor = GzipCompressor()
Local gz = compressor.Add("first part, ".Bytes)
gz.AddAll(compressor.Add("second part".Bytes))
gz.AddAll(compressor.Finish())
Expect(String.FromUtf8(Try Gzip.Decompress(gz)) == "first part, second part")std/compress/deflate.fb, line 205
-
A new GzipCompressor at the
level. Panics when it isn't from 0 to 9. -
Compresses
data, and gives the Gzip bytes it has made so far, which can be none, as it holds some back to compress with what comes next. -
The rest of the bytes, which end the member, and starts again, for another.
DeflateDecompressor
class
Public Class DeflateDecompressor Implements Coder
Deflate's bytes taken in turn: Add gives what the bytes it takes decompress to so far, and Finish what's left once they've all been added, and starts again. Each throws an IOError when the bytes aren't Deflate's or would come to more than the limit, and Finish when they end early. Bytes after the end are an IOError too.
std/compress/deflate.fb, line 229
-
A new DeflateDecompressor that gives at most
limitbytes. Panics when thelimitis negative. -
Decompresses
data, and gives the bytes it comes to so far. Throws an IOError when the bytes aren't Deflate's, or would come to more than the limit. -
The rest of the bytes, once every one is added, and starts again. Throws an IOError when the stream ended early.
ZlibDecompressor
class
Public Class ZlibDecompressor Implements Coder
Zlib's bytes taken in turn, as DeflateDecompressor takes Deflate's.
std/compress/deflate.fb, line 260
-
A new ZlibDecompressor that gives at most
limitbytes. Panics when thelimitis negative. -
Decompresses
data, and gives the bytes it comes to so far. Throws an IOError when the bytes aren't Zlib's, or would come to more than the limit. -
The rest of the bytes, once every one is added, and starts again. Throws an IOError when the stream ended early.
GzipDecompressor
class
Public Class GzipDecompressor Implements Coder
Gzip's bytes taken in turn, as DeflateDecompressor takes Deflate's, a member after another.
Examples
Local gz = Gzip.Compress("in two halves".Bytes)
Local decompressor = GzipDecompressor()
Local half = gz.Count / 2
Local text = Try decompressor.Add(gz[0..half])
text.AddAll(Try decompressor.Add(gz[half..gz.Count]))
text.AddAll(Try decompressor.Finish())
Expect(String.FromUtf8(text) == "in two halves")std/compress/deflate.fb, line 305
-
A new GzipDecompressor that gives at most
limitbytes. Panics when thelimitis negative. -
Decompresses
data, and gives the bytes it comes to so far. Throws an IOError when the bytes aren't Gzip's, or would come to more than the limit. -
The rest of the bytes, once every one is added, and starts again. Throws an IOError when the stream ended early.
Report an issue with this page or with FutureBlitz on GitHub.