Linchakin

10th Oct - How to Create Zip File in Java

 October 10, 2021     No comments   

In Java, working with zip file is not the same as working with word file or text file. In the context of zip file Java provides different ways to create zip or compressed or archive file. In this section, we will learn how to create zip file in Java.

The necessary classes and interfaces that are required to create a zip file are defined in java.util.zip package. The package does not only provide classes for ZIP format but also provides classes for the GZIP format. The package also provides the classes to read from and write to the zip file.

How to Create Zip File in Java

Using java.util.Zip Package

Before creating the Java program, first we need to download the apache common compress JAR file. Now create a Java project and add this JAR file to the project. Copy the following Java program and paste it in the class file that you have created.

CreateZipFile1.java

Let's compile and run the above program.

Output:

How to Create Zip File in Java

We observe that a zip file with the specified name is created. Let's open the zip file and see the specified file added to zip file or not.

How to Create Zip File in Java

Zipping Multiple Files

ZippingMultipleFiles.java

Output:

How to Create Zip File in Java

Zipping a Directory

Create a zip of a directory is a bit different form the above approach. Java provides two way to zip a directory:

  • Use Files.walkFileTree() Method
  • Read all files from the folder and add that files to list, then perform compression.

Using Files.walkFileTree()

  • The method walkFileTree() is presented since Java 7. It is defined in the Files class that resides in the nio.file package. It is used to reclusively zip the file. It makes code short and simple. The method walks over a file tree.

Syntax:

It accepts two parameters:

start: It denotes the starting file.

visitor: Th file visitor to invoke each file.

The FileVisitor is an interface that acts as an argument for the method. To walk over a file tree, we need to implement a FileVisitor interface. It specifies the required behavior at key points in the traversal process that are:

  • When a file is visited
  • Before a directory is accessed
  • After a directory is accessed
  • When a failure occurs

Let's implement the logic to create a zip file for a directory.

ZippingDirectory.java

Output:

How to Create Zip File in Java

Adblock test (Why?)


You may be interested in:
>> Is a Chromebook worth replacing a Windows laptop?
>> Find out in detail the outstanding features of Google Pixel 4a
>> Top 7 best earbuds you should not miss

Related Posts:
>> Recognizing 12 Basic Body Shapes To Choose Better Clothes
>>Ranking the 10 most used smart technology devices
>> Top 5+ Best E-readers: Compact & Convenient Pen
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook

Related Posts:

  • 14th Aug - Examples of Machine LearningMachine Learning technology has widely changed the lifestyle of a human beings as we are highly dependent on this technology. It is the subset of Arti… Read More
  • NMac Ked - Mac OSX Apps & Games Download You may be interested in: >> Is a Chromebook worth replacing a Windows laptop? >> Find out in detail the outstanding features of Google P… Read More
  • Tech You may be interested in: >> Is a Chromebook worth replacing a Windows laptop? >> Find out in detail the outstanding features of Google P… Read More
  • What to Pack for Gorilla Trekking • The Blonde Abroad I’ve fallen in love with exploring the great outdoors and (ethically) seeing animals in their natural habitat. From national parks around the globe… Read More
  • 14th Aug - Semi-Supervised LearningSemi-Supervised learning is a type of Machine Learning algorithm that represents the intermediate ground between Supervised and Unsupervised learning … Read More
Newer Post Older Post Home

0 Comments:

Post a Comment


Copyright © 2025 Linchakin | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com | Distributed By Gooyaabi Templates