Introduction
Salesforce developers have long awaited the ability to work with compressed files directly in Apex. With the latest release, Salesforce has introduced native support for compressing and extracting ZIP files using the Compression namespace. This feature allows developers to efficiently manage file compression and extraction without relying on external tools.
Benefits of Compress and Extract ZIP Files in Apex
Reduce Storage Usage: Compressing large files helps in optimizing storage space in Salesforce.
Enhance File Transfer Efficiency: Smaller file sizes make data transfers quicker and more efficient.
Improve Data Management: Archiving multiple documents into a single ZIP file simplifies organization.
Understanding the Compression Namespace in Apex
The new Compression namespace provides built-in classes like ZipWriter to create ZIP archives and ZipReader to extract files from a ZIP. However, developers should be mindful of Apex heap size limits, as compression methods can affect the amount of data that can be processed.
How to Zip Files in Apex
Below is a practical example demonstrating how to create a ZIP file containing documents from a Salesforce library and save it back to the same library.
Code Example: Creating and Saving a ZIP File in Salesforce
/**
* This class demonstrates how to use Compression.ZipWriter to create a ZIP file
* containing documents from a specific library and save it back to the same library.
*/
public with sharing class CompressionExample {
/**
* Archives documents from the specified library into a ZIP file and saves it back to the library.
*/
public static void zip() {
// Create an instance of Compression.ZipWriter to handle zip file creation
Compression.ZipWriter zipWriter = new Compression.ZipWriter();
// Get the ID of the ContentWorkspace (library) named 'Salesforce Characters Zip'
Id libraryId = [
SELECT Id
FROM ContentWorkspace
WHERE Name = 'Salesforce Characters Zip'
WITH USER_MODE
][0].Id;
// Retrieve documents from the specified library and add them to the zip writer
for (ContentVersion contentVersion : [
SELECT PathOnClient, VersionData
FROM ContentVersion
WHERE ContentDocument.ParentId = :libraryId
WITH USER_MODE
]) {
zipWriter.addEntry(
contentVersion.PathOnClient,
contentVersion.VersionData
);
}
// Get the resulting ZIP file as a Blob
Blob zippedFile = zipWriter.getArchive();
// Save the ZIP file as a new ContentVersion in the same library
ContentVersion newContentVersion = new ContentVersion();
newContentVersion.VersionData = zippedFile;
newContentVersion.Title = 'Salesforce Characters';
newContentVersion.PathOnClient = 'salesforce_characters.zip';
insert as user newContentVersion;
// Retrieve the ContentDocumentId associated with the saved ContentVersion
newContentVersion = [
SELECT Id, ContentDocumentId
FROM ContentVersion
WHERE Id = :newContentVersion.Id
WITH USER_MODE
LIMIT 1
];
// Share the ZIP file to the library using ContentDocumentLink
ContentDocumentLink contentDocumentLink = new ContentDocumentLink();
contentDocumentLink.ContentDocumentId = newContentVersion.ContentDocumentId;
contentDocumentLink.ShareType = 'I'; // Inferred permission
contentDocumentLink.Visibility = 'AllUsers'; // Shared with all users
contentDocumentLink.LinkedEntityId = libraryId;
insert as user contentDocumentLink;
}
}
Best Practices When Using Apex ZIP Compression
Test Heap Size Limits: Since Apex enforces heap size limits, test different file sizes to determine the optimal compression methods.
Monitor Performance: Compressing large files can impact performance; ensure efficient handling to avoid governor limits.
Error Handling: Implement proper exception handling to prevent failures during file compression or extraction.
Security Considerations: Be cautious when dealing with file storage and sharing permissions within Salesforce.
Conclusion
The ability to compress and extract ZIP files in Apex is a game-changer for Salesforce developers, enabling more efficient file management within the platform. By leveraging the Compression namespace, developers can streamline data storage, optimize file transfers, and enhance overall system performance. Start exploring this powerful feature and integrate it into your Salesforce solutions today!
Comments