Newer Version Available
ZipWriter Class
Namespace
Example
This sample code compresses email attachments into a single file.
1ZipWriter writer = new ZipWriter();
2
3List<id> contentDocumentIds = new List<id>();
4// Add IDs of documents to be compressed to contentDocumentIds
5
6for ( ContentVersion cv : [SELECT PathOnClient, Versiondata
7 FROM ContentVersion
8 WHERE ContentDocumentId IN :contentDocumentIds]) {
9 writer.addEntry(cv.PathOnClient, cv.versiondata);
10}
11
12blob zipAttachment = writer.getArchive();
13
14Messaging.EmailFileAttachment efa1 = new Messaging.EmailFileAttachment();
15efa.setFileName('attachments.zip');
16efa.setBody(zipAttachment);
17fileAttachments.add(efa);
18
19Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
20// Set all the other email fields, like addresses, subject and body
21
22email.setEntityAttachments(fileAttachments);
23
24Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
25ZipWriter Methods
The following are methods for ZipWriter.
addEntry(name, data)
Signature
public Compression.ZipEntry addEntry(string name, blob data)
Parameters
addEntry(prototype)
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
getArchive()
Signature
public blob getArchive()
getEntries()
Signature
public List<Compression.ZipEntry> getEntries()
Return Value
Type: List<Compression.ZipEntry>
getEntry(name)
Signature
public compression.ZipEntry getEntry(string name)
Parameters
- name
- Type: string
- Name of the zip entry to be retrieved.
Return Value
Type: Compression.ZipEntry
getLevel()
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()
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)
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)
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)
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.