Compression Namespace in Apex: A Powerful New Salesforce Feature

Published on
November 5, 2025
Author
Salesforce Dev Team
Compression Namespace in Apex: A Powerful New Salesforce Feature

Introduction

Working with documents inside Salesforce has always challenged developers because of the platform’s multitenant constraints. Previously, packaging and sending files in a compact form required external services, like an AWS Lambda function, that retrieved files via API and then compressed them.

With the introduction of the Compression Namespace and the powerful pre-defined Apex functions, you can now perform these tasks entirely within Salesforce boundaries. This brings significant benefits for Salesforce Apex Development, but it also comes with governor limits you must respect. Let’s dive into what this new feature means for developers.

What is Apex Compression Namespace?

Introduced in Spring 25 release, the Compression Namespace is a new Apex library added to the ever-growing list of native Salesforce functionalities. It makes it possible to create, read, and manage archived files (.zip extension) with Apex code, eliminating the need for leveraging external apps. The namespace provides the standard functionalities available when compressing files on your operating system.

You can specify:

Compression Level: Choose the right balance between processing time and file size. The available options in the Compression.Level enum are:

  • BEST_COMPRESSION: Slowest to process, but produces the smallest size compressed file.
  • BEST_SPEED: Fastest processing, but results in the largest compressed file size.
  • DEFAULT_LEVEL: A balance between speed and size.
  • NO_COMPRESSION: The file is added to a ZIP archive without being compressed. The data is stored as-is without undergoing any deflation.

Compression Method: The standard compression methods DEFLATED (the standard for ZIP files) and STORED (no compression) are available via the Compression.Method enum.

Note: Both the compression Level and Methods are available as enumerated data types within the namespace.

The namespace also contains three separate apex classes for reading and writing a zip file, namely:

  • ZipEntry Class: Represents a single entry (a file or directory) within a ZIP archive and contains methods to access its metadata, such as its name and size.
  • ZipReader Class: Used for reading and extracting files from a compressed ZIP archive within Apex.
  • ZIPWriter Class: As the name suggests, this class is used for creating ZIP files within Apex, which can then be stored or sent as attachments.

public static void zipReader(){
        //Create a ZIPReader instance for working on zip files in Salesforce\
        //Zip Reader has a single constructor which accepts blob data
        
        //VersionData data type is blob
        ContentVersion compressedVersion = [SELECT Id,VersionData FROM ContentVersion where title = 'Test Compressed File'];
        Blob compressedBlob = compressedVersion.VersionData;
        
        //Initiate Zip Reader instance
        Compression.ZipReader zipReader = new Compression.ZipReader(compressedBlob);
        Map<String,Compression.ZipEntry> zipReaderMap = zipReader.getEntriesMap();
        
        //Iterate over ZipReader Map
        for(String fileName : zipReaderMap.keyset()){
            system.debug(fileName);
        }
    }

Handling Multiple Files within a ZIP

The Apex Compression Namespace offers powerful capabilities for managing multiple files inside a single ZIP archive directly within Salesforce. Using the Compression.ZipReader class and its methods — getEntries() and getEntriesMap() — developers can easily retrieve all ZIP entries in a structured and readable format.

Each Compression.ZipEntry object represents a file within the archive. After retrieving an entry, you can call the extract() method on the ZipReader instance to obtain its content as a Blob. This Blob can then be processed, stored as a new ContentVersion, or passed to other Apex routines according to your business requirements.

Understanding Governor Limits with the Compression Namespace

While the Compression Namespace introduces advanced new functionalities to Salesforce, it comes with constraint that applies to all of the Salesforce instances – Governor Limits.

Compression may reduce the final size of a file or payload, but it does not reduce the memory (heap space) consumed during the compression process—including loading the file and storing intermediate variables still counts toward the governor limits.

Key points to remember:

  • In synchronous transactions, the heap size is limited to 6MB.
  • In asynchronous contexts (like Queueable or Batch Apex), the limit increases to 12MB.

When dealing with large files, especially when converting them into a compressed format, it’s important to understand that reading the file into memory (e.g., as a Blob) and storing it in a variable consumes heap space before compression takes place. Simply compressing the file does not circumvent the governor limit; careful handling and memory management are still essential.

Use Cases of the Apex Compression Namespace

The Apex Compression Namespace can be used across several practical Salesforce file-compression scenarios. Here are some of the common implementation areas:

  • When sending data through HTTP callouts, you can compress the request body to stay within strict size limits or improve network efficiency. This helps optimize integration performance and reduce API payload size.
  • If an external system returns data in a compressed format, use the Compression.ZipReader class to unpack and process it directly within Apex—no external services required.
  • In certain cases, email systems restrict or reject uncompressed file attachments. With the Apex Compression.ZipWriter class, you can create Apex ZIP files and send them as email attachments securely and efficiently, all within Salesforce.

These use cases demonstrate how the Compression Namespace enables end-to-end file compression workflows, from API communication to document management, while keeping logic and data entirely native to Salesforce.

Final Thoughts

The Apex Compression namespace in Spring ’25 is a welcome addition for Salesforce developers. It streamlines file management tasks like creating and reading ZIP archives with ZipReader and ZipWriter, enabling practical use cases like compressed API payloads and optimized email attachments.

However, the platform’s governor limits remain in play. Plan your memory usage carefully, favor asynchronous patterns for large files, and choose compression settings intentionally. Used wisely, this feature reduces reliance on external tools, speeds up development, and keeps your logic 100% on-platform.

Visit our Resource Center to explore more real-world integration patterns, expert tips, and implementation guides like this.

Recent Blogs

Boost LWC Performance with Debouncing
BlogSep 18, 2025

Boost LWC Performance with Debouncing

Introduction Lightning Web Components (LWC) is a modern framework for building fast and dynamic user interfaces on the Salesforce platform. However, one common challenge in web development, including LWC, is efficiently handling user input, especially when dealing with rapid or repetitive events, such as typing in a search field. This is where debouncing becomes an… Continue reading Boost LWC Performance with Debouncing

Read More
Blog
7 min read

Boost LWC Performance with Debouncing

Introduction Lightning Web Components (LWC) is a modern framework for building fast and dynamic user interfaces on the Salesforce platform. However, one common challenge in web development, including LWC, is efficiently handling user input, especially when dealing with rapid or repetitive events, such as typing in a search field. This is where debouncing becomes an… Continue reading Boost LWC Performance with Debouncing

Read More
Salesforce Pricing Automation: Boost Efficiency And Accuracy with Apex Triggers
BlogSep 9, 2025

Salesforce Pricing Automation: Boost Efficiency And Accuracy with Apex Triggers

Introduction In order to succeed in today’s fast-paced business landscape, precision and speed define competitive advantage. For businesses, especially those managing complex product catalogs, ensuring accurate pricing on sales orders or custom lines can be a time-consuming and error-prone task. To overcome this challenge, Salesforce trigger handlers offer a powerful solution to automate the entire… Continue reading Salesforce Pricing Automation: Boost Efficiency And Accuracy with Apex Triggers

Read More
Blog
6 min read

Salesforce Pricing Automation: Boost Efficiency And Accuracy with Apex Triggers

Introduction In order to succeed in today’s fast-paced business landscape, precision and speed define competitive advantage. For businesses, especially those managing complex product catalogs, ensuring accurate pricing on sales orders or custom lines can be a time-consuming and error-prone task. To overcome this challenge, Salesforce trigger handlers offer a powerful solution to automate the entire… Continue reading Salesforce Pricing Automation: Boost Efficiency And Accuracy with Apex Triggers

Read More
Connecting MuleSoft and Azure SQL with Entra ID
BlogJul 14, 2025

Connecting MuleSoft and Azure SQL with Entra ID

Introduction Establishing a secure connection between MuleSoft and Azure SQL Database can be challenging, especially if you are using Entra ID (formerly known as Azure Active Directory) for authentication. This blog walks through a fully working configuration for connecting to Azure SQL using ActiveDirectoryServicePrincipal in Mule runtime 4.7.4 with Java 8 — addressing driver setup,… Continue reading Connecting MuleSoft and Azure SQL with Entra ID

Read More
Blog
2 min read

Connecting MuleSoft and Azure SQL with Entra ID

Introduction Establishing a secure connection between MuleSoft and Azure SQL Database can be challenging, especially if you are using Entra ID (formerly known as Azure Active Directory) for authentication. This blog walks through a fully working configuration for connecting to Azure SQL using ActiveDirectoryServicePrincipal in Mule runtime 4.7.4 with Java 8 — addressing driver setup,… Continue reading Connecting MuleSoft and Azure SQL with Entra ID

Read More
Understanding Salesforce Flow Approval Processes
BlogJun 30, 2025

Understanding Salesforce Flow Approval Processes

Introduction: Salesforce introduced Flow Approval Processes in the Spring '25 release. This is an evolved version of the classic approval process model, powered by Flow Orchestrator. The new approach brings unprecedented flexibility, enabling the creation of dynamic, multi-level, and logic-driven approval workflows that are entirely declarative. Continue reading the blog to get a deeper understanding… Continue reading Understanding Salesforce Flow Approval Processes

Read More
Blog
5 min read

Understanding Salesforce Flow Approval Processes

Introduction: Salesforce introduced Flow Approval Processes in the Spring '25 release. This is an evolved version of the classic approval process model, powered by Flow Orchestrator. The new approach brings unprecedented flexibility, enabling the creation of dynamic, multi-level, and logic-driven approval workflows that are entirely declarative. Continue reading the blog to get a deeper understanding… Continue reading Understanding Salesforce Flow Approval Processes

Read More