Skip to main content

How to Zip your File using Java


Today, I will teach you how to create a program that will zip your file using Java. We all know that zip is a popular compression format that also contains one or more files that have been compressed into the ZIP format.

So, now let's start this tutorial!

1. Open Notepad. Encode any data in there and save it to the same folder with your java program and named it as READ FIRST.txt (Note: you can name your text file in any name that you want).

2. Open JCreator or NetBeans and make a java program with a file name of

3. Import package. Hence we will use an input/output in creating files. Import also package to access the zip class from this utilities.

  1. import*;
  2. import*;

4. In your main, initialize the data.txt that you have created a while ago for this file. This READ FIRTS.txt file is the file that we will going to locate. Initialize also the name of the zip file after compression. Here I used This will be in your Try and Catch method.

  1. try {
  2. String file = "READ FIRST.txt";
  3. String zipFile = "";

Make a ZipOutputStream for the zipFile variable that you have created. This will trigger to have an output for zipping. Then the FileInputStream for file variable that will be your input file.

  1. ZipOutputStream zipStream = new ZipOutputStream(new FileOutputStream(zipFile));
  2. FileInputStream fileStream = new FileInputStream(file);

Now, put a new ZipEntry in the ZipOutputStream.

  1. zipStream.putNextEntry(new ZipEntry(file));

Initialize the value of the file size and its byte.

  1. int size = 0;
  2. byte[] buffer = new byte[1024];

Create a while loop that reads data to the end of the file and write it to the zip for the outputstream.

  1. while ((size =, 0, buffer.length)) > 0) {
  2. zipStream.write(buffer, 0, size);
  3. }

Close the zip process after finishing the compression.

  1. zipStream.closeEntry();
  2. fileStream.close();
  3. zipStream.close();

In your Catch method, prefer to catch IOException then use printStackTrace() method. This will help to trace the exception and identify which method causes the bug.

  1. catch (IOException e) {
  2. e.printStackTrace();
  3. }



Hope this helps! :)

Best Regards,
Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer
Mobile: 09079373999
Telephone: 826-9296
E-mail:[email protected]

Visit and like my page on Facebook at: Bermz ISware Solutions

Subscribe at my YouTube Channel at: Ser Bermz

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.