ZipSupport In Apex

The ZIP Support (Developer Preview) feature is currently available as a Developer Preview. You can create and extract ZIP archive files using the classes and methods in the Compression namespace. 

However, this feature is not yet generally available unless Salesforce announces its general availability in documentation or public statements. 

Features, commands, and parameters may change or be removed at any time without notice. Salesforce recommends avoiding the use of compression methods in a production environment.

With this feature, you can compress multiple attachments or documents into an Apex blob that contains the ZIP archive. You can also extract specific data from the ZIP archive without having to uncompress the entire archive. To optimize compression, you can specify a compression method and compression level.

This feature is available in scratch orgs where the ZipSupportInApex feature is enabled. If the feature isn’t enabled, Apex code with this feature can be compiled but not executed.

You must enable support for zip file processing by adding ZipSupportInApex to the features property in your scratch org definition. "features": ["ZipSupportInApex"]

Sinppet of a Location Records

File Name - LocationB2B.csv





Here's an Example

Zip a File

ContentVersion cv = [SELECT PathOnClient, Versiondata, Title

                           FROM ContentVersion
                           WHERE Title = 'LocationData' Limit 1];

Compression.ZipWriter writer=new Compression.ZipWriter();
    writer.addEntry(cv.PathOnClient,cv.versiondata);
    Blob zipFile=writer.getArchive();
 
    ContentVersion contentVersion = new ContentVersion(
        VersionData =zipFile,
        Title = cv.Title,
        PathOnClient = cv.Title+'.zip'
    );
Insert contentVersion;

UnZip a File

ContentVersion cv = [SELECT PathOnClient, Versiondata, Title
                           FROM ContentVersion
                           WHERE Title = 'LocationData.zip' Limit 1];

Blob zipBody = contentVer.VersionData;
    Compression.ZipReader reader = new Compression.ZipReader(zipBody);
    Compression.ZipEntry csvEntry = reader.getEntry(cv.Title+'.csv');
    Blob csv = reader.extract(csvEntry);
 
    System.debug('Location CSV body = ' + csv.toString()); // Retrive content of Locatin CSV File

Comments

Popular posts from this blog

Introduction to Salesforce Agent Script: Build Predictable AI Agents

Anti-Patterns in Salesforce Agentforce: What to Avoid When Building AI Agents

MCP vs. Traditional APIs: Choosing the Right Integration Approach