Opening and extracting Zip files in java – It’s there for a while, but I just found out

2

A while ago I found out that it was possible to open Zip files with Java. Just open a regular java.io.File and pass it to the java.util.zip.ZipFile constructor.
It is possible for a long time (at least since version 1.3), but I didn’t expect this kind of functionality in an SDK. I was looking for a library to do it when a javadoc page from Sun showed up.

After analysis of that page I came up with a small piece of code. Again no rocket science, but it can save you some time and when I have to do something with zip files I just search for this blog ;-)
I created an application that uses xml files. Those xml files can be uploaded, but they can grow very big. The largest file I had to handle was about 83MB. Internet is fast but you still have to wait for these file sizes to be uploaded. A solution is zipping the file and let the server extract it.

<code>
<span style="color: #7f0055;"><strong>public </strong></span><span style="color: #7f0055;"><strong>void </strong></span><span style="color: #000000;">extractFile</span><span style="color: #000000;">(){</span>
<span style="color: #ffffff;">  </span><span style="color: #000000;">File file = </span><span style="color: #7f0055;"><strong>new </strong></span><span style="color: #000000;">File</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">"c:\\temp\\ZipFileWithManyFiles.zip"</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>
<span style="color: #ffffff;">  </span><span style="color: #000000;">ZipFile zipFile = </span><span style="color: #7f0055;"><strong>new </strong></span><span style="color: #000000;">ZipFile</span><span style="color: #000000;">(</span><span style="color: #000000;">file</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>
<span style="color: #ffffff;">  </span><span style="color: #000000;">Enumeration&lt;? </span><span style="color: #7f0055;"><strong>extends </strong></span><span style="color: #000000;">ZipEntry&gt; enumeration = zipFile.entries</span><span style="color: #000000;">()</span><span style="color: #000000;">;</span>
<span style="color: #ffffff;">  </span><span style="color: #7f0055;"><strong>while </strong></span><span style="color: #000000;">(</span><span style="color: #000000;">enumeration.hasMoreElements</span><span style="color: #000000;">()) {</span>
<span style="color: #ffffff;">    </span><span style="color: #000000;">ZipEntry zipEntry = enumeration.nextElement</span><span style="color: #000000;">()</span><span style="color: #000000;">;</span>
<span style="color: #ffffff;">    </span><span style="color: #000000;">String zipFileName = zipEntry.getName</span><span style="color: #000000;">()</span><span style="color: #000000;">;</span>

<span style="color: #ffffff;">    </span><span style="color: #000000;">InputStream inputStream = zipFile.getInputStream</span><span style="color: #000000;">(</span><span style="color: #000000;">zipEntry</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>
<span style="color: #ffffff;">    </span><span style="color: #000000;">OutputStream out = </span><span style="color: #7f0055;"><strong>new </strong></span><span style="color: #000000;">FileOutputStream</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">"c:\\temp\\extracted\\" </span><span style="color: #000000;">+ zipFileName</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>
<span style="color: #ffffff;">    </span><span style="color: #000000;">writeInputStreamToOutputStream</span><span style="color: #000000;">(</span><span style="color: #000000;">inputStream, out</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>
<span style="color: #ffffff;">    </span><span style="color: #000000;">out.close</span><span style="color: #000000;">()</span><span style="color: #000000;">;</span>
<span style="color: #ffffff;">    </span><span style="color: #000000;">inputStream.close</span><span style="color: #000000;">()</span><span style="color: #000000;">;</span>
<span style="color: #ffffff;">  </span><span style="color: #000000;">}</span>
<span style="color: #000000;">}</span>

<span style="color: #7f0055;"><strong>private </strong></span><span style="color: #7f0055;"><strong>void </strong></span><span style="color: #000000;">writeInputStreamToOutputStream</span><span style="color: #000000;">(</span><span style="color: #000000;">InputStream inputStream, OutputStream out</span><span style="color: #000000;">) </span><span style="color: #7f0055;"><strong>throws </strong></span><span style="color: #000000;">IOException </span><span style="color: #000000;">{</span>
<span style="color: #ffffff;">    </span><span style="color: #3f7f5f;">// Transfer bytes from the ZIP file to the output file</span>
<span style="color: #ffffff;">    </span><span style="color: #7f0055;"><strong>byte</strong></span><span style="color: #000000;">[] </span><span style="color: #000000;">buf = </span><span style="color: #7f0055;"><strong>new </strong></span><span style="color: #7f0055;"><strong>byte</strong></span><span style="color: #000000;">[</span><span style="color: #990000;">1024</span><span style="color: #000000;">]</span><span style="color: #000000;">;</span>
<span style="color: #ffffff;">    </span><span style="color: #7f0055;"><strong>int </strong></span><span style="color: #000000;">len;</span>
<span style="color: #ffffff;">    </span><span style="color: #7f0055;"><strong>while </strong></span><span style="color: #000000;">((</span><span style="color: #000000;">len = inputStream.read</span><span style="color: #000000;">(</span><span style="color: #000000;">buf</span><span style="color: #000000;">)) </span><span style="color: #000000;">&gt; </span><span style="color: #990000;">0</span><span style="color: #000000;">) {</span>
<span style="color: #ffffff;">        </span><span style="color: #000000;">out.write</span><span style="color: #000000;">(</span><span style="color: #000000;">buf, </span><span style="color: #990000;">0</span><span style="color: #000000;">, len</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span>
<span style="color: #ffffff;">    </span><span style="color: #000000;">}</span>
<span style="color: #000000;">}</span></code>

A ZipFile is not a File when you follow the Object Oriented inheritance rules, that’s a bit strange, but I can live with it. Once you instansiated the ZipFile you can call the entries() method and you will receive a list with all files in the archive.
You can get an InputStream for every entry, note that you have to get the InputStream from ZipFile, not ZipEntry.
To write the InputStream to an OutputStream I wrote a small method (does anyone know a better solution?, my method doesn’t feel right). And finally you have to close the out and inputStream.

The ZipFile classes are very useful, the API is a bit odd, but does the trick.

Share.

About Author

2 Comments

  1. In your last line you say ‘And finally you have to close the out en inputStream’. But you don’t use the finally clause in your code, which is a bug in this case!