Zip attachments only with Apex in Salesforce

Before this post (about 2 years ago), I’d shared a post that is about how to zip attachments with JSZip and Apex in Salesforce, and recently I found a more simple way to do that thing, which is only using an Apex library named “Zippex” ( view on Github).

Here is the outline of my sample code:

1. Select attachments from an object id.
2. Zip those files with Zippex.
3. Save Blob data of zip file into a Document.
4. Redirect to Document download url and download it from Salesforce.

Let’s get started.

Prepare Attachments

Upload two Attachments to Account, e.g. apex PDF.pdf and apex Word.docx.


Upload Zippex library

Upload and compile Zippex library files ( view on Github) into your Salesforce organization.

Create sample class and page


It works fine for me : )

Apex zip sample page
You can download all of the above source files on my Github repository from HERE.

One more thing :

Before you decide to use this library in your project, make sure you have tested the cases of max number of files and max size of files, which may occur the Apex CPU Governor error.

Enjoy It!

Related Post

Instantiating an Apex class based on dynamic strin... Somtimes we need to instantiate a Apex class by dynamic value in Salesforce, which can make our program more modular and more readable, such as the below case : 1. Each Contact has its socail fields : "Type__c" and "AccessToken__c" 2. Needs to request its profile via socail api in Apex Firs...
Salesforce reRender dose not work with rendered at... Salesforce reRender will not work, if we're going to hide reRender target with rendered attribute of itself, as the following sample code. In my opinion, reRender will reset the target DOM Node but...
A way to make Thread Sleep in Apex Sometimes we need make apex thread to sleep for a while, sush as testing if the soql with [For Update] works,and as we know the sfdc do not offer a method to sleep the thread. Then I thought it may work if I use the "while", like the following code. ...
Create a sObject dynamically The following code provides a general example of how to create a sObject dynamically in apex. Usage This kind of function will be useful in creating general class.