How to Extract Zip Files in Salesforce: A Comprehensive Guide using JSZip and Zippex. Salesforce developers often encounter scenarios where they need to work with zip files, extracting their contents for further processing or storage. To accomplish this task seamlessly, JavaScript libraries like JSZip and Apex classes like Zippex come to the rescue. In this blog, we will explore how to extract zip files in Salesforce using JSZip and Zippex, providing a step-by-step guide along with code examples.
Understanding JSZip
JSZip is a popular open-source JavaScript library that enables easy zip file manipulation within web applications. It provides methods to create, read, and extract zip files in memory. With JSZip, developers can work with zip files directly in the browser, avoiding the need for server-side processing. Let’s see how to utilize JSZip to extract zip files.
Step 1 Download and Upload JSZip to static resource to use JSZip in your Salesforce Lightning component. You can download it from here
Step 2: Loading the Zip File To extract a zip file using JSZip, you first need to load the zip data into the library. Assuming you have the zip data in Base64 format, you can convert it to a Blob or an ArrayBuffer before passing it to JSZip.
base64ToBlob(base64String, contentType) {
// Convert the base64 string to binary data
const binaryString = window.atob(base64String);
// Create an ArrayBuffer with the binary data
const arrayBuffer = new ArrayBuffer(binaryString.length);
const uint8Array = new Uint8Array(arrayBuffer);
for (let i = 0; i < binaryString.length; i++) {
uint8Array[i] = binaryString.charCodeAt(i);
}
// Create the Blob using the ArrayBuffer and content type
return new Blob([arrayBuffer], { type: contentType });
}
Step 3: Extracting Files from the Zip
Once the zip file is loaded into JSZip, you can extract its contents using the file() and async() methods.
zipInstance.loadAsync(arrayBuffer).then(function (zip) {
Object.keys(zip.files).forEach(function (filename) {
zip.files[filename].async('blob').then(function (fileData) {
const link = document.createElement('a');
link.href = URL.createObjectURL(fileData);
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
console.log(fileData) // These are your file contents
})
})
})
Using the jsZip library is advantageous when there is a requirement to extract or compress files in a single transaction. For instance, if a client triggers a button on the user interface to download multiple files simultaneously, jsZip can be employed to bundle these files into a zip archive directly within the client’s browser. In this scenario, the execution of jsZip takes place on the client-side, alleviating any concerns related to Salesforce’s execution limits.
We can also utilize the Zippex library for extracting our Zip files. In this scenario, the code execution will be performed on the server-side, which may lead to longer processing times for the files. Therefore, it becomes necessary to implement one of the asynchronous approaches. You can get Zippex class code from Github.
Zippex: Apex Class for Zip File Handling
While JSZip is used for client-side zip manipulation, Zippex is an Apex class designed to handle zip files within Salesforce. Zippex allows developers to work with zip files stored as Blob data, extracting individual files for storage or processing.
Step 1: Create Zippex Apex Class
To use Zippex, Create the Apex class into your Salesforce org.
Step 2: Invoking Zippex in Apex
To extract a zip file using Zippex, follow these steps:
public void extractZipFile(Blob zipData) {
Zippex zip = new Zippex(zipData);
for (String fileName : zip.getFileNames()) {
Blob fileData = zip.getFile(fileName);
// Perform operations with the extracted file data
// e.g., create a ContentVersion record for each file
}
}
Conclusion
Extracting zip files in Salesforce is a crucial requirement for many developers. By leveraging JSZip on the client side and Zippex in Apex, developers can efficiently manipulate zip files, extracting and processing their contents. The combination of these two powerful tools ensures seamless zip file handling within Salesforce, enabling developers to build feature-rich applications with enhanced zip file management capabilities. Whether you’re working on file uploads, data imports, or content management systems, mastering the art of zip extraction with JSZip and Zippex is an invaluable skill for Salesforce developers. Happy coding!
I have created a demo code you can look at it here
How can we use Zippex to unzip password protected files? (we have the password but unable to unzip it with Zippex)