Find your content:

Search form

You are here

How To Download ZIP Files From URL?

 
Share

I want to read a zip file from a give url. That zip file is having a XML file which is the main file which I want to access.

Is there a way to access/read that XML/Zip file ?


Attribution to: S.Sharma

Possible Suggestion/Solution #1

You can do this natively in Apex with the Zippex library. It's open source and available here https://github.com/pdalcol/Zippex

//Fetch Zip file from URL
HttpRequest request = new HttpRequest();
request.setEndpoint('http://www.example.com/archive.zip');
request.setMethod('GET');
HttpResponse response = new Http().send(request);
Blob zipFileBlob = response.getBodyAsBlob();

//Pass zipFileBlob to Zippex constructor
Zippex zip = Zippex(zipFileBlob);

//Extract sample_file.xml from zip archive
String xmlString = zip.getFile('sample_file.xml').toString();

Attribution to: Pedro Dal Col

Possible Suggestion/Solution #2

No, you can't natively decompress/read the contents of a ZIP file in Apex Code. Furthermore, given the inherent limitations of the platform, including the CPU time limit and lack of binary facilities, it's impossible to decompress any file over a couple of kilobytes in size. Preferably, you'll want to download the XML file directly, if possible.

MORE INFO: If you're willing (and able) to use JavaScript as a solution, you can use JSZIP to read the file, jQuery or any other JavaScript to download the file (through the JavaScript AJAX Proxy, if necessary). This will only run in a Visualforce or browser context (e.g. SControl, custom buttons), but it would allow you to read the ZIP file, which you can then read using jQuery's (or any other library/script) methods to parse and navigate the contained XML file. Also, since you're talking about client-side code, Flash, Java, and Silverlight are all viable options as well.


Attribution to: sfdcfox
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/31338

My Block Status

My Block Content