@@ -13,33 +13,34 @@ npm install archiver --save
13
13
## Quick Start
14
14
15
15
``` js
16
- // require modules
17
- const fs = require (' fs' );
18
- const archiver = require (' archiver' );
16
+ import fs from " fs" ;
17
+ import { ZipArchive } from " archiver" ;
19
18
20
19
// create a file to stream archive data to.
21
- const output = fs .createWriteStream (__dirname + ' /example.zip' );
22
- const archive = archiver ( ' zip ' , {
23
- zlib: { level: 9 } // Sets the compression level.
20
+ const output = fs .createWriteStream (__dirname + " /example.zip" );
21
+ const archive = new ZipArchive ( {
22
+ zlib: { level: 9 }, // Sets the compression level.
24
23
});
25
24
26
25
// listen for all archive data to be written
27
26
// 'close' event is fired only when a file descriptor is involved
28
- output .on (' close' , function () {
29
- console .log (archive .pointer () + ' total bytes' );
30
- console .log (' archiver has been finalized and the output file descriptor has closed.' );
27
+ output .on (" close" , function () {
28
+ console .log (archive .pointer () + " total bytes" );
29
+ console .log (
30
+ " archiver has been finalized and the output file descriptor has closed." ,
31
+ );
31
32
});
32
33
33
34
// This event is fired when the data source is drained no matter what was the data source.
34
35
// It is not part of this library but rather from the NodeJS Stream API.
35
36
// @see: https://nodejs.org/api/stream.html#stream_event_end
36
- output .on (' end' , function () {
37
- console .log (' Data has been drained' );
37
+ output .on (" end" , function () {
38
+ console .log (" Data has been drained" );
38
39
});
39
40
40
41
// good practice to catch warnings (ie stat failures and other non-blocking errors)
41
- archive .on (' warning' , function (err ) {
42
- if (err .code === ' ENOENT' ) {
42
+ archive .on (" warning" , function (err ) {
43
+ if (err .code === " ENOENT" ) {
43
44
// log warning
44
45
} else {
45
46
// throw error
@@ -48,35 +49,35 @@ archive.on('warning', function(err) {
48
49
});
49
50
50
51
// good practice to catch this error explicitly
51
- archive .on (' error' , function (err ) {
52
+ archive .on (" error" , function (err ) {
52
53
throw err;
53
54
});
54
55
55
56
// pipe archive data to the file
56
57
archive .pipe (output);
57
58
58
59
// append a file from stream
59
- const file1 = __dirname + ' /file1.txt' ;
60
- archive .append (fs .createReadStream (file1), { name: ' file1.txt' });
60
+ const file1 = __dirname + " /file1.txt" ;
61
+ archive .append (fs .createReadStream (file1), { name: " file1.txt" });
61
62
62
63
// append a file from string
63
- archive .append (' string cheese!' , { name: ' file2.txt' });
64
+ archive .append (" string cheese!" , { name: " file2.txt" });
64
65
65
66
// append a file from buffer
66
- const buffer3 = Buffer .from (' buff it!' );
67
- archive .append (buffer3, { name: ' file3.txt' });
67
+ const buffer3 = Buffer .from (" buff it!" );
68
+ archive .append (buffer3, { name: " file3.txt" });
68
69
69
70
// append a file
70
- archive .file (' file1.txt' , { name: ' file4.txt' });
71
+ archive .file (" file1.txt" , { name: " file4.txt" });
71
72
72
73
// append files from a sub-directory and naming it `new-subdir` within the archive
73
- archive .directory (' subdir/' , ' new-subdir' );
74
+ archive .directory (" subdir/" , " new-subdir" );
74
75
75
76
// append files from a sub-directory, putting its contents at the root of archive
76
- archive .directory (' subdir/' , false );
77
+ archive .directory (" subdir/" , false );
77
78
78
79
// append files from a glob pattern
79
- archive .glob (' file*.txt' , {cwd: __dirname });
80
+ archive .glob (" file*.txt" , { cwd: __dirname });
80
81
81
82
// finalize the archive (ie we are done appending files but streams have to finish yet)
82
83
// 'close', 'end' or 'finish' may be fired right after calling this method so register to them beforehand
@@ -86,7 +87,3 @@ archive.finalize();
86
87
## Formats
87
88
88
89
Archiver ships with out of the box support for TAR and ZIP archives.
89
-
90
- You can register additional formats with ` registerFormat ` .
91
-
92
- You can check if format already exists before to register a new one with ` isRegisteredFormat ` .
0 commit comments