How to Encrypt a Zip File in JavaScript

If you need to share large files, one of the most efficient formats to utilize is the ZIP format; this will compress your files into a compact zip folder that won’t overwhelm your recipient’s storage space. Additionally, ZIP files can be easily protected through encryption and password-protection, ensuring that the information can’t be accessed without your knowledge. The following API will allow you to create new ZIP files and apply encryption and password protection in JavaScript.

To get things started, we will run this command to install the jQuery library:

bower install jquery

For the next step, we are going to call the function; you will need the file or files (up to 10) you want to compress, the password for the zip file, and the encryption algorithm (default is AES-256):

var data = new FormData();
data.append("inputFile1", fileInput.files[0], "file");
data.append("inputFile2", fileInput.files[0], "file");
data.append("inputFile3", fileInput.files[0], "file");
data.append("inputFile4", fileInput.files[0], "file");
data.append("inputFile5", fileInput.files[0], "file");
data.append("inputFile6", fileInput.files[0], "file");
data.append("inputFile7", fileInput.files[0], "file");
data.append("inputFile8", fileInput.files[0], "file");
data.append("inputFile9", fileInput.files[0], "file");
data.append("inputFile10", fileInput.files[0], "file");

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function() {
if(this.readyState === 4) {
console.log(this.responseText);
}
});
xhr.open("POST", "https://api.cloudmersive.com/convert/archive/zip/create/encrypted");
xhr.setRequestHeader("password", "<string>");
xhr.setRequestHeader("encryptionAlgorithm", "<string>");
xhr.setRequestHeader("Content-Type", "multipart/form-data");
xhr.setRequestHeader("Apikey", "YOUR-API-KEY-HERE");
xhr.send(data);

To retrieve your personal API key, head to Cloudmersive website to register for a free account; this will give you access to 800 calls/month across our entire library of APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store