Skip to content

Commit d89bd4c

Browse files
author
Eirik Bjørsnøs
committed
8380542: ZipOutputStream.setComment and ZipEntry.setComment spec updates for rejecting unmappable characters and allowing null or empty comments
Reviewed-by: alanb, lancea, jpai
1 parent 24f7945 commit d89bd4c

3 files changed

Lines changed: 78 additions & 13 deletions

File tree

src/java.base/share/classes/java/util/zip/ZipEntry.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1995, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1995, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -651,8 +651,9 @@ public byte[] getExtra() {
651651
}
652652

653653
/**
654-
* Sets the optional comment string for the entry.
655-
* @param comment the comment string
654+
* Sets the optional comment string for the entry. If {@code comment} is an
655+
* empty string or {@code null} then the entry will have no comment.
656+
* @param comment the comment string, or an empty string or null for no comment
656657
* @throws IllegalArgumentException if the combined length
657658
* of the specified entry comment, the {@linkplain #getName() entry name},
658659
* the {@linkplain #getExtra() extra field data}, and the

src/java.base/share/classes/java/util/zip/ZipOutputStream.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
* <p> Unless otherwise noted, passing a {@code null} argument to a constructor
4444
* or method in this class will cause a {@link NullPointerException} to be
4545
* thrown.
46+
* <p> By default, the UTF-8 charset is used to encode entry names and comments.
47+
* {@link #ZipOutputStream(OutputStream, Charset)} may be be used to specify
48+
* an alternative charset.
49+
*
4650
* @author David Connelly
4751
* @since 1.1
4852
*/
@@ -110,10 +114,8 @@ private void ensureOpen() throws IOException {
110114
public static final int DEFLATED = ZipEntry.DEFLATED;
111115

112116
/**
113-
* Creates a new ZIP output stream.
114-
*
115-
* <p>The UTF-8 {@link java.nio.charset.Charset charset} is used
116-
* to encode the entry names and comments.
117+
* Creates a new ZIP output stream using the UTF-8
118+
* {@link Charset charset} to encode entry names and comments.
117119
*
118120
* @param out the actual output stream
119121
*/
@@ -122,12 +124,13 @@ public ZipOutputStream(OutputStream out) {
122124
}
123125

124126
/**
125-
* Creates a new ZIP output stream.
127+
* Creates a new ZIP output stream using the specified
128+
* {@link Charset charset} to encode entry names and comments.
126129
*
127130
* @param out the actual output stream
128131
*
129132
* @param charset the {@linkplain java.nio.charset.Charset charset}
130-
* to be used to encode the entry names and comments
133+
* to be used to encode entry names and comments
131134
*
132135
* @since 1.7
133136
*/
@@ -140,10 +143,15 @@ public ZipOutputStream(OutputStream out, Charset charset) {
140143
}
141144

142145
/**
143-
* Sets the ZIP file comment.
144-
* @param comment the comment string
145-
* @throws IllegalArgumentException if the length of the specified
146-
* ZIP file comment is greater than 0xFFFF bytes
146+
* Sets the ZIP file comment. If {@code comment} is an empty string or
147+
* {@code null} then the output will have no ZIP file comment.
148+
*
149+
* @param comment the comment string, or an empty string or null for no comment
150+
*
151+
* @throws IllegalArgumentException if the length of the specified ZIP file
152+
* comment is greater than 0xFFFF bytes or if the {@code comment}
153+
* contains characters that cannot be mapped by the {@code Charset}
154+
* used to encode entry names and comments
147155
*/
148156
public void setComment(String comment) {
149157
byte[] bytes = null;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import org.junit.jupiter.api.Test;
25+
26+
import java.io.IOException;
27+
import java.nio.charset.Charset;
28+
import java.nio.charset.StandardCharsets;
29+
import java.util.zip.ZipOutputStream;
30+
31+
import static java.io.OutputStream.nullOutputStream;
32+
import static org.junit.jupiter.api.Assertions.assertThrows;
33+
34+
/* @test
35+
* @bug 8380542
36+
* @summary ZipOutputStream.setComment should throw IllegalArgumentException for unmappable characters in comment
37+
* @run junit ${test.main.class}
38+
*/
39+
public class UnmappableZipFileComment {
40+
/**
41+
* Verify that calling ZipOutputStream.setComment with an unmappable
42+
* comment is rejected with a IllegalArgumentException.
43+
*
44+
* @throws IOException if an unexpected IO error occurs
45+
*/
46+
@Test
47+
void rejectUnmappableZipFileComment() throws IOException {
48+
// Charset used when creating the ZIP file
49+
Charset charset = StandardCharsets.US_ASCII;
50+
// 'ø' is an unmappable character in US_ASCII
51+
String comment = "\u00f8";
52+
try (var out = new ZipOutputStream(nullOutputStream(), charset)) {
53+
assertThrows(IllegalArgumentException.class, () -> out.setComment(comment));
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)