function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
prashant1.393935347365481E12prashant1.393935347365481E12 

Read zip attachments

Hi, 
Please suggest how can I read a zip attachment from the VF page. The attachment consists of some text files. Currently I am reading it via zip.js library but it gives a cross domain accessibility error since the attachments are in a different sub domain.

Please suggess a way out!!
PrasanntaPrasannta (Salesforce Developers) 
Hi,

In order to read the Read zip attachments, Please include JSzip library in the javascript function.

Here is the example of the javascript code snippet-
function viewContent(){
        var zip = null;
        var zipFileName = null;
        var zipFileNames = null;
        data = "{!contentAsText}";
        zip = new JSZip(data, {base64:true});
            zipFileName = 'files.zip';
            zipFileNames = [];
            for(var zipfileName in zip.files){
                zipFileNames.push(zipfileName);
         if(zipfileName == 'index.html'){ 
         var file = zip.files[zipfileName];
         var data = file.data;
         document.getElementById('contentdiv').innerHTML = data;
         //var data = JSZipBase64.encode(file.data);
         }
         }
                
n controller --

public String contentAsText {get;set;}

List<Attachment> atts = [Select Id, Body from Attachment where name='files.zip' limit 1];
       contentAsText = EncodingUtil.base64Encode(atts[0].Body);
          
Hope this helps.
Pedro I Dal ColPedro I Dal Col
You can try using Zippex (https://github.com/pdalcol/Zippex). It's an open source zip library for Apex. You can load the zip files using an HttpRequest in the controller and uncompress them using Zippex before you display the results in the page.