Salesforce Apex Zip Attachments With JSZip

Sometimes we need to zip Attachments or PDFs and download it at once in Salesforce development.
As we know, Apex does not support Zip. it is conceivable that Zip files with other service such as Heroku or AWS, But in fact we can do this without any External Service, just Salesforce and JavaScript.
This post will share you the usage of Apex + JSZip.

The following is the outline of programming.
1.Call WebService and get the Blob of target files from JavaScript.
2.Zip files with JSZip Library.
3.Send Zipped file data with WebService, Create a Document Object in Apex.
4.Redirect to Document download url,download zip(Document) file from Salesforce.

Let’s begin.

Prepare Attachments

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

Create Apex Class WS_Util.cls and WS_ZipUtil.cls

WS_Util.cls

WS_ZipUtil.cls

Add jQuery、JSZip to Static Resource

Download jQuery and JSZip and add to Static Resource.

Create Visualforce Page

Do operation check

Demo Great, we can download zip file now!
zip

SourceCode

Download from Github

Recently updated

If you’re looking for a way to zip files only with Apex code, you could take a look at this post : Zip attachments only with Apex in Salesforce

Enjoy It!