How to compress files in ZIP format in Java

0 Flares 0 Flares ×

Sometimes in your Java application you may need to compress files in zip format while file related applications. Luckily Java comes with inbuilt zip utility packages that can be use to compress any file in zip format. Here is a very straightforward tutorial to compress any file in zip format in Java program.

The Concept

The concept of compressing the file in Java is quite straightforward and simple.

  1. Read the file with FileInputStream
  2. Use the Java zip utility and add to ZipEntry
  3. Output it to ZipOutputStream
  4. Once done close all resources.

File Compression Example in Java

We are going to compress the file ‘mylog.txt’ to ‘mycomressslog.zip’. The below Java program will do the compression of the file.

package com.lessonslab.util.zip;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipAFileInJava
{
public static void main( String[] args )
{
File inputFile = new File("/home/hduser/test/mylog.txt");
String zipFile = "/home/hduser/test/mycomressslog.zip";
zipTheFile(inputFile, zipFile);
}

private static void zipTheFile(File sourceFile, String targetFile)
{
byte[] buffer = new byte[1024];
try
{
FileInputStream in = new FileInputStream(sourceFile);
FileOutputStream fos = new FileOutputStream(targetFile);
ZipOutputStream zos = new ZipOutputStream(fos);
//Initialize the ZipEntry with the included file name, you can give any name in the parameter
ZipEntry ze= new ZipEntry(sourceFile.getName());
zos.putNextEntry(ze);
System.out.println("Compression started...");
int len;
while ((len = in.read(buffer)) > 0)
{
zos.write(buffer, 0, len);
}
//Remember to close all opened resources.
in.close();
zos.closeEntry();
zos.close();
System.out.println("Done!!!!!!");
System.out.println(sourceFile.getCanonicalPath()+" is zipped to "+targetFile);
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
}

Output

Compression started...
Done!!!!!!
/home/hduser/test/mylog.txt is zipped to /home/hduser/test/mycomressslog.zip

The above program is capable of zipping a single file, we can even zip a complete folder recursively in a similar way but little bit different. In my next tutorial I will show how to recursively compress a folder into zip format using Java program.

Guru

+Ayodhyanath Guru holds a B.Tech degree in Electrical Engineering and has worked with various prestigious clients in the IT industry and presently working as a Software Engineer. He is a part time blogger and presently authors the Jafaloo.Com blog. Being a tech enthusiast Guru likes to surf the web and blogs about interesting technical topics like How-To guides, freewares, Tutorials, Software, Gadgets, web applications etc. Apart from blogging he likes coding in Java/J2EE and PHP.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 0 Flares ×