Skip to content

Commit 970e778

Browse files
committed
ZipInputStream fix to allow odd zero-length file format; file opener
issue
1 parent 84ea1a2 commit 970e778

File tree

11 files changed

+89
-32
lines changed

11 files changed

+89
-32
lines changed
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20241128160622
1+
20241217101652
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20241128160622
1+
20241217101652
Binary file not shown.
Binary file not shown.

sources/net.sf.j2s.java.core/src/java/util/zip/ZipInputStream.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -346,16 +346,29 @@ private ZipEntry readLOC() throws IOException {
346346
}
347347
e.method = get16(tmpbuf, LOCHOW);
348348
e.time = get32(tmpbuf, LOCTIM);
349-
if ((flag & 8) == 8) {
350-
/* "Data Descriptor" present */
351-
if (e.method != DEFLATED) {
352-
throw new ZipException("only DEFLATED entries can have EXT descriptor");
353-
}
354-
} else {
355-
e.crc = get32(tmpbuf, LOCCRC);
356-
e.csize = get32(tmpbuf, LOCSIZ);
357-
e.size = get32(tmpbuf, LOCLEN);
358-
}
349+
boolean readSizes = ((flag & 8) != 8 || e.getMethod() != DEFLATED);
350+
// flag 8 means size is unknown at compression time
351+
// leave crc and sizes -1
352+
/* "Data Descriptor" present */
353+
// still read the 0 if (e.getMethod() == DEFLATED)
354+
// DO NOT throw new ZipException
355+
//System.out.println("ZipInputStream: only DEFLATED entries can have EXT descriptor?? - " + e.getName());
356+
if (readSizes) {
357+
e.setCrc(get32(tmpbuf, LOCCRC));
358+
e.setCompressedSize(get32(tmpbuf, LOCSIZ));
359+
e.setSize(get32(tmpbuf, LOCLEN));
360+
}
361+
// if ((flag & 8) == 8) {
362+
// oops - don't set 0!
363+
// /* "Data Descriptor" present */
364+
// if (e.method != DEFLATED) {
365+
// throw new ZipException("only DEFLATED entries can have EXT descriptor");
366+
// }
367+
// } else {
368+
// e.crc = get32(tmpbuf, LOCCRC);
369+
// e.csize = get32(tmpbuf, LOCSIZ);
370+
// e.size = get32(tmpbuf, LOCLEN);
371+
// }
359372
len = get16(tmpbuf, LOCEXT);
360373
byte[] bb = new byte[len + 4];
361374
if (len > 0) {

sources/net.sf.j2s.java.core/src/test/Test_Zipin.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ public static void main(String[] args) {
5353

5454

5555
if (/** @j2sNative true || */
56-
false) {
56+
true
57+
|| false) {
5758

5859

5960

@@ -75,12 +76,19 @@ public static void main(String[] args) {
7576
System.out.println(ze.getName());
7677
}
7778
;
79+
if (ze == null)
80+
break;
7881
System.out.println(ze.getName());
79-
if (ze.getName().equals("xl/calcChain.xml")) {
80-
bytes = new byte[(int) ze.getSize()];
81-
zis.read(bytes);
82-
break out;
83-
}
82+
83+
bytes = new byte[(int) ze.getSize()];
84+
System.out.println(zis.read(bytes));
85+
86+
87+
// if (ze.getName().equals("xl/calcChain.xml")) {
88+
// bytes = new byte[(int) ze.getSize()];
89+
// zis.read(bytes);
90+
// break out;
91+
// }
8492
}
8593
zis.close();
8694
s = new String(bytes);

sources/net.sf.j2s.java.core/src_4.2/java/util/zip/ZipInputStream.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -341,16 +341,28 @@ private ZipEntry readLOC() throws IOException {
341341
}
342342
e.method = get16(tmpbuf, LOCHOW);
343343
e.time = get32(tmpbuf, LOCTIM);
344-
if ((flag & 8) == 8) {
345-
/* "Data Descriptor" present */
346-
if (e.method != DEFLATED) {
347-
throw new ZipException("only DEFLATED entries can have EXT descriptor");
348-
}
349-
} else {
350-
e.crc = get32(tmpbuf, LOCCRC);
351-
e.csize = get32(tmpbuf, LOCSIZ);
352-
e.size = get32(tmpbuf, LOCLEN);
353-
}
344+
boolean readSizes = ((flag & 8) != 8 || e.getMethod() != DEFLATED);
345+
// flag 8 means size is unknown at compression time
346+
// leave crc and sizes -1
347+
/* "Data Descriptor" present */
348+
// still read the 0 if (e.getMethod() == DEFLATED)
349+
// DO NOT throw new ZipException
350+
//System.out.println("ZipInputStream: only DEFLATED entries can have EXT descriptor?? - " + e.getName());
351+
if (readSizes) {
352+
e.setCrc(get32(tmpbuf, LOCCRC));
353+
e.setCompressedSize(get32(tmpbuf, LOCSIZ));
354+
e.setSize(get32(tmpbuf, LOCLEN));
355+
}
356+
// if ((flag & 8) == 8) {
357+
// /* "Data Descriptor" present */
358+
// if (e.method != DEFLATED) {
359+
// throw new ZipException("only DEFLATED entries can have EXT descriptor");
360+
// }
361+
// } else {
362+
// e.crc = get32(tmpbuf, LOCCRC);
363+
// e.csize = get32(tmpbuf, LOCSIZ);
364+
// e.size = get32(tmpbuf, LOCLEN);
365+
// }
354366
len = get16(tmpbuf, LOCEXT);
355367
if (len > 0) {
356368
byte[] bb = new byte[len];

sources/net.sf.j2s.java.core/srcjs/js/j2sApplet.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,22 +1212,34 @@ if (database == "_" && J2S._serverUrl.indexOf("//your.server.here/") >= 0) {
12121212
// x.click() in any manifestation will not work from Chrome or Safari.
12131213
// These browers require that the user see and click the link.
12141214
if (J2S._canClickFileReader) {
1215+
var px = screen.width / 2 - 80;
1216+
var py = screen.height / 2 - 40;
12151217
var x = document.createElement("input");
1218+
x.id = "swingjs2input";
12161219
x.value = x.text = "xxxx";
12171220
x.type = "file";
1221+
x.style.z_index = 1000000;
1222+
x.style.position = "fixed";
1223+
x.style.left = px + 'px';
1224+
x.style.top = py + 'px';
1225+
12181226
if (isMultiple)
12191227
x.setAttribute("multiple", "true");
12201228
x.addEventListener("change", function(ev) {
12211229
J2S._fileReaderCancelListener = null;
1230+
var f = this.files;
1231+
$("#swingjs2input").remove();
12221232
window.removeEventListener("focus", J2S._fileReaderCancelListener);
1223-
(isMultiple ? readFiles(this.files) : readFile(this.files[0]));
1224-
}, false);
1233+
window.setTimeout(function(){(isMultiple ? readFiles(f) : readFile(f[0]))},1);
1234+
}, false);
1235+
document.body.append(x);
12251236
x.click();
12261237
window.addEventListener("focus", J2S._fileReaderCancelListener = function(a){
12271238
setTimeout(function() {
12281239
window.removeEventListener("focus", J2S._fileReaderCancelListener);
12291240
if (J2S._fileReaderCancelListener != null) {
12301241
J2S._fileReaderCancelListener = null;
1242+
$("#swingjs2input").remove();
12311243
fDone(null, null);
12321244
}
12331245
},500)

0 commit comments

Comments
 (0)