我想解析包含在一個zip文件中的多個gzip壓縮文件,通過InputStream從一個壓縮文件http連接。解析壓縮輸入流中包含的多個壓縮json文件,而不將任何文件保存到磁盤(因爲谷歌應用程序引擎)
我已經設法讀取第一個文件,但沒有更多。有時會失敗,不會讀取整個(第一個)文件。我已經檢查了連接上的內容長度標題,即使在我無法讀取整個文件時也是如此。
我使用的goole應用程序引擎,它不允許我在本地保存文件,而我發現的大多數示例都在這樣做。
我對Zip文件使用https://commons.apache.org/proper/commons-compress/的ZipArchiveInputStream。
這是最密切相關的問題,我已經能夠找到:How to read from file containing multiple GzipStreams
private static ArrayList<RawEvent> parseAmplitudeEventArchiveData(HttpURLConnection connection)
throws IOException, ParseException {
String name, line;
ArrayList<RawEvent> events = new ArrayList<>();
try (ZipArchiveInputStream zipInput =
new ZipArchiveInputStream(connection.getInputStream(), null, false, true);) {
ZipArchiveEntry zipEntry = zipInput.getNextZipEntry();
if (zipEntry != null) {
try(GZIPInputStream gzipInputStream = new GZIPInputStream(connection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(gzipInputStream))) {
name = zipEntry.getName();
log.info("Parsing file: " + name);
while ((line = reader.readLine()) != null) {
events.add(parseJsonLine(line));
}
log.info("Events size: " + events.size());
}
}
}
return events;
}
我不知道這是如何工作,因爲你使用的輸入流來自GZIPInputStream的連接。但是你真正想要的是讀取ZipArchiveInputStream的數據並從這些數據創建一個GZIPInputStream。 –
@MartinKrüger是的我一直在想,如果我把它切換出來,你建議我得到一個「IOException:截斷的ZIP文件」 – Jitan