Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / JU / ZipTools.js
1 Clazz.declarePackage ("JU");
2 Clazz.load (["javajs.api.GenericZipTools"], "JU.ZipTools", ["java.io.BufferedInputStream", "$.IOException", "java.lang.Boolean", "java.util.zip.CRC32", "$.GZIPInputStream", "$.ZipEntry", "$.ZipInputStream", "javajs.api.GenericZipInputStream", "$.ZInputStream", "JU.BArray", "$.Lst", "$.PT", "$.Rdr", "$.SB"], function () {
3 c$ = Clazz.declareType (JU, "ZipTools", null, javajs.api.GenericZipTools);
4 Clazz.makeConstructor (c$, 
5 function () {
6 });
7 Clazz.overrideMethod (c$, "newZipInputStream", 
8 function (is) {
9 return JU.ZipTools.newZIS (is);
10 }, "java.io.InputStream");
11 c$.newZIS = Clazz.defineMethod (c$, "newZIS", 
12  function (is) {
13 return (Clazz.instanceOf (is, javajs.api.ZInputStream) ? is : Clazz.instanceOf (is, java.io.BufferedInputStream) ?  new javajs.api.GenericZipInputStream (is) :  new javajs.api.GenericZipInputStream ( new java.io.BufferedInputStream (is)));
14 }, "java.io.InputStream");
15 Clazz.overrideMethod (c$, "getAllZipData", 
16 function (is, subfileList, name0, binaryFileList, fileData) {
17 var zis = JU.ZipTools.newZIS (is);
18 var ze;
19 var listing =  new JU.SB ();
20 binaryFileList = "|" + binaryFileList + "|";
21 var prefix = JU.PT.join (subfileList, '/', 1);
22 var prefixd = null;
23 if (prefix != null) {
24 prefixd = prefix.substring (0, prefix.indexOf ("/") + 1);
25 if (prefixd.length == 0) prefixd = null;
26 }try {
27 while ((ze = zis.getNextEntry ()) != null) {
28 var name = ze.getName ();
29 if (prefix != null && prefixd != null && !(name.equals (prefix) || name.startsWith (prefixd))) continue;
30 listing.append (name).appendC ('\n');
31 var sname = "|" + name.substring (name.lastIndexOf ("/") + 1) + "|";
32 var asBinaryString = (binaryFileList.indexOf (sname) >= 0);
33 var bytes = JU.Rdr.getLimitedStreamBytes (zis, ze.getSize ());
34 var str;
35 if (asBinaryString) {
36 str = this.getBinaryStringForBytes (bytes);
37 name += ":asBinaryString";
38 } else {
39 str = JU.Rdr.fixUTF (bytes);
40 }str = "BEGIN Directory Entry " + name + "\n" + str + "\nEND Directory Entry " + name + "\n";
41 fileData.put (name0 + "|" + name, str);
42 }
43 } catch (e) {
44 if (Clazz.exceptionOf (e, Exception)) {
45 } else {
46 throw e;
47 }
48 }
49 fileData.put ("#Directory_Listing", listing.toString ());
50 }, "java.io.InputStream,~A,~S,~S,java.util.Map");
51 Clazz.defineMethod (c$, "getBinaryStringForBytes", 
52  function (bytes) {
53 var ret =  new JU.SB ();
54 for (var i = 0; i < bytes.length; i++) ret.append (Integer.toHexString (bytes[i] & 0xFF)).appendC (' ');
55
56 return ret.toString ();
57 }, "~A");
58 Clazz.overrideMethod (c$, "getZipFileDirectory", 
59 function (jzt, bis, list, listPtr, asBufferedInputStream) {
60 var ret;
61 if (list == null || listPtr >= list.length) return this.getZipDirectoryAsStringAndClose (bis);
62 bis = JU.Rdr.getPngZipStream (bis, true);
63 var fileName = list[listPtr];
64 var zis =  new java.util.zip.ZipInputStream (bis);
65 var ze;
66 try {
67 var isAll = (fileName.equals ("."));
68 if (isAll || fileName.lastIndexOf ("/") == fileName.length - 1) {
69 ret =  new JU.SB ();
70 while ((ze = zis.getNextEntry ()) != null) {
71 var name = ze.getName ();
72 if (isAll || name.startsWith (fileName)) ret.append (name).appendC ('\n');
73 }
74 var str = ret.toString ();
75 return (asBufferedInputStream ? JU.Rdr.getBIS (str.getBytes ()) : str);
76 }var pt = fileName.indexOf (":asBinaryString");
77 var asBinaryString = (pt > 0);
78 if (asBinaryString) fileName = fileName.substring (0, pt);
79 fileName = fileName.$replace ('\\', '/');
80 while ((ze = zis.getNextEntry ()) != null && !fileName.equals (ze.getName ())) {
81 }
82 var bytes = (ze == null ? null : JU.Rdr.getLimitedStreamBytes (zis, ze.getSize ()));
83 ze = null;
84 zis.close ();
85 if (bytes == null) return "";
86 if (JU.Rdr.isZipB (bytes) || JU.Rdr.isPngZipB (bytes)) return this.getZipFileDirectory (jzt, JU.Rdr.getBIS (bytes), list, ++listPtr, asBufferedInputStream);
87 if (asBufferedInputStream) return JU.Rdr.getBIS (bytes);
88 if (asBinaryString) {
89 ret =  new JU.SB ();
90 for (var i = 0; i < bytes.length; i++) ret.append (Integer.toHexString (bytes[i] & 0xFF)).appendC (' ');
91
92 return ret.toString ();
93 }if (JU.Rdr.isGzipB (bytes)) bytes = JU.Rdr.getLimitedStreamBytes (this.getUnGzippedInputStream (bytes), -1);
94 return JU.Rdr.fixUTF (bytes);
95 } catch (e) {
96 if (Clazz.exceptionOf (e, Exception)) {
97 return "";
98 } else {
99 throw e;
100 }
101 }
102 }, "javajs.api.GenericZipTools,java.io.BufferedInputStream,~A,~N,~B");
103 Clazz.overrideMethod (c$, "getZipFileContentsAsBytes", 
104 function (bis, list, listPtr) {
105 var ret =  Clazz.newByteArray (0, 0);
106 var fileName = list[listPtr];
107 if (fileName.lastIndexOf ("/") == fileName.length - 1) return ret;
108 try {
109 bis = JU.Rdr.getPngZipStream (bis, true);
110 var zis =  new java.util.zip.ZipInputStream (bis);
111 var ze;
112 while ((ze = zis.getNextEntry ()) != null) {
113 if (!fileName.equals (ze.getName ())) continue;
114 var bytes = JU.Rdr.getLimitedStreamBytes (zis, ze.getSize ());
115 return ((JU.Rdr.isZipB (bytes) || JU.Rdr.isPngZipB (bytes)) && ++listPtr < list.length ? this.getZipFileContentsAsBytes (JU.Rdr.getBIS (bytes), list, listPtr) : bytes);
116 }
117 } catch (e) {
118 if (Clazz.exceptionOf (e, Exception)) {
119 } else {
120 throw e;
121 }
122 }
123 return ret;
124 }, "java.io.BufferedInputStream,~A,~N");
125 Clazz.overrideMethod (c$, "getZipDirectoryAsStringAndClose", 
126 function (bis) {
127 var sb =  new JU.SB ();
128 var s =  new Array (0);
129 try {
130 s = this.getZipDirectoryOrErrorAndClose (bis, null);
131 bis.close ();
132 } catch (e) {
133 if (Clazz.exceptionOf (e, Exception)) {
134 System.out.println (e.toString ());
135 } else {
136 throw e;
137 }
138 }
139 for (var i = 0; i < s.length; i++) sb.append (s[i]).appendC ('\n');
140
141 return sb.toString ();
142 }, "java.io.BufferedInputStream");
143 Clazz.overrideMethod (c$, "getZipDirectoryAndClose", 
144 function (bis, manifestID) {
145 var s =  new Array (0);
146 try {
147 s = this.getZipDirectoryOrErrorAndClose (bis, manifestID);
148 bis.close ();
149 } catch (e) {
150 if (Clazz.exceptionOf (e, Exception)) {
151 System.out.println (e.toString ());
152 } else {
153 throw e;
154 }
155 }
156 return s;
157 }, "java.io.BufferedInputStream,~S");
158 Clazz.defineMethod (c$, "getZipDirectoryOrErrorAndClose", 
159  function (bis, manifestID) {
160 bis = JU.Rdr.getPngZipStream (bis, true);
161 var v =  new JU.Lst ();
162 var zis =  new java.util.zip.ZipInputStream (bis);
163 var ze;
164 var manifest = null;
165 while ((ze = zis.getNextEntry ()) != null) {
166 var fileName = ze.getName ();
167 if (manifestID != null && fileName.startsWith (manifestID)) manifest = JU.ZipTools.getStreamAsString (zis);
168  else if (!fileName.startsWith ("__MACOS")) v.addLast (fileName);
169 }
170 zis.close ();
171 if (manifestID != null) v.add (0, manifest == null ? "" : manifest + "\n############\n");
172 return v.toArray ( new Array (v.size ()));
173 }, "java.io.BufferedInputStream,~S");
174 c$.getStreamAsString = Clazz.defineMethod (c$, "getStreamAsString", 
175 function (is) {
176 return JU.Rdr.fixUTF (JU.Rdr.getLimitedStreamBytes (is, -1));
177 }, "java.io.InputStream");
178 Clazz.overrideMethod (c$, "newGZIPInputStream", 
179 function (is) {
180 return  new java.io.BufferedInputStream ( new java.util.zip.GZIPInputStream (is, 512));
181 }, "java.io.InputStream");
182 Clazz.overrideMethod (c$, "getUnGzippedInputStream", 
183 function (bytes) {
184 try {
185 return JU.Rdr.getUnzippedInputStream (this, JU.Rdr.getBIS (bytes));
186 } catch (e) {
187 if (Clazz.exceptionOf (e, Exception)) {
188 return null;
189 } else {
190 throw e;
191 }
192 }
193 }, "~A");
194 Clazz.overrideMethod (c$, "addZipEntry", 
195 function (zos, fileName) {
196 (zos).putNextEntry ( new java.util.zip.ZipEntry (fileName));
197 }, "~O,~S");
198 Clazz.overrideMethod (c$, "closeZipEntry", 
199 function (zos) {
200 (zos).closeEntry ();
201 }, "~O");
202 Clazz.overrideMethod (c$, "getZipOutputStream", 
203 function (bos) {
204 {
205 return javajs.api.Interface.getInterface(
206 "java.util.zip.ZipOutputStream").setZOS(bos);
207 }}, "~O");
208 Clazz.overrideMethod (c$, "getCrcValue", 
209 function (bytes) {
210 var crc =  new java.util.zip.CRC32 ();
211 crc.update (bytes, 0, bytes.length);
212 return crc.getValue ();
213 }, "~A");
214 Clazz.overrideMethod (c$, "readFileAsMap", 
215 function (bis, bdata, name) {
216 var pt = (name == null ? -1 : name.indexOf ("|"));
217 name = (pt >= 0 ? name.substring (pt + 1) : null);
218 try {
219 if (JU.Rdr.isPngZipStream (bis)) {
220 var isImage = "_IMAGE_".equals (name);
221 if (name == null || isImage) bdata.put ((isImage ? "_DATA_" : "_IMAGE_"),  new JU.BArray (JU.ZipTools.getPngImageBytes (bis)));
222 if (!isImage) this.cacheZipContents (bis, name, bdata, true);
223 } else if (JU.Rdr.isZipS (bis)) {
224 this.cacheZipContents (bis, name, bdata, true);
225 } else if (name == null) {
226 bdata.put ("_DATA_",  new JU.BArray (JU.Rdr.getLimitedStreamBytes (bis, -1)));
227 } else {
228 throw  new java.io.IOException ("ZIP file " + name + " not found");
229 }bdata.put ("$_BINARY_$", Boolean.TRUE);
230 } catch (e) {
231 if (Clazz.exceptionOf (e, java.io.IOException)) {
232 bdata.clear ();
233 bdata.put ("_ERROR_", e.getMessage ());
234 } else {
235 throw e;
236 }
237 }
238 }, "java.io.BufferedInputStream,java.util.Map,~S");
239 Clazz.overrideMethod (c$, "cacheZipContents", 
240 function (bis, fileName, cache, asByteArray) {
241 var zis = JU.ZipTools.newZIS (bis);
242 var ze;
243 var listing =  new JU.SB ();
244 var n = 0;
245 var oneFile = (asByteArray && fileName != null);
246 var pt = (oneFile ? fileName.indexOf ("|") : -1);
247 var file0 = (pt >= 0 ? fileName : null);
248 if (pt >= 0) fileName = fileName.substring (0, pt);
249 try {
250 while ((ze = zis.getNextEntry ()) != null) {
251 var name = ze.getName ();
252 if (fileName != null) {
253 if (oneFile) {
254 if (!name.equalsIgnoreCase (fileName)) continue;
255 } else {
256 listing.append (name).appendC ('\n');
257 }}var nBytes = ze.getSize ();
258 var bytes = JU.Rdr.getLimitedStreamBytes (zis, nBytes);
259 if (file0 != null) {
260 this.readFileAsMap (JU.Rdr.getBIS (bytes), cache, file0);
261 return null;
262 }n += bytes.length;
263 var o = (asByteArray ?  new JU.BArray (bytes) : bytes);
264 cache.put ((oneFile ? "_DATA_" : (fileName == null ? "" : fileName + "|") + name), o);
265 if (oneFile) break;
266 }
267 zis.close ();
268 } catch (e) {
269 if (Clazz.exceptionOf (e, Exception)) {
270 try {
271 zis.close ();
272 } catch (e1) {
273 if (Clazz.exceptionOf (e1, java.io.IOException)) {
274 } else {
275 throw e1;
276 }
277 }
278 return null;
279 } else {
280 throw e;
281 }
282 }
283 if (n == 0 || fileName == null) return null;
284 System.out.println ("ZipTools cached " + n + " bytes from " + fileName);
285 return listing.toString ();
286 }, "java.io.BufferedInputStream,~S,java.util.Map,~B");
287 c$.getPngImageBytes = Clazz.defineMethod (c$, "getPngImageBytes", 
288  function (bis) {
289 try {
290 if (JU.Rdr.isPngZipStream (bis)) {
291 var pt_count =  Clazz.newIntArray (2, 0);
292 JU.Rdr.getPngZipPointAndCount (bis, pt_count);
293 if (pt_count[1] != 0) return JU.ZipTools.deActivatePngZipB (JU.Rdr.getLimitedStreamBytes (bis, pt_count[0]));
294 }return JU.Rdr.getLimitedStreamBytes (bis, -1);
295 } catch (e) {
296 if (Clazz.exceptionOf (e, java.io.IOException)) {
297 return null;
298 } else {
299 throw e;
300 }
301 }
302 }, "java.io.BufferedInputStream");
303 c$.deActivatePngZipB = Clazz.defineMethod (c$, "deActivatePngZipB", 
304  function (bytes) {
305 if (JU.Rdr.isPngZipB (bytes)) bytes[51] = 32;
306 return bytes;
307 }, "~A");
308 });