my requirement is to unzip a log file dynamically and write it in the console. But I am getting an exception
java.util.zip.ZipException: Not in GZIP format
at java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:166) ~[?:?]
at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:80) ~[?:?]
at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:92) ~[?:?]
here is my code
private static void readLogFile(File f) {
try(BufferedReader reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(f)), "UTF-8"))) {
String nextLine;
while ((nextLine = reader.readLine()) != null) {
System.out.println(nextLine);
}
} catch (FileNotFoundException e) {
logger.error("file doesn't exist." );
} catch (IOException e) {
logger.error("Error reading the file", e);
}
}
The size of my file is around 50 MB The size of my .gz file is 2.4 MB
I have already tried unzipping the file in the terminal and it works so the file is not corrupt as well
can someone help me with what is going wrong here?
Your code looks correct I think that your file may be compressed in format that is not GZIP and that format is supported gzip command line utility but not by
GZIPInputStreamclass. If this is the case one of the workarounds would be is to invoke command line utility from Java using ProcessBuilder class. This is definitely not ideal as your immediately becomes operating system dependent, but it will work. Other options would be to request that your log files provided to you be compressed in GZIP (but that depends on who is your log files supplier and if they can and willing to co-operate with you) or look for 3d party java library that supports the format in which your files are compressed