Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to Unzip files with Java

In this article you will see how you can work with unzip files in Java.

This can be helpful in situation where you have to unzip files making use of a java program. Its always useful to zip your files to save space on your local computer.

Now suppose you have several files zipped and you want to unzip some selected files on basis of some rule at that time you may take the reference from this program.

Listing 1: Importing classes

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream; 

Here,

  1. BufferedInputStream is used to buffer the input and can be used to read file
  2. BufferedOutputStream can be used to write bytes in output stream
  3. A FileInputStream obtains input bytes from a file in a file system.
  4. A File output stream is an output stream for writing data to a File
  5. ZipEntry class is used to show the entry in ZIP file
  6. ZipInputStream is for reading files in the ZIP file.

Now we define the class along with main method

Listing 2: Defining class along with main method

public class Unzip
{
public static void main(String args[])
{
	new Unzip().start(); 
}

Here,

  1. We define a class named Unzip
  2. We make an object of Unzip and call the start method which will contain the processing part.

Now we will define the start method

Listing 3: Define the start method

public void start()
{
	try
	  {
		 byte[] data = new byte[1000]; 
	  int byteRead;
	  
	  BufferedOutputStream bout = null;
	  ZipInputStream  zin = new ZipInputStream(new BufferedInputStream(new FileInputStream("my.zip")));
	  ZipEntry entry;
	  while((entry = zin.getNextEntry()) != null)
	 {
	 byteRead=0;
	 data = new byte[1000];
	 bout = new BufferedOutputStream(new FileOutputStream(entry.getName()),1000);
	 while ((byteRead = zin.read(data,0,1000)) != -1)
	 {
	  bout.write(data,0,byteRead);
	 }
	 bout.flush();
	 bout.close();
	 }
	  }
	 catch(Exception e)
	  {
	 e.printStackTrace();
	  }  
}
}

Here,

  1. We define a variable called data of byte[] type.
  2. We defined an object called bout which will be used to write the files from the zip files
  3. We define a variable called zin which read from the zip file
  4. Now we start reading the zip file and for each file found we write it making use of the bout object.We use the getName method to obtain the name of file present in the zip
  5. After completing now we close all the streams.
  6. If any exception occurs then we catch it making use of the catch block.

Listing 4:Full Source code

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Unzip 
{
public static void main(String args[])
{
	new Unzip().start();
}

public void start()
{
	try
	  {
		 byte[] data = new byte[1000]; 
	  int byteRead;
	  
	  BufferedOutputStream bout = null;
	  ZipInputStream  zin = new ZipInputStream(new BufferedInputStream(new FileInputStream("my.zip")));
	  ZipEntry entry;
	  while((entry = zin.getNextEntry()) != null)
	 {
	 byteRead=0;
	 data = new byte[1000];
	 bout = new BufferedOutputStream(new FileOutputStream(entry.getName()),1000);
	 while ((byteRead = zin.read(data,0,1000)) != -1)
	 {
	  bout.write(data,0,byteRead);
	 }
	 bout.flush();
	 bout.close();
	 }
	  }
	 catch(Exception e)
	  {
	 e.printStackTrace();
	  }  
}
}

Output

Unzip File with Java

This is all. Hope you liked the article. See you next time.



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login