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,
- [Fix] java.time.zone.ZoneRulesException: Unknown time-zone ID
- Parse XML file in Java using DOM Parser
- Java equals method - Tutorial
- [Program] How to read three different values using Scanner in Java
- Java: The value of the local variable string is not used
- Display Output in Java Console as a Table
- How to detect Operating System using Java code
- Java 8 Streams map() with examples
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Add newline character Java code example (\r \n \r\n)
- List of Java Major Minor Version Numbers
- IntelliJ Keyboard Shortcut to remove unused imports [Java]
- Java - Check if array contains the value
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- How to declare and initialize Array in Java Programming
- [Solved] com.sun.xml.ws.transport.http.servlet.WSServletContextListener ClassNotFoundException
- XmlRpcException ConnectException connection refused error
- Create a Zip file using Java Code programmatically
- List of jar files for Jax-ws (SOAP) based Java Web Services
- How to fix Java HTTP java.net.UnknownHostException
- List of jars required for Struts2 project
- [fix] java: incompatible types: double cannot be converted to java.lang.Integer Generics
- Maven BUILD FAILURE: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin
- Get the current timestamp in Java
- java: unclosed string literal [Error]
More Posts:
- Mac turn dark mode on or off using terminal command - MacOS
- Using Document Map in Notepad++ - NotepadPlusPlus
- [Error] There was an error connecting to the apple id server - HowTos
- How to create API Token for Atlassian account to connect Jira Cloud REST API - Jira
- [Java] Read a File with UTF-8 Encoding - Java
- Create simple struts2 project using maven commands - Java
- macOS Big Sur compatible Macs List - MacOS
- Add Custom header and footer to Windows Notepad file - NotepadPlusPlus