Search code examples
zipsalesforceblobunzipapex

Salesforce extract files within Blob Zip File


I am developing an application in Salesforce Where i get a zip file form an external server using HttpRequest.

The file i get from the server is Blob, but i don't know how to process it to extract the files i want.

Here's the code

        HttpRequest req = new HttpRequest();

       /* Here i set the HttpRequest Attributes*/

        Http http = new Http();                
        HttpResponse response = http.send(req);

        Blob file = response.getBodyAsBlob();

        /*Now i need to access: file to extract the files i want*/

Solution

  • This can be done with the Zippex library. It's open source and available here: https://github.com/pdalcol/Zippex

    HttpRequest request = new HttpRequest();
    
    /* Here set the HttpRequest Attributes */
    
    Http http = new Http();                
    HttpResponse response = http.send(request);
    
    Blob file = response.getBodyAsBlob();
    
    //Instantiate Zippex with zip file Blob
    Zippex zip = Zippex(file);
    
    for (String fileName : zip.getFileNames()) {    
        //Extract file
        Blob fileData = zip.getFile(fileName);
        //Process the file here
        ...
    }