-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
executable file
·158 lines (133 loc) · 3.56 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
var util = require('util'),
async = require('async'),
path = require('path'),
buffer = require('buffer'),
fs = require('fs'),
jszip = require('./jszip');
function FolderZip() {
jszip.JSZip.apply(this, arguments);
}
util.inherits(FolderZip, jszip.JSZip);
function toArrayBuffer(buffer) {
var ab = new ArrayBuffer(buffer.length),
view = new Uint8Array(ab);
for (var i = 0; i < buffer.length; ++i) {
view[i] = buffer[i];
}
return ab;
}
FolderZip.prototype.addFile = function (file, filePath, callback) {
var datas = [],
me = this,
rs = fs.createReadStream(filePath);
rs
.on('data', function (data) {
datas.push(data);
})
.on('end', function () {
var buf = Buffer.concat(datas);
me.file(file, toArrayBuffer(buf), {base64: false, binary: true});
callback();
});
};
FolderZip.prototype.batchAdd = function (files, callback) {
var me = this;
async.each(files, function (item, callback) {
var source = item.source,
target = item.target,
appender = me,
fileName = path.basename(target),
dirname = path.dirname(target);
if (dirname != '.') {
appender = me.folder(dirname);
}
if (source != null && source.trim() != '') {
appender.addFile(fileName, source, function () {
callback();
});
} else {
//if no source ,make the target as folder
me.folder(target);
callback();
}
}, function () {
callback(me);
});
};
FolderZip.prototype.zipFolder = function (folder, options, callback) {
if (typeof options === 'function') {
callback = options;
options = {
excludeParentFolder: false,
parentFolderName: ''
};
}
if (!fs.existsSync(folder)) {
callback(new Error('Folder not found'), me);
} else {
var me = this,
files = fs.readdirSync(folder),
rootFolder = path.basename(folder),
zips = [],
file, stat, targetPath, sourcePath;
while (files.length > 0) {
file = files.shift();
sourcePath = path.join(folder, file);
if (options.excludeParentFolder) {
targetPath = path.join(file);
} else {
var parentFolderNameSpecified = options.parentFolderName ? true : false;
if (parentFolderNameSpecified) {
targetPath = path.join(options.parentFolderName, file);
} else {
targetPath = path.join(rootFolder, file);
}
}
stat = fs.statSync(sourcePath);
if (stat.isFile()) {
zips.push({
target: targetPath,
source: sourcePath
});
} else {
zips.push({
target: targetPath
});
//join the path
async.map(fs.readdirSync(sourcePath), function (item, callback) {
callback(null, path.join(file, item));
}, function (erro, result) {
files = files.concat(result);
});
}
}
me.batchAdd(zips, function () {
callback(null, me)
});
}
};
FolderZip.prototype.writeToResponse = function (response, attachmentName) {
attachmentName = attachmentName || new Date().getTime();
attachmentName += '.zip';
response.setHeader('Content-Disposition', 'attachment; filename="' + attachmentName + '"');
response.write(this.generate({base64: false, compression: 'DEFLATE'}), "binary");
response.end();
};
FolderZip.prototype.writeToFile = function (filePath, callback) {
var data = this.generate({base64: false});
fs.writeFile(filePath, data, 'binary', callback);
};
FolderZip.prototype.writeToFileSync = function (filePath) {
var data = this.generate({base64: false});
fs.writeFileSync(filePath, data, 'binary');
};
FolderZip.prototype.clone = function () {
var newObj = new FolderZip();
for (var i in this) {
if (typeof this[i] !== "function") {
newObj[i] = this[i];
}
}
return newObj;
};
module.exports = FolderZip;