java - Parsing multiple gzipped json files that are contained in a zip inputstream without saving any files to disk (because of google app engine) -
i'm trying parse multiple gzipped json files contained in 1 zip file through inputstream http connection.
i've managed read first file not more. fails , not read whole (first) file. have checked content-length header on connection , same when i'm failing read whole file.
i'm using goole app engine doesn't allow me save files locally of examples i've found doing.
i'm using ziparchiveinputstream https://commons.apache.org/proper/commons-compress/ zip file.
this closely related question i've been able find: how read 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; }
this works me:
public class unzipzippedfiles { public static void main(string[] args) throws ioexception, parseexception { fileinputstream inputstream = new fileinputstream("/home/me/dev/scratchpad/src/main/resources/files.zip"); unzipfile(inputstream); } private static void unzipfile(inputstream inputstream) throws ioexception, parseexception { try (ziparchiveinputstream zipinput = new ziparchiveinputstream(inputstream, null, false, true);) { ziparchiveentry zipentry; while ((zipentry = zipinput.getnextzipentry()) != null) { system.out.println("file: " + zipentry.getname()); byte[] filebytes = readdatafromzipstream(zipinput, zipentry); bytearrayinputstream bytein = new bytearrayinputstream(filebytes); unzipgziparchiveandprint(bytein); } } } private static byte[] readdatafromzipstream(ziparchiveinputstream zipstream, ziparchiveentry entry) throws ioexception { byte[] data = new byte[(int) entry.getsize()]; zipstream.read(data); return data; } private static void unzipgziparchiveandprint(inputstream inputstream) throws ioexception { system.out.println("content:"); try (gzipinputstream gzipinputstream = new gzipinputstream(inputstream); bufferedreader reader = new bufferedreader(new inputstreamreader(gzipinputstream))) { string line; while ((line = reader.readline()) != null) { system.out.println(line); } } } }
Comments
Post a Comment