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!


Related Post

Batch Apex running mode (parallel and series) and ... In my early post, I’d shared about how to write a Schedulable Batch Apex in Salesforce, and I’ll show you how transactions run in batch Apex and how to maintain their state across these transactions in this post. Batch Apex running mode (parallel / series) As we know, each execution of a batch...
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...
Using Aggregate SOQL queries/results in Batch Apex In previous post, we had created a schedulable batch apex that implements Database.Batchable in Salesforce, but if you want to use SOQL having aggregate functions like SUM(), MAX(), COUNT() on results grouped by “GROUP BY” clause in start execution, changing to interface Database.Ba...
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: ...