You are on page 1of 3

Here you will learn how to unzip a file using java.

Unzipping a File
Lets learn how to unzip a file using java. The java.util.zip.ZipEntry class is used to represent a zip file entry. The java.util.zip.ZipEntry class implements java.util.zip.ZipConstants interface. Here we passing the zipped file name to be unzipped using command line argument .To unzip a file first create an object of byte stream then take the array of any size as per your convenience (Here we are taking 1000 as the size of byte array ) then create a text file. First convert the zipped file into the byte array and then write the zipped file into it, To flush and close the file call flush() and close() respectively. If it throws any exception then catch it in try/catch block. We are printing the whole exception using printStrackTrace() method.
Here is the code of the program :

import java.io.*; import java.util.zip.*; public class UnzipExample { public static void main(String a[]) { try { BufferedOutputStream out = null; ZipInputStream in = new ZipInputStream (new BufferedInputStream(new FileInputStream(a[0]))); ZipEntry entry; while((entry = in.getNextEntry()) != null) { int count; byte data[] = new byte[1000]; out = new BufferedOutputStream(new FileOutputStream("out.txt"),1000); while ((count = in.read(data,0,1000)) != -1) { out.write(data,0,count); } out.flush(); out.close(); } }catch(Exception e) { e.printStackTrace(); } } }

Here, you will learn to compress a file. You can compress any file using various standalone applications. These standalone applications are nothing but the zip tools.

Zipping a File
This example shows how to create a zip file in java. In other words, we will learn how to compress a file just to take less space. We can compress any file using various standalone applications. These standalone applications are nothing but the zip (compress or decompress )tools. Examples of zip tools are Winrar, WinZip etc. It is also possible to zip and unzip the files from your Java applications. This example shows how we zip a file through a java program. The java.util.zip.DeflaterOutputStream class is immediate supper class of java.util.zip.ZipOutputStream class. java.util.zip. ZipOutputStream implements java.util.zip.ZipConstants interface. The class ZipOutStream implements an output stream filter to write files in the zip file format. This class can be used for both compressed and uncompressed files. The java.io.FilterOutputStream class extends java.io.BufferedOutputStream class and implements a buffered output stream. We can write a byte steam into a file on the system. The java.io.FileInputStream class extends java.io.InputStream class. FileInputStream class is used to obtain input bytes from a file in a file system. The FileInputStream class is used to read stream of row bytes (such as image data), for reading streams of characters by considering FileReader class. putNextEntry(new ZipEntry(filesToZip)): This is the method of ZipOutputStream class which is used to enter files one by one to be zipped. This method is used to close previous zip entry if any active and create the next zip entry by passing the instance of the ZipEntry class which holds the file name to be zipped. In this example we are going to zip "profile.txt" into "outFile.zip" .To execute this example you first need to create profile.txt in same directory.

Here is the code of the program : import java.io.*; import java.util.zip.*; public class ZipFileExample { public static void main(String a[]) { try { ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream("outFile.zip"))); byte[] data = new byte[1000]; BufferedInputStream in = new BufferedInputStream (new FileInputStream("profile.txt")); int count; out.putNextEntry(new ZipEntry("outFile.zip")); while((count = in.read(data,0,1000)) != -1) { out.write(data, 0, count); } in.close(); out.flush(); out.close(); System.out.println("Your file is zipped"); } catch(Exception e) { e.printStackTrace(); } } }

You might also like