forked from mwilliamson/mammoth.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzipfile.js
77 lines (64 loc) · 1.75 KB
/
zipfile.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
var JSZip = require("jszip");
var promises = require("./promises");
exports.openArrayBuffer = openArrayBuffer;
exports.splitPath = splitPath;
exports.joinPath = joinPath;
function openArrayBuffer(arrayBuffer) {
var zipFile = new JSZip(arrayBuffer);
function exists(name) {
return zipFile.file(name) !== null;
}
function read(name, encoding) {
var array = zipFile.file(name).asUint8Array();
var buffer = uint8ArrayToBuffer(array);
if (encoding) {
return promises.when(buffer.toString(encoding));
} else {
return promises.when(buffer);
}
}
function write(name, contents) {
zipFile.file(name, contents);
}
function toBuffer() {
return zipFile.generate({type: "nodebuffer"});
}
return {
exists: exists,
read: read,
write: write,
toBuffer: toBuffer
};
}
function uint8ArrayToBuffer(array) {
if (Buffer.from && Buffer.from !== Uint8Array.from) {
return Buffer.from(array);
} else {
return new Buffer(array);
}
}
function splitPath(path) {
var lastIndex = path.lastIndexOf("/");
if (lastIndex === -1) {
return {dirname: "", basename: path};
} else {
return {
dirname: path.substring(0, lastIndex),
basename: path.substring(lastIndex + 1)
};
}
}
function joinPath() {
var nonEmptyPaths = Array.prototype.filter.call(arguments, function(path) {
return path;
});
var relevantPaths = [];
nonEmptyPaths.forEach(function(path) {
if (/^\//.test(path)) {
relevantPaths = [path];
} else {
relevantPaths.push(path);
}
});
return relevantPaths.join("/");
}