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.

Account

Upload Zippex library

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

Create sample class and page

ApexZipSampleController.cls

ApexZipSample.page

It works fine for me : )

Apex zip sample page
apex-zip-sample
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!