This article mainly introduces some practical JavaScript file compression and decompression library recommendations on GitHub. We recommend that you all support zip format, for more information, see the existing folder of archive and unarchive in the project. when looking for a solution, I tried some popular libraries, including adm-zip, JSZip and archiver.
I. Use adm-zip
Adm-zip supports the archive and unarchive functions of one or more files or the entire folder, which is very simple and convenient to use.
var adm_zip = require('adm-zip'); //creating archives var zip = new adm_zip(); zip.addLocalFolder('archiver'); zip.writeZip('adm/adm-archive.zip'); //extracting archives var unzip = new adm_zip('adm/adm-archive.zip'); unzip.extractAllTo("adm/adm-unarchive/", /*overwrite*/true);
Pros and cons:
1. Both compression and decompression are implemented, and operations can be performed on existing files or folders as long as a path is provided, implementing many interfaces, making it easy to use.
2. There is a bug in itself, and sometimes the unzipped file cannot be restored to the original file. Hope these bugs will fix them slowly.
Ii. UseJSZip
When using this library, you need to add files to the zip object one by one, and manually add the content, and then use the Write File Operation to convert the zip object in the memory to physical storage. Therefore, it is very difficult for a whole folder to traverse the folder.
var JSZip = require("jszip");var fs = require("fs");var zip = new JSZip();var file_content = fs.readFileSync('archive/a.txt');zip.file("a.txt",file_content);var data = fs.readFileSync("archive/img/pic.jpeg");zip.file("img/pic.jpeg", data, {base64: true});var zipfolder = zip.generate({type:"nodebuffer"});fs.writeFile("jszip.zip", zipfolder, function(err) { if (err) throw err;});
There is also a folder method in JSZip, but it is only used to switch the virtual path inside the zip object, such as zip. folder ("img" ).file('a.txt'folder) downloads an imgsubdirectory in zips and creates a.txt in the lower part. The effect is equivalent to zip. file ("img/a.txt "). Note that the file content must be manually added, if it is only zip. file ("a.txt"); only a txt file with an empty content is created in the zip object, and it only exists in the memory. You need to write the file before it is actually saved to the disk.
Pros and cons:
1. It is applicable to converting some real-time received data to zip. 2. For the inconvenience of folder operations, you need to add the content to the zip object one by one and convert it into a file.
3. Pay attention to many codes.
4. Only compression is supported.
Iii. Use archiver and unzip
This combination is used at the end. It is reliable and easy to use. archiver is powerful and supports the tar format. You only need to provide a path to compress the existing folder. Compression:
var fs = require('fs');var archiver = require('archiver');var output = fs.createWriteStream('archiver-unzip.zip');var archive = archiver('zip');archive.on('error', function(err){ throw err;});archive.pipe(output);archive.bulk([ { src: ['archiver/**']}]);archive.finalize();
Decompress:
var fs = require("fs");var unzip = require("unzip");fs.createReadStream('archiver-unzip.zip').pipe(unzip.Extract({ path: 'unarchive' }));
Pros and cons:
1. It has been tried for a long time and has fewer bugs.
2. Easy to use, no need to traverse folders.
3. Only compression or decompression are provided, and none of them are implemented. (So adm-zip is actually very useful, but the bug is hard to crack ...)
These are just some libraries I found yesterday. Are you welcome to recommend other libraries?