如何使用Java在Zip文件中读写评论?如何从Zip文件读取评论?
我可以这样写评论:
FileOutputStream fos = new FileOutputStream(output);
ZipOutputStream zos = new ZipOutputStream(fos);
zos.setComment("BMC Comment");
如何使用Java在Zip文件中读写评论?如何从Zip文件读取评论?
我可以这样写评论:
FileOutputStream fos = new FileOutputStream(output);
ZipOutputStream zos = new ZipOutputStream(fos);
zos.setComment("BMC Comment");
检查这个帖子:
编辑:不幸的是,原来的链接现已无法使用,这里有一个网页档案连结:
对于后人,这里的要点(略格式):
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;
}
我刚刚发现该网站就在你给出答案之前... :) 这是不是一个直接的方式来做到这一点..但它也做的伎俩^^ 谢谢反正......我选择这个作为一个现在的答案:) – 2012-03-29 04:35:17
链接已死,会很高兴有相关部分的答案[:-( – 2017-07-14 10:26:32
@CarlosHeuberger谢谢,更新! – 2017-07-15 18:51:35
尝试:
java.util.zip.ZipFile.getComment()
这比接受的答案要容易得多 – Aerilys 2014-12-29 07:29:14
嗯,我错过了OP想要阅读单个ZIP *文件*评论,而不是ZIP *条目*评论。 – 2014-12-29 11:25:47
按照[来源](HTTP://www.jarvana。 com/jarvana/view/ant/ant/1.5.1/ant-1.5.1-sources.jar!/org/apache/tools/zip/ZipOutputStream.java?format = ok),文件注释写在压缩文件的_end_。 'ZipInputStream'中似乎没有任何内容可以访问这些数据,但是可能只需从文件末尾(手动)向后搜索就可以得到它。 – aroth 2012-03-29 04:28:03
是啊...我刚刚读了使用你的方法的冰雹链接...我想我没有太多的选择... – 2012-03-29 04:33:37