---
schema_version: "1"
id: e84542417fc0da909fbc3eb847fa5e6b073542b953188d40fa8ac6d2c7e64d27
title: ZipWriter Class
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexref/apex_class_compression_zipwriter.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_compression_zipwriter.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: 448c73465aa545c7c80b63dc2c2c2d5ba23dcdd2ae15f06432a7af473b44468f
description: Contains methods to add zip entries, generate a zipped archive, and return the result as an Apex blob.Creates a new instance of the ZipWriter class.Adds an entry to the zip file with the specified name and content.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.Adds an entry to the zip file with the specified name, comment, last modification time, compression method, and content. Compresses the zip entries and generates a ZIP archive.Gets a list of all the entries in the zip file.Gets the entry with the specified name from the zip file.Gets a set of all the zip entry names in the zip file.Gets the compression level of the zip file.Gets the compression method of the zip file.Removes the entry with the specified name from the zip file.Sets the compression level of the zip file.Sets the compression method for the zip file.
---
# ZipWriter Class

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

## Namespace

[Compression](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_namespace_compression.htm "The Compression namespace provides classes and methods to create and extract zip files.")

## 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 
                           WITH USER\_MODE\]) 
{
      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](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_compression_zipwriter.htm#apex_compression_zipwriter_constructors)**
- **[ZipWriter Methods](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_compression_zipwriter.htm#apex_compression_zipwriter_methods)**

## ZipWriter Constructors

The following are constructors for ZipWriter.

- **[ZipWriter()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_compression_zipwriter.htm#apex_compression_zipwriter_ctor)**  
    Creates a new instance of the ZipWriter class.

### ZipWriter()

Creates a new instance of the ZipWriter class.

#### Signature

global ZipWriter()

## ZipWriter Methods

The following are methods for ZipWriter.

- **[addEntry(name, data)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_compression_zipwriter.htm#apex_compression_zipwriter_addEntry)**  
    Adds an entry to the zip file with the specified name and content.
- **[addEntry(prototype)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_compression_zipwriter.htm#apex_compression_zipwriter_addEntry_2)**  
    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.
- **[addEntry(name, comment, modTime, method, data)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_compression_zipwriter.htm#apex_compression_zipwriter_addEntry_3)**  
    Adds an entry to the zip file with the specified name, comment, last modification time, compression method, and content.
- **[getArchive()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_compression_zipwriter.htm#apex_compression_zipwriter_getArchive)**  
    Compresses the zip entries and generates a ZIP archive.
- **[getEntries()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_compression_zipwriter.htm#apex_compression_zipwriter_getEntries)**  
    Gets a list of all the entries in the zip file.
- **[getEntry(name)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_compression_zipwriter.htm#apex_compression_zipwriter_getEntry)**  
    Gets the entry with the specified name from the zip file.
- **[getEntryNames()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_compression_zipwriter.htm#apex_compression_zipwriter_getEntryNames)**  
    Gets a set of all the zip entry names in the zip file.
- **[getLevel()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_compression_zipwriter.htm#apex_compression_zipwriter_getLevel)**  
    Gets the compression level of the zip file.
- **[getMethod()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_compression_zipwriter.htm#apex_compression_zipwriter_getMethod)**  
    Gets the compression method of the zip file.
- **[removeEntry(name)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_compression_zipwriter.htm#apex_compression_zipwriter_removeEntry)**  
    Removes the entry with the specified name from the zip file.
- **[setLevel(level)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_compression_zipwriter.htm#apex_compression_zipwriter_setLevel)**  
    Sets the compression level of the zip file.
- **[setMethod(method)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_compression_zipwriter.htm#apex_compression_zipwriter_setMethod)**  
    Sets the compression method for the zip file.

### 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](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.") The name of the zip entry.
- **data**: Type: [blob](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_blob.htm#apex_methods_system_blob "Contains methods for the Blob primitive data type.") The content of the zip entry.

#### Return Value

Type: [Compression.ZipEntry](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_compression_zipentry.htm#apex_class_compression_zipentry "Contains methods to get and set information about a zip file entry.")

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](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_compression_zipentry.htm#apex_class_compression_zipentry "Contains methods to get and set information about a zip file entry.") Details of the entry to be added to the zip file.

#### Return Value

Type: [Compression.ZipEntry](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_compression_zipentry.htm#apex_class_compression_zipentry "Contains methods to get and set information about a zip file entry.")

### 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](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.") The name of the zip entry.
- **comment**: Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.") The comment about the zip entry.
- **modTime**: Type: [Datetime](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_datetime.htm#apex_methods_system_datetime "Contains methods for the Datetime primitive data type.") The last modification timestamp of the zip entry.
- **method**: Type: [Compression.Method](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_enum_compression_Method.htm "Specifies the compression method for the zip entries.") The compression method of the zip entry, which is either DEFLATED or STORED.
- **data**: Type: [Blob](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_blob.htm#apex_methods_system_blob "Contains methods for the Blob primitive data type.") The content of the zip entry.

#### Return Value

Type: [Compression.ZipEntry](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_compression_zipentry.htm#apex_class_compression_zipentry "Contains methods to get and set information about a zip file entry.")

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](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_blob.htm#apex_methods_system_blob "Contains methods for the Blob primitive data type.")

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](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_compression_zipentry.htm#apex_class_compression_zipentry "Contains methods to get and set information about a zip file entry.")\>

### getEntry(name)

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

#### Signature

public compression.ZipEntry getEntry(string name)

#### Parameters

- **name**: Type: [string](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.") Name of the zip entry to be retrieved.

#### Return Value

Type: [Compression.ZipEntry](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_compression_zipentry.htm#apex_class_compression_zipentry "Contains methods to get and set information about a zip file entry.")

### getEntryNames()

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

#### Signature

public Set<String> getEntryNames()

#### Return Value

Type: [Set<String>](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_set.htm#apex_methods_system_set "Represents a collection of unique elements with no duplicate values.")

### getLevel()

Gets the compression level of the zip file.

#### Signature

public Compression.Level getLevel()

#### Return Value

Type: [Compression.Level](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_enum_compression_Level.htm "Specifies the compression level for creating a zip file.")

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](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_enum_compression_Method.htm "Specifies the compression method for the zip entries.")

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](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.") 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](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_enum_compression_Level.htm "Specifies the compression level for creating a zip file.") Uses the Level enum to set the compression level.

#### Return Value

Type: [Compression.ZipWriter](#apex_class_compression_zipwriter "Contains methods to add zip entries, generate a zipped archive, and return the result as an Apex blob.")

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](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_enum_compression_Method.htm "Specifies the compression method for the zip entries.") Uses the Method enum to set the compression method.

#### Return Value

Type: [Compression.ZipWriter](#apex_class_compression_zipwriter "Contains methods to add zip entries, generate a zipped archive, and return the result as an Apex blob.")

Returns the zip file set with the specified compression method.