[英]How to parse file inside a zip without writing to disk - java
I have a password protected zip file [in the form of a base64 encoded data and the name of the zip file] which contains a single xml. 我希望在不向磁盘写入任何内容的情况下解析 xml。 在 Zip4j 中执行此操作的方法是什么? 以下是我尝试过的。
String docTitle = request.getDocTitle();
byte[] decodedFileData = Base64.getDecoder().decode(request.getBase64Data());
InputStream inputStream = new ByteArrayInputStream(decodedFileData);
try (ZipInputStream zipInputStream = new ZipInputStream(inputStream, password)) {
while ((localFileHeader = zipInputStream.getNextEntry()) != null) {
String fileTitle = localFileHeader.getFileName();
File extractedFile = new File(fileTitle);
try (InputStream individualFileInputStream = org.apache.commons.io.FileUtils.openInputStream(extractedFile)) {
// Call parser
parser.parse(localFileHeader.getFileName(),
individualFileInputStream));
} catch (IOException e) {
// Handle IOException
}
}
} catch (IOException e) {
// Handle IOException
}
这让我感到java.io.FileNotFoundException: File 'xyz.xml' does not exist
at line FileUtils.openInputStream FileUtils.openInputStream(extractedFile)
。 你能建议我这样做的正确方法吗?
ZipInputStream
保留 zip 文件的所有内容。 每次调用zipInputStream.getNextEntry()
传递每个文件的内容并将“指针”移动到下一个条目(文件)。 您还可以在移至下一个条目之前读取文件 ( ZipInputStream.read )。
你的情况:
byte[] decodedFileData = Base64.getDecoder().decode(request.getBase64Data());
InputStream inputStream = new ByteArrayInputStream(decodedFileData);
try (ZipInputStream zipInputStream = new ZipInputStream(inputStream, password)) {
ZipEntry zipEntry = null;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
byte[] fileContent = IOUtils.toByteArray(zipInputStream);
parser.parse(zipEntry.getName(),
new ByteArrayInputStream(fileContent)));
}
} catch (Exception e) {
// Handle Exception
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.