Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.
ZipWriter Class
Namespace
Example
This sample code compresses email attachments into a single file.
1Compression.ZipWriter writer = new Compression.ZipWriter();
2
3List<id> contentDocumentIds = new List<id>();
4
5// Add IDs of documents to be compressed to contentDocumentIds
6
7for ( ContentVersion cv : [SELECT PathOnClient, Versiondata
8 FROM ContentVersion
9 WHERE ContentDocumentId IN :contentDocumentIds])
10{
11 writer.addEntry(cv.PathOnClient, cv.versiondata);
12}
13
14blob zipAttachment = writer.getArchive();
15
16Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
17efa.setFileName('attachments.zip');
18efa.setBody(zipAttachment);
19
20List<Messaging.EmailFileAttachment> fileAttachments = new List<Messaging.EmailFileAttachment>();
21fileAttachments.add(efa);
22
23Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
24
25// Set all the other email fields, such as addresses, subject, and body
26
27email.setFileAttachments(fileAttachments);
28
29Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
30ZipWriter 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
addEntry(name, comment, modTime, method, data)
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.
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
getEntryNames()
Signature
public Set<String> getEntryNames()
Return Value
Type: Set<String>
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.