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
SS KarthickSS Karthick 

Create Zipfile in salesforce

Hi folks,
      Can anyone tell me how to create a zip file in salesforce via apex?
I have seen the below link but it doesnt work for me.

Please someone give me the working code sinppet for creating zipfile.

Thanks in advance
Ravikant kediaRavikant kedia
             String result= res.getHeader('result');
              //Get body as a blob
              Blob ResponseString=res.getBodyAsBlob();
              System.debug('Response str is   '+ResponseString);  
              //save file in Attachment
              attachment DataFile = new attachment();
              //store file as a zip file        
              DataFile.Name = '';       
              DataFile.ParentId ='a049000000HRSkc';                            
              DataFile.body = ResponseString; 
              insert DataFile;   
Similer to above method you  can create zip file in apex
SS KarthickSS Karthick
@Ravikant kedia:
  My usecase is download all the attachements on click of visualforce button.
For that we need to create a zip file which contains all the attachments.
I need a sample code for my usecase.

Thanks in advance,
Pedro I Dal ColPedro I Dal Col
You can use the Zippex library to do this. It's open source.
//First create a blank zip archive
Zippex zip = new Zippex();

//Add files one by one using the addFile() method
//The first parameter is the filename
//The second parameter is a Blob with the file contents
//The thrid parameter is the file chechsum, you can pass null
zip.addFile('sampleFile1.txt', Blob.valueOf('Sample text1.'), null);
zip.addFile('sampleFile2.txt', Blob.valueOf('Sample text2.'), null);
//Finally fetch the resulting zip file
Blob zipBlob = zip.getZipArchive();