ZipWriter Class

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

Namespace

Compression

Example

This sample code compresses email attachments into a single file.

Compression.ZipWriter writer = new Compression.ZipWriter();

List<id> contentDocumentIds = new List<id>();

// Add IDs of documents to be compressed to contentDocumentIds 

for ( ContentVersion cv : [SELECT PathOnClient, Versiondata
                           FROM ContentVersion
                           WHERE ContentDocumentId IN :contentDocumentIds]) 
{
      writer.addEntry(cv.PathOnClient, cv.versiondata);
}

blob zipAttachment = writer.getArchive();

Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName('attachments.zip');
efa.setBody(zipAttachment);

List<Messaging.EmailFileAttachment> fileAttachments = new List<Messaging.EmailFileAttachment>();
fileAttachments.add(efa);

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

// Set all the other email fields, such as addresses, subject, and body

email.setFileAttachments(fileAttachments);

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });  

ZipWriter Constructors

The following are constructors for ZipWriter.

ZipWriter()

Creates a new instance of the ZipWriter class.

Signature

global ZipWriter()

ZipWriter Methods

The following are methods for ZipWriter.

addEntry(name, data)

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

Signature

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

Parameters

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

Return Value

Type: Compression.ZipEntry

Zip entry added to the zip file.

addEntry(prototype)

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.

Signature

public Compression.ZipEntry addEntry(compression.ZipEntry prototype)

Parameters

prototype
Type: Compression.ZipEntry
Details of the entry to be added to the zip file.

Return Value

Type: Compression.ZipEntry

addEntry(name, comment, modTime, method, data)

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

Signature

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

Parameters

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.

Return Value

Type: Compression.ZipEntry

Zip entry added to the zip file.

getArchive()

Compresses the zip entries and generates a ZIP archive.

Signature

public blob getArchive()

Return Value

Type: blob

Apex blob that contains the bytes of the compression operation.

getEntries()

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

Signature

public List<Compression.ZipEntry> getEntries()

Return Value

Type: List<Compression.ZipEntry>

getEntry(name)

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

Signature

public compression.ZipEntry getEntry(string name)

Parameters

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

Return Value

Type: Compression.ZipEntry

getEntryNames()

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

Signature

public Set<String> getEntryNames()

Return Value

Type: Set<String>

getLevel()

Gets the compression level of the zip file.

Signature

public Compression.Level getLevel()

Return Value

Type: Compression.Level

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

getMethod()

Gets the compression method of the zip file.

Signature

public Compression.Method getMethod()

Return Value

Type: Compression.Method

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

removeEntry(name)

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

Signature

public Void removeEntry(string name)

Parameters

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.

Return Value

Type: Void

setLevel(level)

Sets the compression level of the zip file.

Signature

public Compression.ZipWriter setLevel(compression.Level value)

Parameters

value
Type: Compression.Level
Uses the Level enum to set the compression level.

Return Value

Type: Compression.ZipWriter

Returns the zip file set with the specified compression level.

setMethod(method)

Sets the compression method for the zip file.

Signature

public Compression.ZipWriter setMethod(compression.Method value)

Parameters

value
Type: Compression.Method
Uses the Method enum to set the compression method.

Return Value

Type: Compression.ZipWriter

Returns the zip file set with the specified compression method.