Salesforce ZIP File Apex: Compression Tools in Spring ’25

The Spring ’25 release brings something we’ve all been waiting for: the ability to create and extract ZIP files natively in Apex, no more third-party workarounds or external services.

With the introduction of the Compression.ZipWriter and Compression.ZipReader classes, you can now handle zipped files directly within Apex code. This update is a big win for file handling, automation, and integrations inside Salesforce.

Let’s walk through how it works, where you can use it, and how to avoid common pitfalls, all in plain language.

Salesforce ZIP file Apex

Salesforce ZIP file Apex Guide

Why ZIP Support in Apex Matters

Before Spring ’25, working with ZIP files in Apex wasn’t straightforward. You had to:

  • Use complex integrations
  • Rely on JavaScript hacks
  • Manually decode BLOBs

Now, all that changes. With just a few lines of code, you can:

  • Package multiple files into a ZIP archive
  • Extract ZIP file contents
  • Process uploaded ZIPs in triggers or batch jobs

Meet the Compression Classes

Salesforce has added three key classes under the Compression namespace:

1. ZipWriter – Creating Salesforce ZIP Files in Apex

This class helps you create a .zip file on the fly.

Basic usage:

Compression.ZipWriter writer = new Compression.ZipWriter();

writer.addEntry('report.csv', Blob.valueOf('Sales Data'));

Blob zipBlob = writer.getArchive();

What you can do with zipBlob:

  • Attach it to an email
  • Upload to Salesforce Files
  • Save on a record as an attachment

You can also add multiple entries in one go using addEntries().

2. ZipReader – Extracting ZIP Files with Salesforce Apex

This class lets you extract and work with the contents of a ZIP file.

Compression.ZipReader reader = new Compression.ZipReader(zipBlob);

List files = reader.getEntries();

Loop through the entries to get each file’s data and name.

3. ZipEntry – Details of Each File

Each file in a ZIP is represented by a ZipEntry.

Properties include:

  • name: File name (like invoice.pdf)
  • data: The file content as a Blob
  • size: Original size
  • compressedSize: After compression
  • isDirectory: If it’s a folder (rare, but possible)

Where You Can Use This in Salesforce ZIP File Apex

Here are some practical, real-world scenarios where this feature shines:

  • Email Reports: Attach ZIPs with multiple files (e.g. logs, test results)
  • Bulk Uploads: Accept user-uploaded ZIPs and extract files server-side
  • APIs Integration: Send zipped files to third-party services
  • Data Archiving: Store compressed files to save space
  • Batch Processing: Upload and unzip large datasets in one go

A Quick Demo Class

public class ZipDemo {

    public static void testZipFunctionality() {

        // Create ZIP

        Compression.ZipWriter writer = new Compression.ZipWriter();

        writer.addEntry('hello.txt', Blob.valueOf('Hello from Salesforce!'));

        Blob archive = writer.getArchive();

        // Extract ZIP

        Compression.ZipReader reader = new Compression.ZipReader(archive);

        for (Compression.ZipEntry entry : reader.getEntries()) {

            System.debug('File: ' + entry.name + ', Content: ' + entry.data.toString());

        }

    }

}

Resource Usage and Limits in Salesforce ZIP File Apex

Salesforce governor limits apply here too. Always monitor heap and BLOB sizes:

Limit TypeSynchronous ApexAsynchronous Apex
Max Heap Size6 MB12 MB
Max BLOB Size6 MB12 MB
Max ZIP Files1,0001,000
Max Uncompressed Size20 MB20 MB
Max ZIP File Size10 MB10 MB

Use Queueable or Batch Apex if your ZIP files are large.

Best Practices for Using ZIP in Apex

To keep things smooth and efficient:

  • Use asynchronous Apex for anything larger than a few files
  • Log heap and CPU usage for better debugging
  • Handle errors with try-catch blocks to avoid data loss
  • Avoid special characters in file names when zipping
  • Encrypt sensitive data before compression if required
  • Always test with realistic file sizes

Summary

Salesforce has made it much easier to work with ZIP files in Apex. With ZipWriter and ZipReader, you can now:

  • Compress and package files with minimal code
  • Read and extract uploaded ZIPs directly
  • Improve storage, performance, and integrations

If your org deals with files, this is a must-use feature. From email automation to API integrations, it’s a game-changer.

FAQs

What is the Salesforce ZIP file Apex feature?

It’s a new Spring ’25 feature that allows developers to create and extract ZIP files directly in Apex using built-in classes like ZipWriter and ZipReader, without relying on external tools or APIs.

How do I create a ZIP file in Salesforce Apex?

Use the Compression.ZipWriter class. Add file entries with addEntry() and generate the ZIP using getArchive(). The result is a Blob you can email, store, or attach to records.

How can I extract files from a ZIP in Apex?

Use the Compression.ZipReader class. Pass the ZIP Blob to the constructor, then call getEntries() to get a list of ZipEntry objects representing each file in the archive.

What are the size limits for ZIP files in Apex?

  • Max compressed ZIP size: 10 MB
  • Max total uncompressed size: 20 MB
  • Max files per ZIP: 1,000
  • Max Heap Size: 6 MB (sync), 12 MB (async)

Use asynchronous Apex for large files to avoid governor limit issues.

Can I attach a ZIP file to an email in Apex?

Yes. After creating a ZIP Blob with ZipWriter, you can attach it using Messaging.EmailFileAttachment and send it via Apex email services.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top