ZipWriter Class
Namespace
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 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.