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,
- Java equals method - Tutorial
- Unbound classpath container: JRE System Library [JavaSE-1.7]
- Spring Boot: @RequestBody not applicable to method
- Java 8: Steam map with Code Examples
- Java Program: Random Number Generator
- Java java.time.Clock class code examples [Java Date Time API]
- Fix: type argument is not within bounds of type-variable T
- [Fix] java.net.MalformedURLException: unknown protocol
- Java 7 addSuppression() and getSuppression() Exception Handling
- Convert Java Array to ArrayList Code Example
- How to Word-Warp Console logs in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Remove Trailing zeros BigDecimal Java
- CRUD operations in Spring Boot + JDBC
- [Java Threads] Should we extend Thread Class or implement Runnable interface
- Json Serialization and Deserialization using Java Jackson
- Create simple struts2 project using maven commands
- How to install Java OpenJDK 11 on Alpine Linux
- Unsupported major.minor version 52.0 in java
- Error: Can not find the tag library descriptor for
- Java: Convert String to Binary
- How to run Java Unit Test cases with Apache Maven?
- Java: Testing Private Methods in JUnit using reflection API Example
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Java Join Strings with Comma Separator
More Posts:
- How to fix java.net.NoRouteToHostException in Android Studio - Android-Studio
- Fix: Xbox Error Code: 0x80190190 - Microsoft
- How to install pip on macOS using terminal command [Python] - Python
- How to Setup maven on Mac (macOS) - Mac-OS-X
- Tool: Convert Cron Expression To Plain English Text (Supports Quartz) - Tools
- How to pass value to another Power Apps screen - PowerApps
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException - Java
- How to delete a Python Virtual Environment - Python