ZSTD (.zst)
Background & Context
-
- MIME type: application/zstdZstandard (zstd) compression method and file format.
- General-purpose lossless compression method.
- Commonly used for archiving data and for exchanging files on the internet.
- Developed by Yann Collet at Facebook in 2015.
- Binary format.
- Stores a single file.
Import & Export
- Import["file.zst"] uncompresses file.zst and imports file, automatically inferring the format from the file contents.
- Import["file.ext.zst",elem] imports the specified element from the uncompressed file "file.zst".
- Import["file.ext.zst","format"] uncompresses a GZIP file and imports "file.zst" as "format".
- Import["file.ext.zst",{"format",elements}] imports elements from "file.zst" as "format".
- Import["file.ext.zst",{elem,suba,subb,…}] imports a subelement.
- The import format can be explicitly specified with Import["file.zst",{"Zstandard",…}].
- Export["file.ext.zst",expr] exports any expression expr to a Zstandard-compressed file whose format is implied by the file name extension .ext.
- The export format and Zstandard compression method can be explicitly specified with Export["file",expr,{"Zstandard","format"}].
- 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
- The Zstandard format itself does not support Import or Export elements.
- Import elements of the Zstandard-compressed file:
-
"Elements" list of elements and options available in this file "Summary" summary of the file "Rules" list of rules for all available elements "Options" rules for options, properties and settings - Import["file.ext.zst","Elements"] returns the names of elements available in file.ext, effectively unpacking the compressed file before importing "file.ext".
Options
- General Export option:
-
CompressionLevel Automatic Zstandard 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 (2)
Export a string to a ZSTD-compressed text file:
ExportString[ "A System for Doing Mathematics by Computer", {"ZSTD", "Text"} ]Convert the previous output back to a Wolfram Language string:
ImportString[%]This exports graphics to a ZSTD-compressed JPEG file:
Export["cat.jpg.zst", \!\(\*GraphicsBox[«3»]\)]This gives the available Import elements of the JPEG file:
Import["cat.jpg.zst", "Elements"]When importing any element from a .zst file, the Wolfram Language automatically uncompresses it:
Import["cat.jpg.zst", "ImageSize"]Export Options (2)
CompressionLevel (2)
By default, the maximum amount of compression is used:
ExportString[\!\(\*GraphicsBox[«3»]\), { "ZSTD", "PNG"}]//ByteCountThis is equivalent to CompressionLevel1:
ExportString[\!\(\*GraphicsBox[«3»]\), {"ZSTD", "PNG"}, CompressionLevel -> 1]//ByteCountSpecify the minimum amount of compression:
ExportString[\!\(\*GraphicsBox[«3»]\), { "ZSTD", "PNG"}, CompressionLevel -> 0]//ByteCountPlot file size over increasing compression level:
text = ExampleData[{"Text", "AliceInWonderland"}];
Table[{l, ByteCount[ExportString[text, { "ZSTD", "Text"}, CompressionLevel -> l]]}, {l, 0, 1, .1}]//ListLinePlotProperties & Relations (2)
data = StringJoin[ConstantArray[ExampleData[{"Text", "AliceInWonderland"}], 5]];Compare GZIP, BZIP2 and ZSTD compression to the uncompressed size:
formats = {"Text", "GZIP", "BZIP2", "ZSTD"};
BarChart[ByteCount[ExportString[data, #]]& /@ formats, ...]First[AbsoluteTiming[ExportString[data, #]]]& /@ formatsCompare GZIP, BZIP2 and ZSTD compression speeds:
formats = {"GZIP", "BZIP2", "ZSTD"};
BarChart[Round[First[AbsoluteTiming[ExportString[data, #]]], .001]& /@ formats, ...]The amount of compression depends on the data being compressed. For example, JPEG images are already compressed and cannot be compressed any further by GZIP:
ByteCount[ExportString[\!\(\*GraphicsBox[«3»]\), "JPEG"]]ByteCount[ExportString[\!\(\*GraphicsBox[«4»]\), {"ZSTD", "JPEG"}]]Related Guides
History
Introduced in 2020 (12.2)