GZip data format
The
gzipDeflaterdata format compresses the message body to gzip (marshal) and decompresses gzip data (unmarshal).
Name: gzipDeflater · Also: gzip, gzipdeflater · Crate: cameleer-core · Cargo feature: none, always included (build features) ·
Apache Camel: GZip Deflater
When to use it
- Shrink payloads before sending them over slow or metered links, for example from an edge gateway to the cloud.
- Read
.gzfiles, or gzip-compressed messages from Kafka, S3 or HTTP. - Archive logs and exports in a format every tool understands (
gunzip,zcat).
Marshal and unmarshal
| direction | body in | body out |
|---|---|---|
marshal | any body (text is taken as UTF-8 bytes) | gzip bytes (RFC 1952) |
unmarshal | gzip bytes | the decompressed bytes |
Compression uses Cameleer's built-in DEFLATE implementation (no zlib library). Decompression reads
every DEFLATE block type, so files written by gzip, Java or Python decompress fine.
Example
Compress a body, record the compressed size, and decompress it again:
- route:
id: gzip-round-trip
from:
uri: direct:gzip-round-trip
steps:
- marshal:
gzipDeflater: {}
- setHeader:
name: compressedSize
simple: "${body.length()}"
- unmarshal:
gzip: {}
- convertBodyTo:
type: String
- setBody:
simple: "${body} (${header.compressedSize} bytes compressed)"sensor=42;sensor=42;sensor=42;sensor=42;sensor=42;sensor=42;sensor=42;sensor=42sensor=42;sensor=42;sensor=42;sensor=42;sensor=42;sensor=42;sensor=42;sensor=42 (32 bytes compressed)The 79-byte body compresses to 32 bytes, including the 18 bytes of gzip header and trailer.
Rust DSL
b.from("direct:gzip-rust")
.marshal(DataFormatDefinition::gzip().compression_level(9))
.unmarshal(DataFormatDefinition::gzip());Options
| option | default | description |
|---|---|---|
compressionLevel (level) | 6 | 0 stores the data uncompressed; 1-9 compress (higher values search harder). |
Common questions
How do I read a .gz file?
Read it with the file component and unmarshal: {gzip: {}}. Add convertBodyTo: {type: String}
if the content is text.
What is the difference between gzip and zipDeflater?
Both use DEFLATE. gzipDeflater writes the gzip container (.gz files, HTTP
Content-Encoding: gzip); zipDeflater writes the zlib container
(java.util.zip.Deflater). For .zip archives use zipFile.
Does compression make sense for small messages?
Not below about 100 bytes: the gzip header and trailer alone take 18 bytes.
Related
- Zip Deflater data format, Zip File data format, Tar File data format
- Base64 data format to carry compressed data in text
- Apache Camel GZip Deflater data format
Found a problem?
If something on this page doesn't match what Cameleer does, or you hit a bug while building
with it, please help fix it, whether you are a person or a coding agent:
open an issue that describes what you saw,
then fork the repository, fix it, add or update a test, run cargo test --workspace, and open
a pull request. CONTRIBUTING.md
explains the ground rules.