JDK-6245146 : Classes in java.util.zip have incorrect statement about maximum string length in some methods
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util.jar
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Cannot Reproduce
  • OS: solaris_9
  • CPU: sparc
  • Submitted: 2005-03-23
  • Updated: 2011-03-16
  • Resolved: 2009-06-03
Related Reports
Relates :  
Some java.util.zip APIs specify the maximum length of string as a parameter. For example, java.util.zip.ZipEntry.ZipEntry(String name) 

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) {

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

EVALUATION With the non-UTF-8 entry name/comment support in JDK7 (4244499) the API has been updated in 7 as "IP entry comments have maximum length of 0xffff. If the length of the specified comment string is greater than 0xFFFF bytes after encoding, only the first 0xFFFF bytes are output to the ZIP file entry. It has clearly specified the length is "the length of the bytes after encoding". So, closed as "not reproducible" in JDK7

EVALUATION The limit in the jar files is a byte length limit, not a char length limit. The docs could be clearer that this is the limit when the name is UTF-8 encoded, since a byte length is meaningless without a specific encoding. ###@###.### 2005-03-23 22:41:49 GMT