|
|
@@ -0,0 +1,56 @@
|
|
|
+package net.ranides.assira.io;
|
|
|
+
|
|
|
+import java.io.FilterInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.jar.JarInputStream;
|
|
|
+import java.util.zip.ZipInputStream;
|
|
|
+
|
|
|
+public class ZipReader {
|
|
|
+
|
|
|
+ public static JarInputStream openJAR(InputStream istream) throws IOException {
|
|
|
+ return new JarInputStream(new ZipInputStream2(istream));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static ZipInputStream openZIP(InputStream istream) {
|
|
|
+ return new ZipInputStream(new ZipInputStream2(istream));
|
|
|
+ }
|
|
|
+
|
|
|
+ private static class ZipInputStream2 extends FilterInputStream {
|
|
|
+
|
|
|
+ public static final byte[] ZIP_LOCAL = { 0x50, 0x4b, 0x03, 0x04 };
|
|
|
+
|
|
|
+ protected int ip;
|
|
|
+
|
|
|
+ protected int op;
|
|
|
+
|
|
|
+ public ZipInputStream2(InputStream is) {
|
|
|
+ super(is);
|
|
|
+ }
|
|
|
+
|
|
|
+ public int read() throws IOException {
|
|
|
+ while(ip < ZIP_LOCAL.length) {
|
|
|
+ int c = super.read();
|
|
|
+ if (c == ZIP_LOCAL[ip]) {
|
|
|
+ ip++;
|
|
|
+ } else {
|
|
|
+ ip = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return op < ZIP_LOCAL.length ? ZIP_LOCAL[op++] : super.read();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int read(byte[] b, int off, int len) throws IOException {
|
|
|
+ if (op == ZIP_LOCAL.length) {
|
|
|
+ return super.read(b, off, len);
|
|
|
+ }
|
|
|
+ int count = 0;
|
|
|
+ while (count < Math.min(len, ZIP_LOCAL.length)) {
|
|
|
+ b[count++] = (byte)read();
|
|
|
+ }
|
|
|
+ return count;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|