Do I need to close the GZIPInputStream when unpacking the gzip in a zip file?

61 Views Asked by At

sample code:

import java.io.*;
import java.util.zip.*;

public class Main {
    public static void main(String[] args) throws IOException {
        try(FileInputStream fileInputStream = new FileInputStream("xxx.zip");
            ZipInputStream zipIn = new ZipInputStream(fileInputStream)){
            ZipEntry entry;
            while ((entry = zipIn.getNextEntry()) != null) {
                try {
                    if (!entry.isDirectory() && entry.getName().endsWith(".gz")) {
                     
                        GZIPInputStream gzipIn = new GZIPInputStream(zipIn);

                        // do something
                        // .......
                        // do something

                        /*
                         * Do I need to close the GZIPInputStream?
                         * Closing the GZIPInputStream will cause the entire ZipInputStream to be closed.
                         * If it is not closed, will it lead to memory leaks?
                         */
                        gzipIn.close();

                    }
                } finally {
                    zipIn.closeEntry();
                }
            }
        }
    }
}

How to do?

0

There are 0 best solutions below