1 | 'use strict';
|
2 |
|
3 | var fs = require('fs');
|
4 | var map = require('map-key');
|
5 | var mkdir = require('mkdirp');
|
6 | var path = require('path');
|
7 | var pipeline = require('stream-combiner');
|
8 | var rm = require('rimraf');
|
9 | var tempfile = require('tempfile');
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | function Decompress(opts) {
|
25 | opts = opts || {};
|
26 | this.opts = opts;
|
27 | this.path = opts.path || process.cwd();
|
28 | this.ext = opts.ext || '';
|
29 | this.strip = +opts.strip || 0;
|
30 | this.extractors = {
|
31 | '.zip': this._extractZip,
|
32 | '.tar': this._extractTar,
|
33 | '.tar.gz': this._extractTarGz,
|
34 | '.tgz': this._extractTarGz,
|
35 | 'application/zip': this._extractZip,
|
36 | 'application/x-gzip': this._extractTarGz,
|
37 | 'application/x-tar': this._extractTar,
|
38 | 'application/x-tgz': this._extractTarGz
|
39 | };
|
40 | this.extractor = this._getExtractor(this.ext);
|
41 | }
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | Decompress.prototype.extract = function () {
|
50 | var self = this;
|
51 | var stream = this.extractor();
|
52 |
|
53 | if (!fs.existsSync(this.path)) {
|
54 | mkdir.sync(self.path);
|
55 | }
|
56 |
|
57 | return stream;
|
58 | };
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | Decompress.prototype.canExtract = function (src, mime) {
|
69 | if (this._getExtractor(src)) {
|
70 | return true;
|
71 | }
|
72 |
|
73 | if (mime && this._getExtractor(mime)) {
|
74 | return true;
|
75 | }
|
76 |
|
77 | return false;
|
78 | };
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | Decompress.prototype._getExtractor = function (src) {
|
88 | src = src.toLowerCase();
|
89 | return map(this.extractors, src);
|
90 | };
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | Decompress.prototype._extractZip = function () {
|
99 | var AdmZip = require('adm-zip');
|
100 | var tmp = tempfile('.zip');
|
101 | var self = this;
|
102 | var stream = fs.createWriteStream(tmp);
|
103 | var zip;
|
104 |
|
105 | stream.on('close', function () {
|
106 | zip = new AdmZip(tmp);
|
107 |
|
108 | zip.getEntries().forEach(function (entry) {
|
109 | if (!entry.isDirectory) {
|
110 | var dest;
|
111 | var dir = path.dirname(entry.entryName.toString()).split(path.sep);
|
112 | var file = path.basename(entry.rawEntryName.toString());
|
113 |
|
114 | if (self.strip) {
|
115 | dir = dir.slice(self.strip);
|
116 | }
|
117 |
|
118 | dest = path.join(self.path, dir.join(path.sep), file);
|
119 |
|
120 | mkdir.sync(path.dirname(dest));
|
121 | fs.writeFileSync(dest, entry.getData());
|
122 |
|
123 | if (self.opts.mode) {
|
124 | fs.chmodSync(dest, self.opts.mode);
|
125 | }
|
126 | }
|
127 | });
|
128 |
|
129 | rm.sync(tmp);
|
130 | });
|
131 |
|
132 | return stream;
|
133 | };
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | Decompress.prototype._extractTar = function () {
|
142 | var tar = require('tar');
|
143 | var stream = tar.Extract(this.opts);
|
144 |
|
145 | return stream;
|
146 | };
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 | Decompress.prototype._extractTarGz = function () {
|
155 | var tar = require('tar');
|
156 | var zlib = require('zlib');
|
157 | var stream = zlib.Unzip();
|
158 | var dest = tar.Extract(this.opts);
|
159 |
|
160 | return pipeline(stream, dest);
|
161 | };
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 | module.exports = function (opts) {
|
168 | var decompress = new Decompress(opts);
|
169 | return decompress.extract();
|
170 | };
|
171 |
|
172 | module.exports.canExtract = function (src, mime) {
|
173 | var decompress = new Decompress();
|
174 | return decompress.canExtract(src, mime);
|
175 | };
|