ZipEntry类中的getExtraMethod()返回null,其中作为getName()方法正确返回文件名


ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(byte[])); //input is byte []
ZipEntry ze = zis.getNextEntry();
while (ze != null) {
String fileName = ze.getName();
String extension = fileName.substring(fileName.indexOf("."), fileName.length());
if (".xml".equalsIgnoreCase(extension)) {
Document oldSdoDocument = builder.parse(new ByteArrayInputStream(ze.getExtra()));
//compare xml content
}
}

当我尝试使用ZipInputStream和ZipEntry.getExtra((从byte[]获取文件内容时,我没有获得文件内容。相反,我在临时位置重新创建了zip文件,然后使用ZipFile读取zip文件,对于文件内容,我使用ZipFile.getInputStream(zipEntry(

getExtra((方法不检索zip文件中的文件内容,而是返回zip条目中extra字段的值。额外的字段是:

.ZIP文件格式在文件头中包含一个额外的字段功能,可用于存储现有ZIP规范未定义的额外数据,并允许不识别字段的合规存档人员安全地跳过这些字段。标头ID 0–31保留供PKWARE使用。剩下的ID可以由第三方供应商用于专有用途。

看起来您只想读取ZIP中的XML文件并对其进行解析,因此不使用额外的字段,而是从ZIP输入流本身读取数据:

ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(byte[])); //input is byte []
ZipEntry ze = zis.getNextEntry();
while (ze != null) {
String fileName = ze.getName();
String extension = fileName.substring(fileName.indexOf("."), fileName.length());
if (".xml".equalsIgnoreCase(extension)) {
Document oldSdoDocument = builder.parse(zis);
//compare xml content
}
ze = zis.getNextEntry();
}

最新更新