ZIP
is an archive file format that supports lossless data compression. A ZIP
file may contain one or more files or directories that may have been compressed.
The ZIP
file format permits a number of compression algorithms, though DEFLATE is the most common. This format was originally created in 1989 and released to
the public domain on February 14, 1989 by Phil Katz, and was first implemented in PKWARE, Inc.'s PKZIP utility,[2] as a replacement for the previous ARC compression
format by Thom Henderson.
.zip
files are archives that store multiple files. ZIP
allows contained files to be compressed using many different methods, as well as simply storing a file without compressing it.
Each file is stored separately, allowing different files in the same archive to be compressed using different methods. Because the files in a ZIP
archive are compressed individually
it is possible to extract them, or add new ones, without applying compression or decompression to the entire archive. This contrasts with the format of compressed tar files,
for which such random-access processing is not easily possible.
A directory is placed at the end of a ZIP
file. This identifies what files are in the ZIP
and identifies where in the ZIP
that file is located.
This allows ZIP
readers to load the list of files without reading the entire ZIP
archive. ZIP
archives can also include extra data that is not related to the ZIP
archive.
This allows for a ZIP
archive to be made into a self-extracting archive (application that decompresses its contained data), by prepending the program code to a ZIP
archive
and marking the file as executable. Storing the catalog at the end also makes possible hiding a zipped file by appending it to an innocuous file, such as a GIF image file.
The .zip
format uses a 32-bit CRC
algorithm and includes two copies of the directory structure of the archive to provide greater protection against data loss.
1 Zip
This object allows to create a zip
container and add resources compressed in zip
format.
Neither source resources nor destination are deleted after compression.
Constructors:
Ax.util.zip .Zip(JSFile file) |
Indicates a file to write as destination resource of compressed data. |
Ax.util.zip .Zip(JSBlob blob) |
Indicates a Blob object as destination resource of compressed data. |
Methods:
void | zipFile(JSFile src) | Reads src file, compress it and attach to zip container indicated in object construction.
The name of the compressed resource inside the container is getted automaticaly from the name of the source file. |
void | zipFile(JSFile src, String name) | Reads src file, compress it and attach to zip container indicated in object construction.
The name of the compressed resource inside the container is passed as the "name" parameter and can include relative folder names. |
void | zipBlob(JDBCBlob src) | Reads src blob, compress it and attach to zip container indicated in object construction.
The name of the compressed resource inside the container is passed as the "name" parameter and can include relative folder names. |
void | zipBlob(JDBCBlob src, String name) | Reads src blob, compress it and attach to zip container indicated in object construction.
The name of the compressed resource inside the container is passed as the "name" parameter and can include relative folder names. |
void | close() | Flush data to destination resource and close all resources. |
<script> let file1 = new Ax.io.File("/tmp/sample1.txt"); file1.append(`Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua.`); let file2 = new Ax.io.File("/tmp/sample2.txt"); file2.append(`Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat.`); var zip = new Ax.util.zip.Zip(new Ax.io.File("/tmp/ziptest.zip")); zip.zipFile(new Ax.io.File("/tmp/sample1.txt")); zip.zipFile(new Ax.io.File("/tmp/sample2.txt"), "dir/sample2.txt"); zip.close(); var dst = new Ax.io.File("/tmp/ziptest.zip"); console.log(dst + " " + dst.length() + " byte(s)"); </script>
2 Unzip
This object allows to uncompress a zip
container into a destination folder and returns an iterator of the extracted files.
Neither source resources nor destination are deleted after compression.
Constructors:
Ax.util.zip .Unzip (JSBlob blob, JSFile destDir) |
Extract contents from compressed file into destination folder and returns an iterator of uncompressed resulting files |
Ax.util.zip .Unzip (JSFile file, JSFile destDir) |
Extract contents from compressed file into destination folder and returns an iterator of uncompressed resulting files |
<script> var compressed = new Ax.io.File("/tmp/ziptest.zip"); console.log("Uncompressing " + compressed); for (var file of new Ax.util.zip.Unzip(compressed, new Ax.io.File("/tmp/unzipdir"))) { console.log("Unzipped: " + file); } </script>