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 Zip files with Java

In this article you will see how you can work with zip files in Java to compress data.

Zipping of files let you compress data so that its size decreases to a large extent. You may compress files present on your local computer to save space on the hard disk. This can be helpful in situation where you have to compress files making use of a java program.

You can extend this program further to have the zipping scheduled for particular folders.

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.ZipOutputStream;
 

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. ZipOutputStream is for writing files in the ZIP file format.

Now we define the class along with main method

Listing 2: Defining class along with main method

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

Here,

  1. We define a class named Zipper
  2. We make an object of Zipper 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;
	 BufferedInputStream in = new BufferedInputStream(new FileInputStream("zip.txt"));
	 ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream("my.zip"))); 
	 out.putNextEntry(new ZipEntry("zip.txt"));
	 while((byteRead = in.read(data,0,1000)) != -1)
	 {  
	 out.write(data, 0, byteRead);
	 }
	 in.close();
	 out.flush();
	 out.close();
	  System.out.println("Zipped.");
	  }
	catch(Exception e)
	{
		System.out.println(e.getMessage());
	}
}
}

 

Here,

  1. We define a variable called data of byte[] type.
  2. We defined an object called in which will read from the input file
  3. We define a variable called out to write on the output zip file
  4. Now we tell zip about the next entry which will be zipped. Here we pass a text file zip.txt and this text file will be zipped
  5. Now we read the input text file and write that data in the zip. We use the buffer array we defined
  6. After completing now we close all the streams.
  7. 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.ZipOutputStream;

public class Zipper
{
public static void main(String args[])
{
	new Zipper().start();
}
public void start()
{
	try
	  {
	 byte[] data = new byte[1000]; 
	 int byteRead;
	 BufferedInputStream in = new BufferedInputStream(new FileInputStream("zip.txt"));
	 ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream("my.zip"))); 
	 out.putNextEntry(new ZipEntry("zip.txt"));
	 while((byteRead = in.read(data,0,1000)) != -1)
	 {  
	 out.write(data, 0, byteRead);
	 }
	 in.close();
	 out.flush();
	 out.close();
	  System.out.println("Zipped.");
	  }
	catch(Exception e)
	{
		System.out.println(e.getMessage());
	}
}
}

Output

Zip 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