Skip to content

Instantly share code, notes, and snippets.

@bonifaido
Created December 13, 2011 19:04
Show Gist options
  • Save bonifaido/1473385 to your computer and use it in GitHub Desktop.
Save bonifaido/1473385 to your computer and use it in GitHub Desktop.
Correct unzipping with Java
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;
public class GZipTest {
public static void main(String[] args) throws IOException {
/**
* GZIPInputStream has internal buffer for reading the underlying
* InputStream's bytes (512 bytes by default, now 8192), but it
* doesn't buffer the inflation of the bytes. This is not a problem
* when you use the read(byte[] bytes) but when you use the read()
* method frequently you may be in trouble without buffering the
* GZIPInputStream itself.
*
* Don't try running this without the BufferedInputStream.
*/
InputStream in =
new BufferedInputStream(
new GZIPInputStream(
new FileInputStream("/path/to/bigbinary.tar.gz"), 8192));
long start = System.currentTimeMillis();
System.out.print("Started reading... ");
while (in.read() != -1) {
}
long end = System.currentTimeMillis();
System.out.println("ended. Took: " + (end - start) + " ms");
in.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment