Home > Article > Web Front-end > Recommendations for some practical JavaScript file compression and decompression libraries on GitHub_javascript skills
The entire existing folder of archive and unarchive needs to be used in the project. When looking for a solution, I tried some popular libraries, mainly adm-zip, JSZip, archiver, etc.
1.Use adm-zip
adm-zip supports the function of archive and unarchive of one or more files or an 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. It realizes compression and decompression at the same time, and can operate on existing files or folders as long as the path is provided. It implements many interfaces and is easy to use.
2. There is a bug in itself. Sometimes the decompressed file cannot be restored to the original file. Hopefully these bugs will be fixed over time.
2. UseJSZip
When using this library, you need to add files one by one to the zip object, and you need to add the content manually, and then use the file writing operation to convert the zip object in the memory into physical storage. So if it is for an entire folder, it is very troublesome and needs 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. For example, zip.folder("img").file('a.txt') is to add an img subdirectory to the zip , create a.txt below, the effect is equivalent to zip.file("img/a.txt"). It should also be noted here that the contents of the file need to be added manually. If it is just zip.file("a.txt"); it only creates a txt file with empty content in the zip object, and it only exists in memory. , it needs to be written to the file before it is actually saved to the disk.
Pros and cons:
1. It is more suitable for converting some data received in real time into zip. 2. It is inconvenient to operate existing folders. You need to add the contents to the zip object one by one and then convert them into files.
3. A lot of coding needs attention.
4. Only compression function.
3. Use archiver and unzip
This combination is the last one I used. It is more reliable and easier to use. The archiver is very powerful and supports the zip format tar format. You only need to provide the 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();
Unzip:
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. They only provide compression or decompression, not both functions. (So adm-zip is actually very useful, but bugs are a flaw...)
These are just some of the libraries I found yesterday. You are welcome to recommend other libraries