Batch Extract Zip Files Using Java & Zip4j
Introduction to the problem
I am an avid gamer, and I enjoy collecting games; however, I am also currently a “poor college student”, and “triple A” titles usually don’t fit in my budget. This is where indie games come in. Indie games, as the name suggests, are developed independently of any large development corporation, usually by a individual person or a small team. These games are typically priced much lower than your average AAA title (less developers, and no publisher to take a large cut)(Brown, Peter 2014), and are therefore more affordable for me. I find that I like to try different things a lot, and games are no exception. This leads me to Humble Bundle.
I love Humble Bundle because it combines a package of great games, at a great price that I choose. Also, I find that indie games tend to be a “labor of love” by their developers. As far as Humble Bundle is concerned, that usually means that the soundtracks are included with the copies of the games. Have you ever had a game that stuck with you because of the haunting or beautiful music? I am also an avid music collector, so getting these awesome soundtracks with the games is a dream-come-true.
Getting the games is usually easy enough from Humble Bundle. They offer drm-free copies that you can download, but the majority of the times I was after the steam key to add it to my steam library. The soundtracks for the games were available in mp3 format, or even flac for the audiophile. I usually quickly redeem the steam key, but I had let the soundtracks stack up in my Humble Bundle download queue, telling myself that I would download them some other time. I finally had the audacity to download all 212 of them at once and stored them in a directory on my hard drive. Okay, but now what? I had a serious problem. I was not going to manually go through unzipping and waiting on 200+ zip files that were 100+ MegaBytes (there were also a few audio books that were over 2 GigaBytes). I needed some way to extract each of these zip files and have them copy to my iTunes library. (Now the fun part).
Writing the Java batch zip extract
I figured there might be some utility already built that would allow you to easily batch extract zips to a directory, but where is the fun in that? I decided to write a program in Java over Christmas Break that would allow me to batch extract zips to any directory I specified. The Java Util has a built in library for handling zip files, but it requires a lot of boilerplate code (mostly from byte array buffers, to output to a zip output stream) and I realized that getting this to work with 100MB files, (let alone 2 GB files) was going to be very difficult. This is when I found Zip4j, an open-source library that can handle any size zip file.
What I love about Zip4j, is that it had an understandable API, and allowed for password-protected zip files. I downloaded the source library for Zip4j, and happily hummed along making methods that I was easily able to pass to Zip4j. At last, I was finally complete, and the time came to test out my code on a few “innocuous” zip files before hurdling my massive 212 zip file soundtrack library. It didn’t work▬That always leaves you scratching your head for a second. I mean, as a programmer, you don’t usually expect your code to work the first time you give it “real data”, but you hope it does. I checked over my code, walked through debug, there it was- Zip4j had thrown an exception I hadn’t caught- NonZipFileException. I looked back at the “zip file” I had specified. It was a rar file. I slapped my forehead and added in some handling lines for this exception and changed to an actual zip file. This time it worked.
Of course the next question was, would it work on such a large directory of files? Would it corrupt all the zip files that had taken hours to download? I decided to “bite the bullet” and find out. I watched in the command output as the utility quickly moved through all the files and cleanly deposited a separate folder for each album into my iTunes library. This made me smile. As a programmer, the best part of programming for me is in solving the puzzle and finally seeing the expected outcome. Don’t get me wrong, I love the journey, but being able to finally look out over the “Everest” summit when the program runs is breathtaking.
This was a fun little project. It is really simple code, but it does what it needs to in my opinion. I have included links to the source in my GitHub, as well as I have provided the executable jar file in the hopes that someone else might find it useful if they have a large amount of zip files which need to be extracted (with or without passwords). It is a simple little utility, (command line only at this point) you provide the source and destination locations, as well as the password(s), or location to a text file with a list of passwords (they must be in order of file appearance). I have also provided links to the Zip4j library. Zip4j does most of the heavy lifting, I have to give it most of the credit here.
Thanks for reading,
ReferencesBrown, Peter 2014. Gamespot.com. Access April 9, 2015.
Trackback from your site.