|
5 | 5 | import java.io.FileInputStream; |
6 | 6 | import java.io.InputStream; |
7 | 7 | import java.net.URL; |
| 8 | +import java.util.Arrays; |
8 | 9 | import java.util.zip.GZIPInputStream; |
9 | 10 | import java.util.zip.ZipEntry; |
10 | 11 | import java.util.zip.ZipInputStream; |
11 | 12 |
|
| 13 | +import javajs.util.AU; |
12 | 14 | import javajs.util.Rdr; |
13 | 15 | import javajs.util.ZipTools; |
14 | 16 |
|
15 | 17 | public class Test_Zipin extends Test_ { |
16 | 18 |
|
| 19 | +static boolean j2sHeadless = true; |
| 20 | + |
17 | 21 | @SuppressWarnings("unused") |
18 | 22 | public static void main(String[] args) { |
19 | 23 |
|
20 | 24 | try { |
21 | | - |
22 | 25 |
|
23 | 26 | InputStream is = Test_Zipin.class.getResourceAsStream("3c9k.xml.gz"); |
24 | | - GZIPInputStream gzis = new GZIPInputStream(is, 8096); |
25 | | - String s = new String((byte[]) Rdr.getStreamAsBytes(new BufferedInputStream(gzis), null), "utf-8"); |
| 27 | + |
| 28 | + System.out.println("unzipping"); |
| 29 | + GZIPInputStream gzis = new GZIPInputStream(is, 8096); |
| 30 | + BufferedInputStream gis = new BufferedInputStream(gzis); |
| 31 | + byte[] buf = new byte[8096]; |
| 32 | + byte[] bytes = new byte[buf.length]; |
| 33 | + int len = 0; |
| 34 | + int totalLen = 0; |
| 35 | + while ((len = gis.read(buf, 0, buf.length)) > 0) { |
| 36 | + totalLen += len; |
| 37 | + if (totalLen >= bytes.length) |
| 38 | + bytes = Arrays.copyOf(bytes, bytes.length << 1); |
| 39 | + System.arraycopy(buf, 0, bytes, totalLen - len, len); |
| 40 | + } |
| 41 | + gis.close(); |
| 42 | + if (bytes.length > totalLen) { |
| 43 | + bytes = Arrays.copyOf(bytes, totalLen); |
| 44 | + } |
| 45 | + String s = new String(bytes); |
26 | 46 | System.out.println(s.length()); |
27 | 47 | assert (s.length() == 2957648); |
28 | 48 | gzis.close(); |
29 | 49 |
|
30 | | - |
31 | 50 | InputStream bzis = Test_Zipin.class.getResourceAsStream("test2.txt.bz2"); |
32 | 51 | bzis = ZipTools.newBZip2InputStream(bzis); |
33 | 52 | s = new String((byte[]) Rdr.getStreamAsBytes(new BufferedInputStream(bzis), null), "utf-8"); |
34 | 53 | System.out.println(s); |
35 | 54 | assert (s.equals("test2 here")); |
36 | 55 | bzis.close(); |
| 56 | + |
37 | 57 |
|
38 | | - String path = (/** @j2sNative 1?"https://./swingjs/j2s/test/t.zip": */ |
39 | | - "https://chemapps.stolaf.edu/jmol/jsmol/data/sage.zip"); |
40 | | - URL url = new URL(path); |
41 | | - System.out.println("getting " + url); |
42 | | - InputStream fis = url.openStream(); |
43 | | - ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis)); |
44 | | - ZipEntry ze = zis.getNextEntry(); |
45 | | - String test = ze.getName() + " " + ze.getSize(); |
46 | | - System.out.println(test); |
47 | | - assert (test.equals("[Content_Types].xml 1548") || test.equals("obj_490758.pmesh 95301"));// "obj_490758.pmesh |
48 | | - // 95301")); |
49 | | - int hashcode = 0; |
50 | | - byte[] bytes = null; |
51 | | - out: while (ze != null) { |
52 | | - while ((ze = zis.getNextEntry()) != null && ze.isDirectory()) { |
53 | | - } |
54 | | - ; |
55 | | - if (ze.getName().equals("xl/calcChain.xml")) { |
56 | | - bytes = new byte[(int) ze.getSize()]; |
57 | | - zis.read(bytes); |
58 | | - break out; |
59 | | - } |
60 | | - } |
61 | | - zis.close(); |
62 | | - s = new String(bytes); |
63 | | - hashcode = s.hashCode(); |
64 | | - System.out.println("hashcode = " + hashcode); |
65 | 58 | if (/** @j2sNative true || */ |
66 | | - false) { |
67 | | - bytes = ze.getBytes(); |
| 59 | + false) { |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + String path = (/** @j2sNative 1?"https://./swingjs/j2s/test/t.zip": */ |
| 64 | + "https://chemapps.stolaf.edu/jmol/jsmol/data/sage.zip"); |
| 65 | + URL url = new URL(path); |
| 66 | + System.out.println("getting " + url); |
| 67 | + InputStream fis = url.openStream(); |
| 68 | + ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis)); |
| 69 | + ZipEntry ze = zis.getNextEntry(); |
| 70 | + String test = ze.getName() + " " + ze.getSize(); |
| 71 | + System.out.println(test); |
| 72 | + assert (test.equals("[Content_Types].xml 1548") || test.equals("obj_490758.pmesh 95301"));// "obj_490758.pmesh |
| 73 | + // 95301")); |
| 74 | + int hashcode = 0; |
| 75 | + bytes = null; |
| 76 | + out: while (ze != null) { |
| 77 | + while ((ze = zis.getNextEntry()) != null && ze.isDirectory()) { |
| 78 | + System.out.println(ze.getName()); |
| 79 | + } |
| 80 | + ; |
| 81 | + System.out.println(ze.getName()); |
| 82 | + if (ze.getName().equals("xl/calcChain.xml")) { |
| 83 | + bytes = new byte[(int) ze.getSize()]; |
| 84 | + zis.read(bytes); |
| 85 | + break out; |
| 86 | + } |
| 87 | + } |
| 88 | + zis.close(); |
68 | 89 | s = new String(bytes); |
69 | | - System.out.println("hashcode = " + s.hashCode()); |
70 | | - assert(s.hashCode() == hashcode); |
| 90 | + hashcode = s.hashCode(); |
| 91 | + System.out.println("hashcode = " + hashcode); |
| 92 | + if (/** @j2sNative true || */ |
| 93 | + false) { |
| 94 | + bytes = ze.getBytes(); |
| 95 | + s = new String(bytes); |
| 96 | + System.out.println("hashcode = " + s.hashCode()); |
| 97 | + assert (s.hashCode() == hashcode); |
| 98 | + } |
71 | 99 | } |
72 | | - |
73 | 100 | System.out.println("Test_Zipin OK"); |
74 | 101 | } catch (Exception e) { |
75 | 102 | e.printStackTrace(); |
|
0 commit comments