Some java.util.zip APIs specify the maximum length of string as a parameter. For example, java.util.zip.ZipEntry.ZipEntry(String name)
states:
IllegalArgumentException - if the entry name is longer than 0xFFFF bytes
However, this statement is not true if the string is created
with 0xFFFF + 1 bytes under UTF-16 encoding. See the following program:
import java.util.zip.*;
import java.io.*;
public class ZipEntryTest {
public static void main(String[] args) throws Exception {
try {
ZipEntry entry = new ZipEntry(new String(new byte[0xffff + 1], "UTF-8"));
} catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException is thrown as expected for UTF-8 encoding");
}
try {
ZipEntry entry = new ZipEntry(new String(new byte[0xffff + 1], "UTF-16"));
System.out.println("No exception is thrown for ZipEntry created with UTF-16 encoding");
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
}
The output is:
IllegalArgumentException is thrown as expected for UTF-8 encoding
No exception is thrown for ZipEntry created with UTF-16 encoding
So it seems that the maximum length of string should be defined
as number of characters, instead of number of bytes.
The following methods are affected by this bug:
ZipEntry.ZipEntry(String name)
ZipEntry.setComment(String comment)
ZipOutputStream.setComment(String comment)
###@###.### 2005-03-23 22:08:45 GMT
###@###.### 2005-03-23 22:39:14 GMT