Some practical JavaScript file compression and decompression libraries on GitHub are recommended.
Archive and unarchive existing folder are used in the project. when looking for a solution, we 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?
Articles you may be interested in:
- Be careful! AngularJS and RequireJS are used to merge and compress files.
- How php uses gzip to compress and transmit js and css files
- Angularjs client compresses image files and uploads instances
- How to format compressed JS files in Python
- Php: How to compress multiple CSS and JS files
- Detailed description of how to use GruntJS to link and compress multiple JavaScript files
- ANT compression (remove spaces/comments) JS files can improve js Running Speed
- Solution to enable Gzip without compressing js files in IIS7