Watermelon
Watermelon

Reputation: 84

Not in GZIP format java while reading the zip dynamically

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?

Upvotes: 0

Views: 786

Answers (2)

Michael Gantman
Michael Gantman

Reputation: 7808

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 GZIPInputStream class. 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

Upvotes: 0

Shark
Shark

Reputation: 6414

Ok, so it's not GZip but it could be ZIP right?

        String fileZip = "src/main/resources/unzipTest/compressed.zip";
        File destDir = new File("src/main/resources/unzipTest");

        byte[] buffer = new byte[1024];
        ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip));
        ZipEntry zipEntry = zis.getNextEntry();
        
        while (zipEntry != null) {
            File newFile = newFile(destDir, zipEntry);
            if (zipEntry.isDirectory()) {
                if (!newFile.isDirectory() && !newFile.mkdirs()) {
                    throw new IOException("Failed to create directory " + newFile);
                }
            } else {
                // fix for Windows-created archives
                File parent = newFile.getParentFile();
                if (!parent.isDirectory() && !parent.mkdirs()) {
                    throw new IOException("Failed to create directory " + parent);
                }

                // write file content
                FileOutputStream fos = new FileOutputStream(newFile);
                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
                fos.close();
            }
            zipEntry = zis.getNextEntry();
        }

        zis.closeEntry();
        zis.close();

as well as this:

public static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException {
    File destFile = new File(destinationDir, zipEntry.getName());

    String destDirPath = destinationDir.getCanonicalPath();
    String destFilePath = destFile.getCanonicalPath();

    if (!destFilePath.startsWith(destDirPath + File.separator)) {
        throw new IOException("Entry is outside of the target dir: " + zipEntry.getName());
    }

    return destFile;
}

give that a shot.

Upvotes: 0

Related Questions