CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > How to > Create ZIP file in Java

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor
  • Home

Create ZIP file in Java

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 · 2 min read · 0 Comments
Share: in X

Overview

A Zip file is an archive archived file format that contains one or more files or directory. You can read more about it in Wikipedia. As a developer we often need to create ZIP file in Java to compress one or more files.

In this post, we will learn to create a Zip file using native way. With this example, you don’t need to add any third-party libraries.

1. Zip a single file

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;

/**
 * Creates the Zip File.
 */
public class ZipFileCreator {
	/**
	 * Adds a single file into zip.
	 * 
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		FileOutputStream fileOutputStream = null;
		ZipOutputStream zipOutputStream = null;
		FileInputStream fis = null;
		try {
			fileOutputStream = new FileOutputStream("singlefile.zip");
			zipOutputStream = new ZipOutputStream(fileOutputStream);
			File myFileToZip = new File("myfile.png");
			fis = new FileInputStream(myFileToZip);
			ZipEntry ze = new ZipEntry(myFileToZip.getName());
			zipOutputStream.putNextEntry(ze);
			/*
			 * Initializing byte array with 1024 byte so that if the input stream is too
			 * large then it will not load all the bytes into the memory. If we add too low
			 * like, 10 or 100 then, the loop will take most of its time
			 */
			byte[] byteArray = new byte[1024];
			int size = 0;
			while ((size = fis.read(byteArray)) != -1) {
				zipOutputStream.write(byteArray, 0, size);
			}
			zipOutputStream.closeEntry();
			System.out.println("Done... Zipped files...");
		} finally {
			if (fis != null)
				fis.close();
			if (zipOutputStream != null)
				zipOutputStream.close();
			if (fileOutputStream != null)
				fileOutputStream.close();
		}
	}
}

Things to remember

You should always close your ZipOutputStream after finishing your task. If you miss it to close then you will get an error while opening the Zip.

Conclusion

In this post we learned to create Zip file in Java by adding single file into it.

Related Posts:

  • Working with Files and Directories in Java
  • Control Statements in Java
  • What Is Java Swing? A Complete Guide to Java’s GUI Toolkit
  • MySQL Commands for Developers
  • Compile and Run Java Program
  • A Brief History Of Java
Tags:java
Was this article helpful?
← Previous ArticleWhat is Java? Exploring Its Key Features, Benefits, and Use Cases
Next Article →How to install MySQL ODBC driver in Ubuntu

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap