Search code examples
javawindowsgzipdirectoryunzip

Unzip multiple gzip files across different folders recursively in Java


I have the following file structure:

D:\Files\File1\file.gz, file2.gz, file3.gz
D:\Files\File2\file.gz, file2.gz, file3.gz
etc..

I would like to provide the Java program with just D:\Files and it should recursively open each directory, and unzip each file.gz and file2.gz (they all have the same name, so I can just ask it to look for all files matching that name) into the same folder.

I have tried different codes to get each file to extract but nothing seems to work. Another funny thing that happens to me is that I can't seem to unlock read-only permissions on the directory. I uncheck it, go to the Security tab and give myself admin privileges, and yet, the next second, its back to read-only. It's Off topic, but I had to put it out there. Please help me someone! Thanks!


Solution

  • Use org.apache.commons.io.FileUtils.iterateFiles using a file filter to specify you only want zip files.

    For the decompression use GZIPInputStream.