Tencent/tinker · error · IllegalArgumentException
Comment too long: {} bytes
Error message
Comment too long: {} bytes What it means
The zip end-of-central-directory record stores the archive comment length in 16 bits, so a comment whose UTF-8 encoding is 65,536 bytes or more cannot be written. setComment encodes immediately and throws IllegalArgumentException when newCommentBytes.length > 0xffff, failing fast rather than producing a truncated comment field.
Source
Thrown at third-party/tinker-ziputils/src/main/java/com/tencent/tinker/ziputils/ziputil/AlignedZipOutputStream.java:397
if (currentEntry.getExtra() != null) {
out.write(currentEntry.getExtra());
}
makePaddingToStream(out, padding);
}
/**
* Sets the comment associated with the file being written.
* @throws IllegalArgumentException if the comment is >= 64 Ki UTF-8 bytes.
*/
public void setComment(String comment) {
if (comment == null) {
this.commentBytes = null;
return;
}
byte[] newCommentBytes = comment.getBytes(Charset.forName("UTF-8"));
if (newCommentBytes.length > 0xffff) {
throw new IllegalArgumentException("Comment too long: " + newCommentBytes.length + " bytes");
}
this.commentBytes = newCommentBytes;
}
/**
* Sets the <a href="Deflater.html#compression_level">compression level</a> to be used
* for writing entry data.
*/
public void setLevel(int level) {
if (level < Deflater.DEFAULT_COMPRESSION || level > Deflater.BEST_COMPRESSION) {
throw new IllegalArgumentException("Bad level: " + level);
}
compressionLevel = level;
}
/**
* Sets the default compression method to be used when a {@code ZipEntry} doesn't
* explicitly specify a method. See {@link ZipEntry#setMethod} for more details.
View on GitHub (pinned to 1b7ea02c23)
Solutions
- Keep the comment under 65,535 UTF-8 bytes; enforce the limit where the comment string is assembled.
- Move large metadata out of the comment into a dedicated zip entry (e.g. META-INF/metadata.json) with no size restrictions.
- If the comment carries a signature/key block, shorten by hashing (store a digest plus a URL instead of full content).
Example fix
// before: stuffing arbitrary-length metadata into the comment
zos.setComment(buildMetadataJson()); // grows past 64Ki over time
// after: cap the comment and move bulk metadata to an entry
byte[] meta = buildMetadataJson();
if (meta.length > 0xffff) {
zos.putNextEntry(new ZipEntry("META-INF/metadata.json"));
zos.write(meta);
zos.closeEntry();
zos.setComment("see META-INF/metadata.json");
} else {
zos.setComment(new String(meta, StandardCharsets.UTF_8));
} Defensive patterns
Strategy: validation
Validate before calling
// Validate comment size before calling setComment
static String fitComment(String comment) {
if (comment == null) return null;
byte[] b = comment.getBytes(java.nio.charset.StandardCharsets.UTF_8);
if (b.length > 0xffff) throw new IllegalArgumentException("comment too long: " + b.length + " bytes");
return comment;
} Try / catch
try {
zos.setComment(comment);
} catch (IllegalArgumentException e) {
if (e.getMessage() != null && e.getMessage().startsWith("Comment too long")) {
zos.setComment(comment.substring(0, comment.length() / 2)); // or move to an entry
} else {
throw e;
}
} Prevention
- Keep zip comments to short identifiers; store bulk metadata as an entry.
- Size-check any generated comment against 65,535 UTF-8 bytes.
- Watch multi-byte content when computing the budget.
When it happens
Trigger: Calling setComment with a very long string — e.g. embedding a JSON metadata blob, build manifest, or log excerpt as the zip comment.
Common situations: Build pipelines that stuff patch metadata (file lists, hashes, signatures) into the APK/zip comment and grow past 64Ki over time; multi-byte content (CJK) halving the effective character budget; copying a comment from another tool that allowed larger comments via Zip64 extensible records.
Related errors
- Name too long: {} UTF-8 bytes
- Expected ${DEX_IN_JAR_NAME} in ${file}
- CRC mismatch
- Size mismatch
- Entry already exists: {}
AI-assisted analysis of Tencent/tinker@1b7ea02c23 (2026-08-14).
Data as JSON: /api/errors/c0c704cc93aa7165.
Report an issue: GitHub.