+ 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 - https://github.com/pdalcol/Zippex, 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:

>> https://salesforcescool.blogspot.com/2018/11/create-zip-of-salesforce-attachment.html

Do let me know if this works.

Regards,
Anutej
{tushar-sharma}{tushar-sharma}
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. https://github.com/pdalcol/Zippex

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.

Regards,
Tushar Sharma
https://newstechnologystuff.com/
 
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?

Thanks,
Khushbu

{tushar-sharma}{tushar-sharma}
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.

Regards,
Tushar Sharma
https://newstechnologystuff.com/
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 - jitendrasingh1304082@gmail.com
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!
https://filezipo.io/zip-files/