Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/java.base/share/classes/java/util/zip/ZipFile.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -308,7 +308,9 @@ public ZipFile(File file, Charset charset) throws IOException
}

/**
* Returns the zip file comment, or null if none.
* Returns the zip file comment. If a comment does not exist or an error is
* encountered decoding the comment using the charset specified
* when opening the Zip file, then {@code null} is returned.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've previously discussed options with Lance around this issue and I agree with the proposal to specify that it returns null when the decoding fails.

(In passing, the casing of "zip file" is very inconsistent in the javadoc, it's "ZIP file" in some places, "zip file" in others, the change here uses "Zip file").

Copy link
Contributor Author

@LanceAndersen LanceAndersen Feb 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(In passing, the casing of "zip file" is very inconsistent in the javadoc, it's "ZIP file" in some places, "zip file" in others, the change here uses "Zip file").

Thank you for pointing out the discrepancy above.

Let's decide on which way to go and I will update under a separate PR.

I would suggest "ZIP file" based on the PKWare APPNOTE.TXT, but the man pages for info-zip command zip command uses "zip file"

Apache commons also uses "ZIP file" or "ZIP archive"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest "ZIP file" based on the PKWare APPNOTE.TXT,

I think using this case would be good.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest "ZIP file" based on the PKWare APPNOTE.TXT, but the man pages for info-zip command zip command uses "zip file"

"ZIP" is not an acronym so I assume this is why they use "zip" in their docs.

*
* @return the comment string for the zip file, or null if none
*
Expand All @@ -322,7 +324,13 @@ public String getComment() {
if (res.zsrc.comment == null) {
return null;
}
return res.zsrc.zc.toString(res.zsrc.comment);
// If there is a problem decoding the byte array which represents
// the Zip file comment, return null;
try {
return res.zsrc.zc.toString(res.zsrc.comment);
} catch (IllegalArgumentException iae) {
return null;
}
}
}

Expand Down
13 changes: 10 additions & 3 deletions src/java.base/share/classes/java/util/zip/ZipInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -515,9 +515,16 @@ private ZipEntry readLOC() throws IOException {
}
readFully(b, 0, len);
// Force to use UTF-8 if the USE_UTF8 bit is ON
ZipEntry e = createZipEntry(((flag & USE_UTF8) != 0)
? ZipCoder.toStringUTF8(b, len)
: zc.toString(b, len));
String entryName;
try {
entryName = ((flag & USE_UTF8) != 0) ?
ZipCoder.toStringUTF8(b, len)
: zc.toString(b, len);
} catch (Exception ex) {
throw (ZipException) new ZipException(
"invalid LOC header (bad entry name)").initCause(ex);
}
ZipEntry e = createZipEntry(entryName);
// now get the remaining fields for the entry
if ((flag & 1) == 1) {
throw new ZipException("encrypted ZIP entry not supported");
Expand Down
Loading