Unzip a file with GZIPInputStream in Java

Description

The following code shows how to unzip a file with GZIPInputStream.

Example


//from w  w w  .ja  v  a  2 s. co  m


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.GZIPInputStream;

public class Main {
  public static void main(String[] args) throws Exception {
    int sChunk = 8192;

    String zipname = "a.txt.gz";
    String source = "a.txt";
    FileInputStream in = new FileInputStream(zipname);
    GZIPInputStream zipin = new GZIPInputStream(in);
    byte[] buffer = new byte[sChunk];
    FileOutputStream out = new FileOutputStream(source);
    int length;
    while ((length = zipin.read(buffer, 0, sChunk)) != -1)
      out.write(buffer, 0, length);
    out.close();
    zipin.close();
  }
}




















Home »
  Java Tutorial »
    I/O »




Binary File
Byte Array
CharSet
Checksum
Console
Create Copy Move Delete
Directory
Drive
Encode Decode
File Attribute
File Lock
File System
GZIP
Jar File
NIO Buffer
Path
Scanner
StreamTokenizer
Temporary File
Text File
Zip