I am trying to unzip a zip file attachment. but, unable to unzip the attachment and process all the internal files in the zip file. I want to do this using an email handler class.
2 Answers
You can do this natively in Apex using the Zippex library which is open source. https://github.com/pdalcol/Zippex
Your code would be similar to this:
Blob zipBlob; //This needs to contain the zip file
Zippex myZip = new Zippex(zipBlob);
//Loop through the files in Zip and process them
for (String fileName : myZip.getFileNames()) {
Blob fileData = myZip.getFile(fileName);
//Process fileData here.
}
-
Pedro, Thank you for sharing this. This is exactly what I was looking for....Very cool!– CarlosCommented Jul 19, 2016 at 22:17
-
This is very cool library. I try to use it in Apex to extract 86.7 KB JSON out of 11 KB Zip file and code failed with LimitException: Apex CPU time limit exceeded, FATAL_ERROR Class.Puff.codes: line 416, column 1– PatlatusCommented Dec 31, 2021 at 13:35
-
Maximum CPU time: 15020 out of 10000. Is there any way to get it work under Apex CPU Time limit?– PatlatusCommented Dec 31, 2021 at 13:37
Unfortunately, there is no native zip support in APEX right now. But you can help yourself using a very good Javascript zip tool like http://stuk.github.io/jszip/
On how to use it, look at http://andyinthecloud.com/2012/11/04/handling-office-files-and-zip-files-in-apex-part-1/
-
1Close, but it won't work for apex code. However, I'm working on a batch class version since the new ten minute limit was introduced.– sfdcfox ♦Commented Jul 12, 2014 at 15:04
-
@sfdcfox sounds very interesting. Will you share your approach? Can you elaborate on how you do it or post a link?– Uwe HeimCommented Jul 12, 2014 at 15:11