Unzip huge gz file in Java and performance

I will unzip the huge gz file in java, the gz file is about 2 gb and the unzipped file is about 6 gb. from time to time the unpacking process lasts forever (hours), sometimes it ends in a reasonable time (for example, less than 10 minutes or faster).
I have a pretty powerful box (8 GB of RAM, 4-processor), is there any way to improve the code below? or use a completely different library?
Also I used Xms256m and Xmx4g for vm.

public static File unzipGZ(File file, File outputDir) {
    GZIPInputStream in = null;
    OutputStream out = null;
    File target = null;
    try {
        // Open the compressed file
        in = new GZIPInputStream(new FileInputStream(file));

        // Open the output file
        target = new File(outputDir, FileUtil.stripFileExt(file.getName()));
        out = new FileOutputStream(target);

        // Transfer bytes from the compressed file to the output file
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }

        // Close the file and stream
        in.close();
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    return target;
}
+3
source share
3 answers

, , , BufferedInputStream/BufferedOutputStream. - 1K - . , . 16K, 64K .. , , BufferedInputStream.

, , . 10 , , , - . , ? ? ? ?

: in out , try.

+2

8 , 2 , . , .

0

Try using channels from java.nio, you have a way to transfer bytes from other channels. Then you do not need to copy them yourself. And this is likely to be pretty optimized. See FileInputStream.getChannel ()

0
source

Source: https://habr.com/ru/post/1791958/


All Articles