GitHub on some practical JavaScript file compression decompression library recommended

  • 2021-01-25 07:08:57
  • OfStack

The project will use archive and unarchive throughout the existing folder, in the search for a solution to try some of the current more popular library, mainly adm-zip, JSZip, archiver, etc.

1.Use adm-zip
adm-zip supports archive and unarchive in 1 or more files or entire folders. It is very easy 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. At the same time to achieve compression and decompression, and as long as the path can be provided to the existing files or folders for operation, to achieve a lot of interfaces, easy to use.
2. bug itself exists, sometimes decompressed file can not be restored to the original file. Hope slowly these bug will fix good.


2. UseJSZip
The library is used by adding files to the zip object and manually adding the contents, and then using a file write operation to convert the in-memory zip object to physical storage. So if it's 1 whole folder, it's a hassle, you have to go through folders.


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;
});

folder("img").file('a.txt') is the same as zip.file("img/a.txt"). It is also important to note that the contents of the file need to be added manually, if only zip.file (" a.txt "); Only the empty txt file is created in the zip object, and it only exists in memory, requiring a file write operation before it is actually saved to disk.

Pros and cons:
1. It is suitable for converting some real-time received data into zip. 2. For the existing folder is inconvenient to operate, you need 1 person to add the content to the ES66en object and then convert it to a file.
3. There's a lot of coding to pay attention to.
4. Compression only.


3. Use archiver and unzip
This combination is the last one I use, which is reliable and easy to use. archiver is very powerful and supports 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();

Extract:


var fs = require("fs");
var unzip = require("unzip");

fs.createReadStream('archiver-unzip.zip').pipe(unzip.Extract({ path: 'unarchive' }));

Pros and cons:
1. After long trial, bug is less.
2. Easy to use, no need to traverse folders.
3. Only provide compression or decompression, no two functions are realized. (So the adm-zip is actually pretty good, but the bug is bad...)

These are just some libraries I found yesterday. Welcome to recommend other libraries mua


Related articles: