Salesforce extract files within Blob Zip File

zippex class in salesforce
salesforce apex zip

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*/

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
    ...
}

How to process a HttpResponse blob zip file, In my application, i am sending an HttpRequest to an external server to .com/​questions/31993430/salesforce-extract-files-within-blob-zip-file 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.

Salesforce doesn't support zip file extraction. You will have to do it at your externally hosted service.

How to unzip a zipfile attachment in apex and process each file in , 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  The file is taken as input from user(as blob) and then encoded into base 64. This base encoding makes the string size grater than 3MB. As the mazimum size of String allowed is 3MB, i am unable to upload zip files greater than 3 MB.<br>Also, even if i do upload the file without using salesforce, i need to read the zip file after uploading it.

I have finally found a solution, it's not the best one, because to unzip the file i should use an external service, but i am running out of time, so these are the steps i followed:

1. Create StaticResource for the zip file (i had to use the Metadata Salesforce Api)

2. extract the file you want by creating a PageReference object with url: `/resource/NameOfResource/NameOfFile

3. delete the StaticResource: i created it only to extract some files, so i don't need it any more.

Here's a sample code of the first point that i get from This link

private static MetadataService.MetadataPort createService()
    { 
        MetadataService.MetadataPort service = new MetadataService.MetadataPort();
        service.SessionHeader = new MetadataService.SessionHeader_element();
        service.SessionHeader.sessionId = UserInfo.getSessionId();
        return service;     
    }    

MetadataService.MetadataPort service = createService(); 
MetadataService.StaticResource staticResource = new MetadataService.StaticResource();
staticResource.fullName = nameOfResource;
staticResource.contentType = 'application/zip';
staticResource.cacheControl = 'public';
staticResource.content = EncodingUtil.base64Encode(zipBlobFile);
List<MetadataService.SaveResult> results =  service.createMetadata( new MetadataService.Metadata[] { staticResource });

and here's the code for the second point that i get from the same link

private static String GetResourceURL(String resourceName){
    // Fetching the resource
    List<StaticResource> resourceList= [SELECT Name, NamespacePrefix, SystemModStamp FROM StaticResource WHERE Name = :resourceName];

    // Checking if the result is returned or not
    if(resourceList.size() == 1){

       // Getting namespace
       String namespace = resourceList[0].NamespacePrefix;
       // Resource URL
       return '/resource/' + resourceList[0].SystemModStamp.getTime() + '/' + (namespace != null && namespace != '' ? namespace + '__' : '') + resourceName; 
    }
    else return '';
}

private Blob  getResourceFile(String url, String fileName) {
    PageReference tmp = new PageReference(url + '/' + fileName);
    Blob contentAsBlob = tmp.getContent();

    return contentAsBlob;
}

String url = GetResourceURL(nameOfResource);
Blob myFile = getResourceFile(url, nameOfFileToExtract);

For the tird point it's just one line of code :

List<MetadataService.DeleteResult> deleteResults =  service.deleteMetadata('StaticResource', new String[] { resourceName });

I hope this answer will be usefull.

EDIT:

If you have to create the StaticResource and then delete it, don't execute DML operations between the create and delete callouts, instead of that execute the DML operation after or before the Callout, unless you want to execute the callout in a @future method but it didn't work for me.

Salesforce extract files within Blob Zip File - zip - php, 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​  Because the data is confidential so we don't want is to process on client side. So is there any way to process Zip file in Apex? Some library or custom approach we could use. Also I have a doubt that if Zip file can be processed, will it within the governor limit of Salesforce? I have searched everywhere but couldn't found any solution.

Salesforce extract files within Blob Zip File - zip - iOS, 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​  In my application, i am sending an HttpRequest to an external server to get a zip file, and once it's done i get the response as Blob, but i don't know how to process the Blob to extract some files from it.

Zip attachments only with Apex in Salesforce, Zip those files with Zippex. 3. Save Blob data of zip file into a Document. 4. Redirect to Document download url and download it from Salesforce  11. If there are files that are not converted, it means that the files have the same file name inside the folder. To convert the remaining unconverted files, create a new folder and copy the unconverted files and the fix.bat file to that new folder. Double click the fix.bat file again.

Download Attachments as a Zip file in Salesforce, Different ways to zip and download all the attachments related to an object in Salesforce using Apex. Download Attachments as a Zip file in Salesforce var attachmentData = zipFile.generate({type:"blob"}); var fileName = 'Attachments.zip​'; http://www.valnavjo.com/blog/compressing-files-in-salesforce/. Native Apex Zip library for Salesforce.com. Contribute to pdalcol/Zippex development by creating an account on GitHub.

Comments
  • i read an article about Zippex Library a few weeks ago, and it seems to be the best solution. Thanks
  • You could improve your answer by generally suggesting implementations of external services.
  • Do you have any suggestions ? I didn't find a solution yet
  • You may want to try the Expand Container API from HPE Haven OnDemand... dev.havenondemand.com/apis/expandcontainer#overview (Disclosure: I work for Hewlett Packard Enterprise)