gzip
is a file format and a software application used for file compression and decompression.
gzip
is based on the DEFLATE algorithm, which is a combination of LZ77 and Huffman coding. DEFLATE was intended as a replacement for LZW and other patent-encumbered data compression algorithms which, at the time, limited the usability of compress and other popular archivers.
"gzip
" is often also used to refer to the gzip
file format, which is:
- a 10-byte header, containing a magic number (1f 8b), compression ID (08 for DEFLATE), file flags, a 32-bit timestamp, compression flags and operating system ID.
- optional extra headers denoted by file flags, such as the original filename
- a body, containing a DEFLATE-compressed payload
- an 8-byte footer, containing a CRC-32 checksum and the length of the original uncompressed data, modulo 2^{32}
gzip
is not to be confused with the ZIP archive format, which also uses DEFLATE. The ZIP format can hold collections of files without an external archiver, but is less compact than compressed tarballs holding the same data, because it compresses files individually and cannot take advantage of redundancy between files
1 Gzip
This object allows to compress a source stream of data using gzip
compression method and stores compressed result into destination resource.
Allows to read resource types like files or blobs and write compressed data to other files or blobs. Neither source resources nor destination are deleted after compression.
Constructors:
Ax.util.zip.Gzip(JSFile file) | Indicates a file to write as destination resource of compressed data. |
Ax.util.zip.Gzip(JSBlob blob) | Indicates a Blob object as destination resource of compressed data. |
Methods:
void | compress(Object src) | Reads source object parameter, compress using gzip and write result to destination resource indicated in object construction. |
void | close() | Flush data to destination resource and close all resources. |
// Compress /tmp/sample into /tmp/sample.gz let file = new Ax.io.File("/tmp/sample.txt"); file.append("Lorem ipsum dolor sit amet, consectetur adipiscing elit"); var bzip2 = new Ax.util.zip.Bzip2(); var src = new Ax.io.File("/tmp/sample.txt"); var dst = new Ax.io.File("/tmp/sample.gz"); var gzip = new Ax.util.zip.Gzip(dst); gzip.compress(src); gzip.close(); console.log(src + " " + src.length() + " byte(s)"); console.log(dst + " " + dst.length() + " byte(s)");
/etc/services 677972 byte(s)
/tmp/services.gz 186135 byte(s)
Next, an example about how to compress a Blob. This can be used when you have a Blob and want to compress to send it as an e-mail attachment without using temporary files.
// Compress Blob content into another Blob var blob = new Ax.sql.Blob("data.txt"); blob.setContentType("text/plain"); blob.setContent("Hello World"); var blobgz = new Ax.sql.Blob("data.txt.gz"); blobgz.setContentType("application/x-gzip-compressed"); var gzip = new Ax.util.zip.Gzip(blobgz); gzip.compress(blob); gzip.close(); return blobgz
2 Gunzip
Gunzip allows to uncompress a compressed source stream of data using gunzip uncompression method and stores uncompressed result into destination resource.
Allows to read resource types like files or blobs and write uncompressed data to other files or blobs. Neither source resources nor destination are deleted after compression.
Constructors:
Ax.compress.Gunzip(JSFile file) | Initializes an uncompression object with a compressed file as source of compressed data. |
Ax.compress.Gunzip(JSBlob blob) | Indicates a Blob object as source resource of compressed data. |
Methods:
byte[] | uncompress() | Reads source object parameter, uncompress using gunzip and returns uncompressed data as an array of bytes. You can write it to a file or fillup a blob object with this returning data. |
<script> // Decompress /tmp/sample.gz into /tmp/sample.txt var src = new Ax.io.File("/tmp/sample.gz"); var dst = new Ax.io.File("/tmp/sample.tmp"); var gunzip = new Ax.util.zip.Gunzip(src); dst.write(gunzip.uncompress()); console.log(src + " " + src.length() + " byte(s)"); console.log(dst + " " + dst.length() + " byte(s)"); </script>
/tmp/services.gz 186135 byte(s)
/tmp/services.tmp 677972 byte(s)