blob: f8e23232f54f323703e4261ad5170a742f61226a (
plain)
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
|
var io = require('io');
var javaZip = Packages.java.util.zip;
var JavaZipFile = javaZip.ZipFile;
var JavaZipEntry = javaZip.ZipEntry;
exports.unzip = function (source, target) {
if (!target)
target = system.fs.path(source).absolute().dirname();
target = system.fs.path(target);
return new exports.Unzip(source).forEach(function (entry) {
var targetPath = target.join(entry.getName());
if (entry.isDirectory())
return;
targetPath.dirname().mkdirs();
targetPath.write(entry.read('b'), 'b');
});
};
exports.Unzip = function (path) {
this._javaZipFile = JavaZipFile(path);
};
exports.Unzip.prototype.iterator = function () {
var self = this;
var enumeration = this._javaZipFile.entries();
return {
next: function () {
if (!enumeration.hasMoreElements())
throw new StopIteration();
return new exports.Entry(
self._javaZipFile,
enumeration.nextElement()
);
}
};
};
exports.Unzip.prototype.forEach = function (block, context) {
var iterator = this.iterator();
var next;
while (true) {
try {
next = iterator.next();
} catch (exception) {
break;
}
block.call(context, next);
}
};
exports.Entry = function (javaZipFile, javaZipEntry) {
this._javaZipFile = javaZipFile;
this._javaZipEntry = javaZipEntry;
};
exports.Entry.prototype.getName = function () {
return String(this._javaZipEntry.getName());
};
exports.Entry.prototype.isDirectory = function () {
return Boolean(this._javaZipEntry.isDirectory());
};
exports.Entry.prototype.open = function (mode, options) {
// TODO mode and options negotiation
return new io.IO(this._javaZipFile.getInputStream(this._javaZipEntry));
};
exports.Entry.prototype.read = function () {
return this.open().read();
};
exports.Entry.prototype.copy = function (output, mode, options) {
return this.open().copy(output);
};
|