In this example, we will take a look at how to create a zip file programmatically in Java using the ZipOutputStream class,
Code:package org.code2care.java;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
*
* Java Examples Code2care
*
* Program to demonstrate how to
* compress files as zip using
* Java ZipOutputStream class
*
*/
public class JavaZipFilesExample {
public static void main(String[] args) throws Exception {
String fileToZipPath = "/Users/code2care/IdeaProjects/lambdas/src/myTextFile.txt";
String zipFileName = "/Users/code2care/IdeaProjects/lambdas/src/myTextFile.zip";
if(zipFile(fileToZipPath,zipFileName)) {
System.out.println("Zip file created...");
} else {
throw new Exception("Error occurred while zipping the file..");
}
}
public static boolean zipFile(String fileToZipPath, String zipFileName) {
boolean success = false;
try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFileName))) {
zipOutputStream.putNextEntry(new ZipEntry((new File(fileToZipPath)).getName()));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return success;
}
}
Output:

More Posts related to Java,
- How to Get List of All Country Codes in Java Using Locale Class
- Unsupported major.minor version 52.0 in java
- Java - How to set custom thread name?
- Get the current timestamp in Java
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- [fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring
- java: unclosed string literal [Error]
- Convert Java Byte Array to String with code examples
- Error: Can not find the tag library descriptor for
- Java 8 - Convert List to Map Examples
- Java - Calculate time taken for the code to execute in milliseconds or nanoseconds
- Fix java.net.ProtocolException: Invalid HTTP method
- Java: Convert Stream to List
- Java equals method - Tutorial
- List of Java JDBC Database Driver Jars, Classes and URLs Details
- Read YAML file Java Jackson Library
- How to display Java Date Time timezone GMT/UTC offset using SimpleDateFormat
- List of Java Keywords
- Enable JSON Pretty Print in Java Jackson
- How to Word-Warp Console logs in IntelliJ
- Convert Map to List in Java 8 using Stream API
- Create a Directory using Java Code
- Ways to Convert Integer or int to Long in Java
- [Program] How to read three different values using Scanner in Java
- Java JDBC Example with Oracle Database Driver Connection
More Posts:
- Send Extra Data with Ajax Get or Post Request - JavaScript
- How to Enable or Disable Dark Mode on macOS Ventura 13 - MacOS
- [Fix] Microsoft Store Error Code: 0x803F8001 - Microsoft
- Get List of all local branches git command - Git
- Android Images with Rounded Corners : ImageView - Android
- [fix] command not found curl - cURL
- How to display Java Date Time timezone GMT/UTC offset using SimpleDateFormat - Java
- Error 404 Tomcat homepage http://localhost:8080/ not displayed - Tomcat