How to Zip your File using Java

Submitted by: 
Language: 
Visitors have accessed this post 1934 times.

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 zipFile.java.

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

  1. import java.io.*;
  2. import java.util.zip.*;

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 createdZipFile.zip. This will be in your Try and Catch method.

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

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 = fileStream.read(buffer, 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. }


Output:

output

Hope this helps! :)

Best Regards,

Engr. Lyndon R. Bermoy

IT Instructor/System Developer/Android Developer/Freelance Programmer

If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware


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 SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.