JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: How to uncompress a file from GZip format?


Description:

The below example shows how to uncompress a file from GZip format.


Code:
package com.java2novice.zip;

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

public class MyDecomGzip {

	public void doUnGzipFile(String filePath) {

		GZIPInputStream gis = null;
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream(filePath);
			gis = new GZIPInputStream(fis);
			fos = new FileOutputStream("C:/test.txt");
			byte[] tmp = new byte[4*1024];
			int size = 0;
			while ((size = gis.read(tmp)) > 0) {
				fos.write(tmp, 0, size);
			}
			fos.flush();
			System.out.println("Done with uncompressng GZip file.");
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			try{
				if(gis != null) gis.close();
				if(fos != null) fos.close();
			} catch(Exception ex){}
		}
	}

	public static void main(String a[]) {

		MyDecomGzip mfg = new MyDecomGzip();
		mfg.doUnGzipFile("C:/myGzip.gzip");
	}
}

Output:
Done with uncompressng GZip file.
<< Previous Program 

List of all java.util.zip class sample examples:

  1. How to compress byte array in java?
  2. How to decompress byte array in java?
  3. How to zip a single file?
  4. How to zip multiple files?
  5. How to read zip files entries or file name list?
  6. How to unzip files in java?
  7. How to generate checksum value for for a file in java?
  8. How to compress and store objects using zip utility?
  9. How to decompress the compressed objects using zip utility?
  10. How to zip a file using ZipFile class?
  11. How to compress a file in GZip format?
  12. How to uncompress a file from GZip format?
Knowledge Centre
Procedural Vs Object-oriented Programs
In procedural program, programming logic follows certain procedures and the instructions are executed one after another. In OOP program, unit of program is object, which is nothing but combination of data and code.

In procedural program, data is exposed to the whole program whereas in OOPs program, it is accessible with in the object and which in turn assures the security of the code.
Famous Quotations
The greatest obstacle to discovery is not ignorance; it is the illusion of knowledge.
-- Daniel J. Boorstin

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.