ZIP (.zip)
Background & Context
-
- Registered MIME type: application/zip
- Popular data compression and archival format.
- Used to reduce storage space and transfer times.
- Binary format.
- Can contain multiple files.
- Supports a variety of compression methods.
- Published by Phil Katz in 1989.
Import & Export
- When importing files from a ZIP archive, the specific converter for each format will be used.
- Import["file.zip"] returns an expression of the form {"fn1", "fn2",…}, giving full path specifications for all files in "file.zip".
- Import["file.zip",elem] imports the specified element from a ZIP file.
- Import["file.zip","fn"] extracts "fn" from the archive and imports it.
- Import["file.zip",{elem,suba,subb,…}] imports a subelement.
- Import["file.zip",{{elem1,elem2,…}}] imports multiple elements.
- Import["file","ZIP"] or Import["file",{"ZIP",elem,…}] imports any file as a ZIP archive.
- Export["file.zip",expr,elem] creates a ZIP archive by treating expr as specifying element elem.
- Export["file.zip",{expr1,expr2,…},{{elem1,elem2,…}}] treats each expri as specifying the corresponding elemi.
- Export["file.zip","fn"->expr] exports expr to a file and compresses it as a ZIP archive, inferring the file format from the file extension of "fn".
- Export["file.zip","fn1"->expr1,"fn2"->expr2,…] exports multiple expressions to a ZIP file archive.
- See the following reference pages for full general information:
-
Import, Export import from or export to a file CloudImport, CloudExport import from or export to a cloud object ImportString, ExportString import from or export to a string ImportByteArray, ExportByteArray import from or export to a byte array
Import Elements
- General Import elements:
-
"Elements" list of elements and options available in this file "Summary" summary of the file "Rules" list of rules for all available elements - The following can be used to select or specify individual files in a ZIP archive:
-
"FileNames" list of filenames representing the contents of a ZIP archive "filename" a single file "filename","format" a single file, taken to be in the specified format "filename","format",elem element elem from the specified file - Import by default uses the "FileNames" element for the ZIP format.
- Import["file.zip","fn"] extracts and imports file "fn".
- File names can include relative or absolute directory specifications and the abbreviated string patterns supported by StringMatchQ.
- Import["file.zip","*"] imports an entire archive.
- Import["file.zip","dir/*.jpg"] imports all JPEG files from subdirectory dir.
- Export["file.zip","file1.gif"->expr1,"file2.txt"->expr2] creates a ZIP archive containing a GIF and a text file.
- Export["file",{expr1,expr2},{"ZIP",{{"file1","GIF"},{"file2","Text"}}}] is equivalent to the above.
Options
- General Export option:
-
CompressionLevel Automatic ZIP compression strength, given as a number in the range 0 to 1 - The setting CompressionLevel->1 corresponds to the strongest available compression, resulting in the smallest possible file size.
Examples
open all close allBasic Examples (1)
This creates a ZIP archive containing a GIF and a text file:
Export["test.zip", <|"a.gif" -> \!\(\*GraphicsBox[«3»]\), "b.txt" -> "earth seen from space"|>, "Rules" ];Importing a ZIP archive gives the names of the files in the archive by default:
Import["test.zip"]Import all files using their default element:
Import["test.zip", "*"]Extract and import the GIF file:
Import["test.zip", "a.gif"]Extract and import all text files:
Import["test.zip", "*.txt"]Scope (1)
Export["test.zip", <|"a.gif" -> \!\(\*GraphicsBox[«3»]\), "b.fa" -> {"TTTCG", "GATAG"}, "c.col" -> {{0, 1, 1, 1}, {1, 0, 1, 1}, {1, 1, 0, 1}, {1, 1, 1, 0}}|>, "Rules" ];Import["test.zip", "Elements"]Extract available elements for one of the files in the ZIP archive:
Import["test.zip", {"a.gif", "Elements"}]Import some elements from one of the files in the archive:
Import["test.zip", {"a.gif", {"ColorSpace", "Image"}}]Import the default element for multiple files with different formats:
Import["test.zip", "*"]Import Elements (1)
"FileNames" (1)
Get the names of all files in a ZIP archive:
Export["test.zip", <|"a.gif" -> \!\(\*GraphicsBox[«3»]\), "b.txt" -> "earth seen from space"|>, "Rules" ];Import["test.zip", "FileNames"]This is also the default element:
Import["test.zip"]Export Options (1)
CompressionLevel (1)
data = <|"a.txt" -> ExampleData[{"Text", "DeclarationOfIndependence"}], "b.txt" -> ExampleData[{"Text", "AliceInWonderland"}]|>;By default, the maximum amount of compression is used:
ExportString[data, "ZIP"]//ByteCountThis is equivalent to CompressionLevel1:
ExportString[data, "ZIP", CompressionLevel -> 1]//ByteCountSpecify the minimum amount of compression:
ExportString[data, "ZIP", CompressionLevel -> 0]//ByteCountProperties & Relations (2)
data = <|"a.txt" -> StringJoin[ConstantArray[ExampleData[{"Text", "DeclarationOfIndependence"}], 3]], "b.txt" -> StringJoin[ConstantArray[ExampleData[{"Text", "AliceInWonderland"}], 5]]|>;Compare ZIP, TARGZ, TARBZ2 and TARZST compression to the uncompressed size:
formats = {"Text", "ZIP", {"GZIP", "TAR"}, {"BZIP2", "TAR"}, {"ZSTD", "TAR"}};
BarChart[ByteCount[ExportString[data, #]]& /@ formats, ...]Compare ZIP, TARGZ, TARBZ2 and TARZST compression speeds:
formats = {"ZIP", {"GZIP", "TAR"}, {"BZIP2", "TAR"}, {"ZSTD", "TAR"}};
BarChart[Round[First[AbsoluteTiming[ExportString[data, #]]], .001]& /@ formats, ChartLabels -> Placed[{"ZIP", "TARGZ", "TARBZ2", "TARZST"}, Above], LabelingFunction -> Bottom, Ticks -> None]The amount of compression depends on the data being compressed. For example, JPEG images are already compressed and cannot be compressed any further by ZIP:
ByteCount[ExportString[\!\(\*GraphicsBox[«3»]\), "JPEG"]]ByteCount[ExportString[<|"a.jpeg" -> \!\(\*GraphicsBox[«4»]\)|>, "ZIP"]]Related Guides
History
Introduced in 2007 (6.0) | Updated in 2017 (11.2) ▪ 2020 (12.1)