Understanding ZIP Compression in Apex: Deflated vs Stored (Salesforce 2026 Guide)

March 27, 2026
216 Views
Understanding ZIP Compression in Apex: Deflated vs Stored (Salesforce 2026 Guide)
Summarize this blog post with:

Efficient file handling has become a critical part of modern Salesforce development. As applications continue to deal with large datasets, document-heavy workflows, and integration-driven architectures, effective ZIP compression and file size management are no longer optional—they directly impact performance and scalability.

With native ZIP support now available in Apex, developers can handle compression directly within Salesforce. This eliminates the need for external tools, but it also introduces an important responsibility: choosing the right compression strategy.

At the centre of this decision lies a simple yet impactful comparison of the Deflated vs Stored methods.

Why Compression Decisions Matter More Than Ever?

In real-world Salesforce implementations, file handling is rarely isolated. It typically connects with:

  • Bulk document generation (invoices, reports, contracts)
  • API integrations exchanging large payloads
  • Data migration and archival processes
  • AI and analytics pipelines working on structured data

Without an optimised compression approach, these operations can lead to:

  • Increased CPU consumption
  • Slower execution times
  • Higher storage usage
  • Risk of hitting governor limits

This is why understanding compression methods is not just technical knowledge; it’s a design decision.

How Apex Handles ZIP Compression?

Salesforce now provides native support for ZIP operations through the Compression namespace. Using classes like ZipReader and ZipWriter, you can bundle and unzip files directly within Apex.

A simple implementation looks like this:

Compression.ZipWriter writer = new Compression.ZipWriter();
writer.addEntry('data.csv', Blob.valueOf('Sample Data'));
writer.addEntry('image.png', imageBlob);

Blob zipFile = writer.getArchive();

This simplicity hides an important layer of control over how each file is compressed inside the archive.

Stored Method: When Performance Takes Priority

The Stored method packages files into a ZIP archive without applying any compression. While it may sound basic, it is often the most efficient choice in performance-sensitive scenarios.

Where Stored Works Best?

    • Files which are already compressed:
      • Images (PNG, JPEG)
      • PDFs
      • Videos
  • Real-time processing requirements
  • Scenarios close to CPU limits
Why It Matters?
  • No compression overhead
  • Faster execution
  • Lower CPU usage
Instead of wasting resources trying to reduce already optimised files, Stored ensures that processing remains fast and predictable.

Deflated Method: When Size Optimisation Matters

The Deflated method applies compression algorithms to reduce file size by eliminating redundancy in data.

It is particularly effective for structured and text-heavy data.

Ideal Use Cases

  • CSV files
  • JSON and XML data
  • Logs and repeated data structures
Key Benefits
  • Significant file size reduction
  • Faster data transfer in integrations
  • Improved storage efficiency
However, this efficiency introduces trade-offs:
Key Benefits
  • Increased CPU usage
  • Longer processing time
  • Higher memory consumption during execution
This makes Deflated powerful but not universally suitable.

The Smarter Approach: Combining Both Methods

In real applications, choosing one approach for all files will rarely be optimal.

A more effective approach is to apply compression selectively.
Example Strategy

  • CSV / JSON → Deflated
  • Images / PDFs → Stored
Why This Works
  • Reduces size where it actually matters
  • Avoids unnecessary processing
  • Maintains overall system efficiency
The hybrid approach is also typically used in enterprise spaces because of the compromise between performance and optimisation.

Applying Compression in Apex (With Control)

Apex allows developers to define compression behaviour at the file level, giving precise control over how each file is handled.


writer.addEntry(
            	'data.csv',
            	null,
            	Datetime.now(),
            	Compression.Method.DEFLATED,
            	Blob.valueOf('Sample Data')
  );
This flexibility enables:
  • Fine-tuned compression strategies
  • Better performance optimisation
  • More efficient resource utilisation
Additionally, compression levels can be adjusted based on requirements:
  • Faster processing (lower compression)
  • Maximum size reduction (higher compression)
  • Balanced default behaviour

Performance Considerations You Can’t Ignore

Compression is more than just a final ZIP file; it is also part of the runtime behaviour. Key Factors to Consider

1. Heap Size
  • Files must be loaded into memory before compression
  • Large datasets can quickly exceed limits
2. CPU Time
  • Deflated method consumes more processing power
  • Large operations may hit execution limits
3. Governor Limits
  • Compression counts toward:
    • CPU time
    • Heap usage
    • Execution constraints
Recommended Approach for Large Files
  • Use Queueable or Batch Apex
  • Avoid heavy compression in synchronous transactions
  • Test with real-world file sizes

Where ZIP Compression Delivers Real Value?

ZIP compression is not just a utility feature; it directly improves multiple Salesforce workflows.

Common Practical Use Cases

1. Bulk File Downloads
  • Combine multiple files into a single ZIP
  • Simplify user experience
2. API Integrations
  • Send compressed payloads
  • Reduce bandwidth and improve speed
3. Data Archiving
  • CPU time
  • Heap usage
  • Execution constraints
4. Excel File Processing
  • Extract and process XLSX files (ZIP-based structure)
  • Enable advanced automation
5. Email Attachments
  • Send multiple documents as one file
  • Reduce attachment size

Building an Efficient Compression Strategy

A well-designed approach to compression includes:

  • Selecting the right method based on file type
  • Avoiding unnecessary compression
  • Monitoring CPU and memory usage
  • Using asynchronous processing when needed

Quick Guidelines

  • Don’t compress already compressed files
  • Use Deflated only where size reduction is meaningful
  • Prefer hybrid compression over a single-method approach
  • Always validate performance with real data

Conclusion

ZIP compression in Apex is no longer just a supporting feature; it is a core part of building efficient and scalable Salesforce applications.

The difference between the Stored and Deflated methods goes beyond theory. It directly influences how applications perform under real-world conditions.

  • Stored ensures speed and minimal processing
  • Deflated enables size optimisation and efficient data transfer
  • A balanced combination of both gives the best results

Native Apex support gives developers full authority on how to handle files. The real value lies in using that control strategically, aligning compression choices with data type, performance constraints, and system requirements.

In a platform governed by limits, these decisions are what separate functional solutions from truly optimised ones.

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

Written by

Dev Anand

A dynamic engineer, innovative thinker, initiative taker and multi technology professional with exceptional logical, analytical and management skills possess a decade experience in Software Development and Salesforce CRM Solutioning. Enrich experience in converting business needs to Salesforce Experience. Worked on multiple RFPs and POCs. 50+ Integrations between Salesforce and other Platforms. Experience in LWC, Aura, Apex, JS, HTML, PHP, WordPress, Magento and many others.

Get the latest tips, news, updates, advice, inspiration, and more….

Contributor of the month
contributor
Mykyta Lovygin

SFCC Developer | SFCC Technical Architect | Salesforce Consultant | Salesforce Developer | Salesforce Architect |

...
Categories
...
Boost Your Brand's Visibility

Want to promote your products/services in front of more customers?

...

Leave a Reply

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