ZipWriter Class

Contains methods to add zip entries, generate a zipped archive, and return the result as an Apex blob.

Compression

This sample code compresses email attachments into a single file.

The following are constructors for ZipWriter.

  • ZipWriter()

    Creates a new instance of the ZipWriter class.

Creates a new instance of the ZipWriter class.

global ZipWriter()

The following are methods for ZipWriter.

Adds an entry to the zip file with the specified name and content.

public Compression.ZipEntry addEntry(string name, blob data)

  • name: Type: string The name of the zip entry.
  • data: Type: blob The content of the zip entry.

Type: Compression.ZipEntry

Zip entry added to the zip file.

Adds a copy of the specified prototype entry to the zip file and includes details such as the zip entry name, comment, last modification time, and content.

public Compression.ZipEntry addEntry(compression.ZipEntry prototype)

Type: Compression.ZipEntry

Adds an entry to the zip file with the specified name, comment, last modification time, compression method, and content.

public Compression.ZipEntry addEntry(String name, String comment, Datetime modTime, Compression.Method method, Blob data)

  • name: Type: String The name of the zip entry.
  • comment: Type: String The comment about the zip entry.
  • modTime: Type: Datetime The last modification timestamp of the zip entry.
  • method: Type: Compression.Method The compression method of the zip entry, which is either DEFLATED or STORED.
  • data: Type: Blob The content of the zip entry.

Type: Compression.ZipEntry

Zip entry added to the zip file.

Compresses the zip entries and generates a ZIP archive.

public blob getArchive()

Type: blob

Apex blob that contains the bytes of the compression operation.

Gets a list of all the entries in the zip file.

public List<Compression.ZipEntry> getEntries()

Type: List<Compression.ZipEntry>

Gets the entry with the specified name from the zip file.

public compression.ZipEntry getEntry(string name)

  • name: Type: string Name of the zip entry to be retrieved.

Type: Compression.ZipEntry

Gets a set of all the zip entry names in the zip file.

public Set<String> getEntryNames()

Type: Set<String>

Gets the compression level of the zip file.

public Compression.Level getLevel()

Type: Compression.Level

Uses the Level enum values to indicate the compression level as BEST_COMPRESSION, BEST_SPEED, DEFAULT_LEVEL, or NO_COMPRESSION.

Gets the compression method of the zip file.

public Compression.Method getMethod()

Type: Compression.Method

Uses the Method enum values to indicate the compression method as DEFLATED or STORED.

Removes the entry with the specified name from the zip file.

public Void removeEntry(string name)

  • name: Type: string Name of the zip entry to be removed. If an entry with this name isn’t found, the method throws a ZipException exception.

Type: Void

Sets the compression level of the zip file.

public Compression.ZipWriter setLevel(compression.Level value)

Type: Compression.ZipWriter

Returns the zip file set with the specified compression level.

Sets the compression method for the zip file.

public Compression.ZipWriter setMethod(compression.Method value)

Type: Compression.ZipWriter

Returns the zip file set with the specified compression method.