-
Notifications
You must be signed in to change notification settings - Fork 17
/
easy-zip.js
143 lines (120 loc) · 3.27 KB
/
easy-zip.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
var util = require('util'),
async = require('async'),
path = require('path'),
buffer = require('buffer'),
fs = require('fs'),
jszip = require('./jszip');
function EasyZip(){
jszip.JSZip.apply(this,arguments);
}
util.inherits(EasyZip, 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;
}
EasyZip.prototype.addFile = function(file,filePath,callback){
var datas = [],
me = this,
rs = fs.createReadStream(filePath);
rs.on('data',function(data){
datas.push(data);
})
rs.on('end',function(){
var buf = Buffer.concat(datas);
me.file(file, toArrayBuffer(buf),{base64:false, binary: true});
callback();
})
}
EasyZip.prototype.batchAdd = function(files,callback) {
var me = this;
async.each(files,function(item,callback){
var source = item.source,
target = item.target,
appender = me,
folder = item.folder,
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);
});
}
EasyZip.prototype.zipFolder = function(folder, callback, options) {
if(!fs.existsSync(folder)){
callback(new Error('Folder not found'),me);
}else{
options = options || {};
var me = this,
files = fs.readdirSync(folder),
rootFolder = options.rootFolder || path.basename(folder),
zips = [],
file,stat,targetPath,sourcePath;
while(files.length > 0){
file = files.shift();
sourcePath = path.join(folder,file);
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)});
}
}
EasyZip.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();
}
EasyZip.prototype.writeToFile = function(filePath,callback){
var data = this.generate({base64:false,compression:'DEFLATE'});
fs.writeFile(filePath, data, 'binary', (err) => {
if (err) {
console.log(err);
}
});
}
EasyZip.prototype.writeToFileSycn = EasyZip.prototype.writeToFileSync = function(filePath){
var data = this.generate({base64:false,compression:'DEFLATE'});
fs.writeFileSync(filePath, data, 'binary');
}
EasyZip.prototype.clone = function() {
var newObj = new EasyZip();
for (var i in this) {
if (typeof this[i] !== "function") {
newObj[i] = this[i];
}
}
return newObj;
}
exports.EasyZip = EasyZip;