Some practical JavaScript on GitHub file Compression decompression Library recommendation _javascript Tips

Source: Internet
Author: User
Tags zip

Project to use archive and unarchive the entire existing folder, in the search for solutions to try some of the current more popular library, mainly Adm-zip, Jszip, archiver and so on.

A. Use Adm-zip
Adm-zip supports archive and unarchive the functionality of one or more files or entire folders, and 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 the compression and decompression, and as long as the path can be provided to the existing files or folders to operate, realize a lot of interfaces, easy to use.
2. There is a bug in itself, sometimes decompression files can not be restored to the original file. Hope slowly, these bugs will fix you up.


two. Usejszip
This library needs to add files to the Zip object one at a time, and it needs to be added manually, using a write-file operation to convert the Zip object in memory to physical storage. So if it's an entire folder, it's a hassle, and you need 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;
});

Jszip also has a folder method, but it is only used to toggle the virtual path inside the Zip object, such as Zip.folder ("img"). File (' a.txt ') adds an IMG subdirectory to the zip. Create the a.txt below, with the effect equivalent to Zip.file ("Img/a.txt"). It is also important to note that the contents of the file need to be added manually, if it is simply Zip.file ("A.txt"), only a TXT file with an empty content is created in the Zip object, and it only exists in memory, which requires a write file operation before it is actually saved to disk.

Pros and Cons:
1. For some of the data received in real time into the comparison of the zip application. 2. For existing folder operation inconvenience, need to add content to the Zip object, and then into a file.
3. A lot of coding needs attention.
4. Only compression function.


three. Use Archiver and Unzip
This combination is the last I use, the more reliable, the use is relatively simple, where archiver is very powerful, support the ZIP format tar format, only need to provide a path can compress existing folders. 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. Trial-tested with fewer bugs.
2. Easy to use, do not need to traverse the folder.
3. All only provide compression or decompression, no two functions are achieved. (So adm-zip in fact very good, but the bug is a mishap ah ...)

These are just some of the libraries I looked for yesterday, welcome to recommend other libraries

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.