[英]How can I read comments from a Zip file?
How can I read and write comments in a Zip file, using Java? 如何使用Java在Zip文件中读写注释?
I can write comments with this: 我可以这样写评论:
FileOutputStream fos = new FileOutputStream(output);
ZipOutputStream zos = new ZipOutputStream(fos);
zos.setComment("BMC Comment");
尝试:
java.util.zip.ZipFile.getComment()
Check this post: 检查这篇文章:
Edit : unfortunately the original link is now unavailable, here's a Web archive link: 编辑 :不幸的是,现在原始链接不可用,这是一个Web存档链接:
http://web.archive.org/web/20100117212418/http://www.flattermann.net:80/2009/01/read-a-zip-file-comment-with-java/ http://web.archive.org/web/20100117212418/http://www.flattermann.net:80/2009/01/read-a-zip-file-comment-with-java/
For posterity, here's the gist (slightly formatted): 为了后代,这里是要点(略有格式):
private static String getZipCommentFromBuffer (byte[] buffer, int len) {
byte[] magicDirEnd = {0x50, 0x4b, 0x05, 0x06};
int buffLen = Math.min(buffer.length, len);
// Check the buffer from the end
for (int i = buffLen - magicDirEnd.length - 22; i >= 0; i--) {
boolean isMagicStart = true;
for (int k = 0; k < magicDirEnd.length; k++) {
if (buffer[i + k] != magicDirEnd[k]) {
isMagicStart = false;
break;
}
}
if (isMagicStart) {
// Magic Start found!
int commentLen = buffer[i + 20] + buffer[i + 21] * 256;
int realLen = buffLen - i - 22;
System.out.println ("ZIP comment found at buffer position "
+ (i + 22) + " with len = " + commentLen + ", good!");
if (commentLen != realLen) {
System.out.println ("WARNING! ZIP comment size mismatch: "
+ "directory says len is " + commentLen
+ ", but file ends after " + realLen + " bytes!");
}
String comment = new String (buffer, i + 22, Math.min(commentLen, realLen));
return comment;
}
}
System.out.println ("ERROR! ZIP comment NOT found!");
return null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.