[SalesForce] How to compress a zip file

I have code that generates text file(text file can be of greater size) now when user view it, it should download as a compressed zip file. Let me know any suggestion. Below is the code which is working fine but I want compressed zip file.

public class filesDB{
    public string str;
    public void outputFiles(){
        str='SALES_FORCE|NAME' + '\n';
        for(OutputFilesDB__c ot : [Select SALES_FORCE__c, NAME FROM OutputDemoFile__c]){
            str+=ot.SALES_FORCE__c + '|' + ot.NAME+ '\n';
        }
        Document d = new Document(); 
        d.Name = '#abc'; 
        d.Body = Blob.valueOf(str); 
        d.ContentType = 'application/zip';
        d.Type = 'txt';
        d.FolderId=UserInfo.getUserId();
        insert d;    
    }
}

Best Answer

I believe there is at least one third-party library which can do this for you.

For example, you can check this one

and use the following code

Zippex sampleZip = new Zippex();
sampleZip.addFile('test.txt', Blob.valueOf(str), null);
Blob zipData = sampleZip.getZipArchive();
d.Body = zipData; 
Related Topic