How to unzip file using Java and shell command

 



I had a simple task to complete, unzip a file using java, because I wanted to it multiple times by reading a config file, the code was supposed to be very easy.


Something like this:



But it turns out that calling unzip hangs forever, so I found an stackoverflow post (https://stackoverflow.com/questions/34088099/executing-unzip-command-programmatically) that had the solution, and I ended doing it like this:



As you can see this is a little bit different, it calls unzipFile.sh that basically does execute unzip command with the parameters passed by. 

I was not alone with this problem, there are plenty of posts and questions about this on stackoverflow, but for some reason these did not work for me:

https://stackoverflow.com/questions/40484180/unzip-command-is-not-working-from-java-code?noredirect=1&lq=1

https://stackoverflow.com/questions/50288224/tar-command-stuck-while-generating-tar-file-from-java-process

https://mkyong.com/java/how-to-execute-shell-command-from-java/

https://www.baeldung.com/run-shell-command-in-java

Comments