archiver-node 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +191 -0
- package/index.js +39 -0
- package/lib/core.js +851 -0
- package/lib/error.js +37 -0
- package/lib/plugins/json.js +79 -0
- package/lib/plugins/tar.js +118 -0
- package/lib/plugins/zip.js +72 -0
- package/lib/utils.js +66 -0
- package/package.json +64 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2012-2014 Chris Talkington, contributors.
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
|
4
|
+
obtaining a copy of this software and associated documentation
|
|
5
|
+
files (the "Software"), to deal in the Software without
|
|
6
|
+
restriction, including without limitation the rights to use,
|
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the
|
|
9
|
+
Software is furnished to do so, subject to the following
|
|
10
|
+
conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be
|
|
13
|
+
included in all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
# Archiver
|
|
2
|
+
|
|
3
|
+
Creates `.zip` or `.tar` archives using Node.js Streams.
|
|
4
|
+
|
|
5
|
+
Forked from [`archiver`](https://www.npmjs.com/package/archiver) package at its unreleased version `8.0.0`.
|
|
6
|
+
|
|
7
|
+
Visit the [API documentation](https://www.archiverjs.com/) for a list of all methods available.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install archiver-node --save
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Use
|
|
16
|
+
|
|
17
|
+
Create a utility class called `ZipArchive`.
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
import { ZipArchive as ZipArchiver } from 'archiver-node'
|
|
21
|
+
|
|
22
|
+
// `WritableStream` doesn't work well with `archiver`.
|
|
23
|
+
// It breaks in certain cases. Use `PassThrough` stream instead.
|
|
24
|
+
// https://github.com/archiverjs/node-archiver/issues/336
|
|
25
|
+
// https://github.com/catamphetamine/archiver-bug
|
|
26
|
+
// import { WritableStream } from 'memory-streams'
|
|
27
|
+
|
|
28
|
+
import Stream, { PassThrough } from 'stream'
|
|
29
|
+
|
|
30
|
+
class ZipArchive {
|
|
31
|
+
constructor() {
|
|
32
|
+
this.outputStream = new PassThrough()
|
|
33
|
+
|
|
34
|
+
const archive = new ZipArchiver({
|
|
35
|
+
// Sets the compression level.
|
|
36
|
+
// zlib: { level: 9 }
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
this.archive = archive
|
|
40
|
+
|
|
41
|
+
this.promise = new Promise((resolve, reject) => {
|
|
42
|
+
// Listens for all archive data to be written.
|
|
43
|
+
// 'close' event is fired when all data has been written.
|
|
44
|
+
this.outputStream.on('close', () => {
|
|
45
|
+
this.size = archive.pointer()
|
|
46
|
+
resolve()
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
// // Listens for all archive data to be written.
|
|
50
|
+
// // `end` event is fired when all archive data has been consumed by a consumer stream.
|
|
51
|
+
// // @see: https://nodejs.org/api/stream.html#stream_event_end
|
|
52
|
+
// archive.on('end', function() {
|
|
53
|
+
// this.size = archive.pointer()
|
|
54
|
+
// resolve()
|
|
55
|
+
// })
|
|
56
|
+
|
|
57
|
+
// Catch "warnings", whatever those're.
|
|
58
|
+
archive.on('warning', function(error) {
|
|
59
|
+
reject(error)
|
|
60
|
+
// The following code sample is from `archiver` README:
|
|
61
|
+
// if (error.code === 'ENOENT') {
|
|
62
|
+
// // `ENOENT` errors happen when a file or folder doesn't exist.
|
|
63
|
+
// // It's not clear what are the cases when it could happen.
|
|
64
|
+
// // And it's not clear why they're dismissed as unimportant here.
|
|
65
|
+
// console.warn(error)
|
|
66
|
+
// } else {
|
|
67
|
+
// reject(error)
|
|
68
|
+
// }
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
// Catch errors.
|
|
72
|
+
archive.on('error', reject)
|
|
73
|
+
|
|
74
|
+
// Pipe archive data to the output stream.
|
|
75
|
+
archive.pipe(this.outputStream)
|
|
76
|
+
})
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @param {(stream.Readable|Buffer|string)} content
|
|
81
|
+
* @param {string} internalPath — Path inside the archive
|
|
82
|
+
*/
|
|
83
|
+
add(content, internalPath) {
|
|
84
|
+
if (content instanceof Stream) {
|
|
85
|
+
// `Stream` is allowed.
|
|
86
|
+
} else if (content instanceof Buffer) {
|
|
87
|
+
// `Buffer` is allowed.
|
|
88
|
+
} else if (typeof content === 'string') {
|
|
89
|
+
// `string` is allowed.
|
|
90
|
+
} else {
|
|
91
|
+
const message = 'Unsupported type of content attempted to be added to a .zip archive'
|
|
92
|
+
console.log(message + ':')
|
|
93
|
+
console.log(content)
|
|
94
|
+
throw new Error(message)
|
|
95
|
+
}
|
|
96
|
+
this.archive.append(content, { name: internalPath })
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* @param {string} filePath — Path to file in the filesystem
|
|
101
|
+
* @param {string} internalPath — Path inside the archive
|
|
102
|
+
*/
|
|
103
|
+
includeFile(filePath, internalPath) {
|
|
104
|
+
this.archive.file(filePath, { name: internalPath })
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* @param {string} directoryPath — Path to directory in the filesystem.
|
|
109
|
+
* @param {string} filePathPattern — File path "glob" pattern. Example: "file*.txt".
|
|
110
|
+
*/
|
|
111
|
+
includeFilesByMatch(directoryPath, filePathPattern) {
|
|
112
|
+
this.archive.glob(filePathPattern, { cwd: directoryPath })
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* @param {string} directoryPath — Path to directory in the filesystem
|
|
117
|
+
* @param {string} [internalPath] — Path inside the archive. Omitting this argument will put the contents of the directory to the root of the archive.
|
|
118
|
+
*/
|
|
119
|
+
includeDirectory(directoryPath, internalPath) {
|
|
120
|
+
this.archive.directory(directoryPath, internalPath || false);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Starts the process of writing the archive file data.
|
|
125
|
+
* @returns {stream.Readable}
|
|
126
|
+
*/
|
|
127
|
+
write() {
|
|
128
|
+
// `.finalize()` starts the process of writing the archive file.
|
|
129
|
+
//
|
|
130
|
+
// `.finalize()` also returns some kind of `Promise` but it's some kind of a weird one
|
|
131
|
+
// and is not meant to be `await`ed or anything like that.
|
|
132
|
+
// https://github.com/archiverjs/node-archiver/issues/772
|
|
133
|
+
//
|
|
134
|
+
// "close", "end" or "finish" events may be fired on `this.archive`
|
|
135
|
+
// right after calling this method, so any required event handlers
|
|
136
|
+
// should have been added beforehand.
|
|
137
|
+
//
|
|
138
|
+
this.archive.finalize()
|
|
139
|
+
|
|
140
|
+
// Returns a readable `Stream` with the `.zip` archive data.
|
|
141
|
+
return this.outputStream
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Returns the size of the resulting archive.
|
|
146
|
+
* Returns `undefined` until the archive has been written.
|
|
147
|
+
* @returns {number | undefined}
|
|
148
|
+
*/
|
|
149
|
+
getSize() {
|
|
150
|
+
return this.size
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
Use the utility class `ZipArchive`.
|
|
156
|
+
|
|
157
|
+
```js
|
|
158
|
+
const archive = new ZipArchive()
|
|
159
|
+
|
|
160
|
+
// append a file from stream
|
|
161
|
+
const file1 = __dirname + "/file1.txt";
|
|
162
|
+
archive.add(fs.createReadStream(file1), "file1.txt");
|
|
163
|
+
|
|
164
|
+
// append a file from string
|
|
165
|
+
archive.add("string cheese!", "file2.txt");
|
|
166
|
+
|
|
167
|
+
// append a file from buffer
|
|
168
|
+
const buffer3 = Buffer.from("buff it!");
|
|
169
|
+
archive.add(buffer3, "file3.txt");
|
|
170
|
+
|
|
171
|
+
// append a file
|
|
172
|
+
archive.includeFile("/path/to/file1.txt", "file4.txt");
|
|
173
|
+
|
|
174
|
+
// append files from a sub-directory and naming it `new-sub-directory` within the archive
|
|
175
|
+
archive.includeDirectory("/path/to/sub-directory/", "new-sub-directory");
|
|
176
|
+
|
|
177
|
+
// append files from a sub-directory, putting its contents at the root of archive
|
|
178
|
+
archive.includeDirectory("/path/to/sub-directory/");
|
|
179
|
+
|
|
180
|
+
// append files from a glob pattern
|
|
181
|
+
archive.includeFilesByMatch("/path/to/some/directory/", "file*.txt");
|
|
182
|
+
|
|
183
|
+
// finalize the archive (i.e. we are done appending files).
|
|
184
|
+
const archiveDataStream = archive.write();
|
|
185
|
+
|
|
186
|
+
archiveDataStream.pipe(...);
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
## Formats
|
|
190
|
+
|
|
191
|
+
Archiver ships with out of the box support for TAR and ZIP archives.
|
package/index.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import Archiver from "./lib/core.js";
|
|
2
|
+
import Zip from "./lib/plugins/zip.js";
|
|
3
|
+
import Tar from "./lib/plugins/tar.js";
|
|
4
|
+
import Json from "./lib/plugins/json.js";
|
|
5
|
+
|
|
6
|
+
export { Archiver };
|
|
7
|
+
|
|
8
|
+
export class ZipArchive extends Archiver {
|
|
9
|
+
constructor(options) {
|
|
10
|
+
super(options);
|
|
11
|
+
this._format = "zip";
|
|
12
|
+
this._module = new Zip(options);
|
|
13
|
+
this._supportsDirectory = true;
|
|
14
|
+
this._supportsSymlink = true;
|
|
15
|
+
this._modulePipe();
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class TarArchive extends Archiver {
|
|
20
|
+
constructor(options) {
|
|
21
|
+
super(options);
|
|
22
|
+
this._format = "tar";
|
|
23
|
+
this._module = new Tar(options);
|
|
24
|
+
this._supportsDirectory = true;
|
|
25
|
+
this._supportsSymlink = true;
|
|
26
|
+
this._modulePipe();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export class JsonArchive extends Archiver {
|
|
31
|
+
constructor(options) {
|
|
32
|
+
super(options);
|
|
33
|
+
this._format = "json";
|
|
34
|
+
this._module = new Json(options);
|
|
35
|
+
this._supportsDirectory = true;
|
|
36
|
+
this._supportsSymlink = true;
|
|
37
|
+
this._modulePipe();
|
|
38
|
+
}
|
|
39
|
+
}
|