In nodejs, archiver is used to compress and package some files into compressed packages in zip format or tar format; archiver is a module that can implement packaging functions across platforms. The packaging formats are zip and tar. You can use the "npm install archiver" statement Install this module before use.
The operating environment of this article: Windows 10 system, nodejs version 12.19.0, Dell G3 computer.
Sometimes we need to compress and package some files into compressed packages in zip format or tar format, and we may also need to package directories. In Node.js, you can use the third-party package archiver to perform operations.
Archiver is a module that can realize cross-platform packaging function in nodejs. It can create zip and tar packages. It is a relatively easy-to-use third-party module.
Install the archive module before use.
The code is as follows:
npm install archiverIntroduction:
// Since the file needs to be read, the fs module is required, and const fs = require('fs'); const archiver = require('archiver'); must also be imported.The basic usage is as follows:
// The first step, import the necessary modules const fs = require('fs'); const archiver = require('archiver'); // The second step, create a writable stream to write data const output = fs.createWriteStream (__dirname + "/hello.zip");// Save the compressed package to the directory of the current project, and the compressed package is named test.zipconst archive = archiver('zip', {zlib: {level: 9}}) ;//Set the compression level//The third step is to establish a pipeline connection archive.pipe(output);//The fourth step is to compress the specified file var stream = fs.createReadStream(__dirname + "/hello.txt");// Read hello.txtarchive.append(stream, {name: 'hello.txt'});//The fifth step is to complete the compression archive.finalize();After the code is successfully executed, a compressed package named hello.zip will be generated in the directory where the project is located. The compressed package contains the compressed file hello.txt.
Compressed files can be operated using archive.append() and archive.file().
The API for compressing a single file is as follows:
//Add a file to the compressed package and read the data through a writable stream. Const file1 = __dirname + '/file1.txt';archive.append(fs.createReadStream(file1), { name: 'file1.txt ' });//Add a file to the compressed package and append the file by writing the string to the file archive.append('string cheese!', { name: 'file2.txt' });//Add a File to compressed package, append the file through Buffer data const buffer3 = Buffer.from('buff it!');archive.append(buffer3, { name: 'file3.txt' });// Add a file to the compressed package package, directly pass in the file path archive.file('file1.txt', { name: 'file4.txt' });The complete example is as follows:
// The first step, import the necessary modules const fs = require('fs'); const archiver = require('archiver'); // The second step, create a writable stream to write data const output = fs.createWriteStream (__dirname + "/hello.zip");// Save the compressed package to the directory of the current project, and the compressed package is named test.zipconst archive = archiver('zip', {zlib: {level: 9}}) ;//Set the compression level//The third step is to establish a pipeline connection archive.pipe(output);//The fourth step is to compress the specified file archive.append(fs.createReadStream(__dirname + '/hello.txt'), { name: 'hello.txt'});//File stream archive.append('index.html', {name: 'index.html'});//File path archive.append(Buffer.from("This is Data in Buffer format"), {name: 'buffer.txt'});// Buffer object archive.append("Directly pass in the string", {name: 'string.txt'});// String/ / The fifth step is to complete the compression archive.finalize();Note: The second parameter {name: 'hello.txt'} of archive.append() is to rename the corresponding file in the compressed package.
If you want to compress multiple files, just call the archive.append() method to append files. These additional files will be added to the compressed package. For example:
// The first step, import the necessary modules const fs = require('fs'); const archiver = require('archiver'); // The second step, create a writable stream to write data const output = fs.createWriteStream (__dirname + "/hello.zip");// Save the compressed package to the directory of the current project, and the compressed package is named test.zipconst archive = archiver('zip', {zlib: {level: 9}}) ;//Set the compression level//The third step is to establish a pipeline connection archive.pipe(output);//The fourth step is to compress multiple files into a compressed package archive.append('index.html', {name: ' index.html'});archive.append('hello.js', {name: 'hello.js'});archive.append('hello.html', {name: 'hello.html'});archive .append('db.json', {name: 'db.json'});// The fifth step is to complete the compression archive.finalize();If you want to compress a directory, you need to use archive.directory() to do it. The API is as follows:
// Pack and compress the specified directory into a compressed package, and rename it to new-subdir, and all files in the subdir directory are still in the new-subdir directory, not in the root directory of the compressed package archive.directory('subdir /', 'new-subdir');// Pack and compress all files in the specified directory into a compressed package, and these files are in the root directory of the compressed package, not in the subdirectory archive.directory('subdir/', false);The complete example is as follows:
// The first step, import the necessary modules const fs = require('fs'); const archiver = require('archiver'); // The second step, create a writable stream to write data const output = fs.createWriteStream (__dirname + "/hello.zip");// Save the compressed package to the directory of the current project, and the compressed package is named test.zipconst archive = archiver('zip', {zlib: {level: 9}}) ;//Set the compression level//The third step is to establish a pipeline connection archive.pipe(output);//The fourth step is to compress the directory into a compressed package archive.directory('public/', 'new-public'); archive.directory('demo/', false);// The fifth step is to complete the compression archive.finalize();Recommended learning: "nodejs video tutorial"
The above are the details of how to use archiver in nodejs. For more information, please pay attention to other related articles on this site!