-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzip.class.js
217 lines (204 loc) · 5.29 KB
/
zip.class.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
* Mock-up of the ZipWriter class from VMware Aria Automation with an
* addition of a ZipReader class.
*
* Hint: This approach handles only string data.
*
* @author Stefan Schnell <[email protected]>
* @license MIT
* @version 0.2.0
*
* Hint: This mock-up works only with the Mozilla Rhino JavaScript
* engine.
*
* Checked with Rhino engines version 1.7R4, 1.7.15 and 1.8.0
*/
var ZipWriter = function(file) {
/**
* Creates a new ZipWriter with the given file.
*
* @param file {string} - The file access.
*
* @example
* var zipWriter = new ZipWriter("test.zip");
* zipWriter.addContent("test.txt", "Hello World");
* var mimeAttachment = new MimeAttachment("mimeTest.txt");
* zipWriter.addMimeAttachment(mimeAttachment);
* zipWriter.writeZip();
*/
if (
typeof file !== "undefined" &&
file !== null &&
String(file).trim() !== ""
) {
this.filename = file;
this.contentItems = [];
}
};
ZipWriter.prototype = {
/**
* Adds a string element to the specified zip file.
*
* @param entryName {string} - Name of the elememt in the zip file.
* @param content {string} - Content as a String.
* @param encoding {string} - Encoding type, e.g. UTF-8 or whatever.
*/
addContent : function(entryName, content, encoding) {
if (
entryName === "undefined" ||
entryName === null ||
String(entryName).trim() === ""
) {
throw new Error("entryName argument can not be undefined or null");
}
if (
content === "undefined" ||
content === null ||
String(content).trim() === ""
) {
throw new Error("content argument can not be undefined or null");
}
this.contentItems.push(
{
"entryName": entryName,
"content": content,
"encoding": encoding
}
);
},
/**
* Adds a mime attachement to the specified zip file.
*
* @param mimeAttachment {MimeAttachment} - Element to add to the zip file.
*/
addMimeAttachment : function(mimeAttachment) {
this.contentItems.push(
{
"entryName": mimeAttachment.name,
"content": mimeAttachment.content,
"encoding": null
}
);
},
/**
* Reinitializes the length to 0 and sets the file-pointer in the very
* begining of the file.
*/
clean : function() {
this.contentItems = [];
},
/**
* Returns the class name.<br>
* Hint: This method is a standard.
*
* @returns {string}
*/
getClassName : function() {
return "ZipWriter";
},
/**
* Writes the elements to the zip File.
*/
writeZip : function() {
try {
var zipOutput = java.util.zip.ZipOutputStream(
java.io.BufferedOutputStream(
java.io.FileOutputStream(this.filename)
)
);
this.contentItems.forEach( function(item) {
var entry = java.util.zip.ZipEntry(String(item.entryName));
zipOutput.putNextEntry(entry);
var data;
if (
item.encoding === undefined ||
item.encoding === null ||
String(item.encoding).trim()
) {
data = java.lang.String(item.content).getBytes();
} else {
// https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/nio/charset/Charset.html
data = java.lang.String(item.content).getBytes(
java.lang.String(item.encoding)
);
}
zipOutput.write(data, 0, data.length);
zipOutput.closeEntry();
});
zipOutput.close();
} catch (exception) {
throw new Error(exception);
}
}
};
var ZipReader = function(file) {
/**
* Creates a new ZipReader with the given file.
*
* @param file {string} - The file access.
*
* @example
* var zipReader = new ZipReader("test.zip");
* var data = zipReader.readZip("test.txt");
* java.lang.System.out.println(data);
*/
if (
typeof file !== "undefined" &&
file !== null &&
String(file).trim() !== ""
) {
this.filename = file;
this.contentItems = [];
try {
if (java.io.File(this.filename).exists()) {
var zipInput = java.util.zip.ZipInputStream(
java.io.BufferedInputStream(
java.io.FileInputStream(this.filename)
)
);
var entry = zipInput.getNextEntry();
while(entry !== null) {
var data = java.lang.String(zipInput.readAllBytes());
this.contentItems.push(
{
"entryName": entry.getName(),
"content": data,
"encoding": null
}
);
zipInput.closeEntry();
entry = zipInput.getNextEntry();
}
zipInput.close();
}
} catch (exception) {
throw new Error(exception);
}
}
};
ZipReader.prototype = {
/**
* Returns the class name.<br>
* Hint: This method is a standard.
*
* @returns {string}
*/
getClassName : function() {
return "ZipReader";
},
/**
* Returns a string element to the specified zip file.
*
* @param entryName {string} - Name of the elememt in the zip file.
* @returns {string}
*/
readZip : function(entryName) {
var retValue = "";
this.contentItems.forEach( function(item) {
if (entryName === String(item.entryName)) {
retValue = String(item.content);
}
});
return retValue;
}
};