Friday, October 16, 2009

Unzipping .gz files in Java

Unzipping files in easy, just double click and the OS unzips it using the default application.

What in case there are hundred of files in a folder you want to unzip. (We won't go into the details of why we want to unzip the files, when we can directly read the zipped version... That's another story).

Task: How to unzip a ".gz" file using Java.

Tools: Classes GZIPInputStream and OutputStream helps us to do the task.

GZIPInputStream gzipInputStream = new GZIPInputStream(newFileInputStream(inFilename));

OutputStream out = new FileOutputStream(outFilename);

byte[] buf = new byte[102400]; //size can be changed according to programmer's need.
int len;
while ((len = > 0) {
out.write(buf, 0, len);

The main points to note here are:-
a) GZIPInputStream class which creates a inputStream for the file to be read.
b) function, reads the uncompressed data into the buffer. The return type of this function is int, which specifies the number of bytes read.
c) The data read can be written into a FileOutputStream in uncompressed form.

So easy to unzip files.


Kapil Dalwani

Curl (like) implemented in Python

The purpose of this post is to implement something that mirrors files from the web. In other words, to download set of files from a seed and download them to your local directory.

I will use Python to implement the same. There are far better code than using my code. I am implementing this just to get familiarity on Python and some of its library.

I am trying to implement something similar to CURL in python.

Coming soon. !

Kapil Dalwani