function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
khushbu parmar 5khushbu parmar 5 

How to create an Zip file with APEX

I want to create an zip file with APEX, my requirement is to extract all the documents related to an object and archive it as a zip file.
Files could be images, text, word or CSV etc.

I have tried this Link -, but it gives me a APEX CPU Limit exceeded error when I try to compress multiple files at once.

Please provide information or tell another way to solve this error using APEX?
ANUTEJANUTEJ (Salesforce Developers) 
Hi Khushbu,

Can you try checking the below link:


Do let me know if this works.

Hi Khushbu,

Salesforce doesn't provide zip support out of the box but you can use Zippex which is an open-source zip library for Apex.

And in the apex we will simply do
Zippex sampleZip = new Zippex();
Blob fileData = Blob.valueOf('Sample text.');
sampleZip.addFile('sampleFolder/test.txt', fileData, null);
Blob zipData = sampleZip.getZipArchive();

If this answer helps you, please mark it as accepted.

Tushar Sharma
khushbu parmar 5khushbu parmar 5

Hi Tushar,

Yes,right. I have used the zippex library and its actually working but the problem occures when I try to compress multiple files at once or we can say its the size limit is there. So that time i get the APEX CPU Time limit error.

If you have any idea about the file size limit that we can compress?


Hi Khushbu,

I am not quite sure but based on heap size (6 MB Sync) file size should be lesser than that. It also depends on other variables. You can use the LIMIT class to get these details. its a hit and run situation.

If this answer helps you, please mark it as accepted.

Tushar Sharma
jitendra singh 172jitendra singh 172
Hi Khushbu, 

If you got the solution of this problem , please share with us 
i'm also suffering from this problem.

Thanks & Regards
Jitendra Singh
Email Id -
rishabh rathor 28rishabh rathor 28
I personally wants you to try File ZIPO once. As it helps you to ZIP files directly from your Salesforce org eliminating the need to follow the tedious standard process. Try it now!
Jason Burke 19Jason Burke 19

I've used the jsZip library with success in the past to offload the zipping to the browser and avoiding Apex limitations.