🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
Creating and extracting ZIP archives is a common requirement in many applications for file management and distribution. Java provides robust support for these operations through its java.util.zip package. This guide will demonstrate how to create and extract ZIP archives using the ZipOutputStream and ZipInputStream classes, respectively, including handling exceptions appropriately.
Table of Contents
- Importing Required Packages
- Creating ZIP Archives
- Extracting ZIP Archives
- Handling Exceptions
- Complete Example
- Conclusion
Importing Required Packages
To create and extract ZIP archives, you need to import the necessary classes from the java.util.zip package.
Example
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
Creating ZIP Archives
To create a ZIP archive, you can use the ZipOutputStream class. The following example demonstrates how to compress multiple files into a single ZIP file.
Example
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class CreateZipArchive {
public static void main(String[] args) {
String[] srcFiles = {"file1.txt", "file2.txt", "file3.txt"};
String zipFile = "compressed_files.zip";
try (FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos)) {
for (String srcFile : srcFiles) {
File fileToZip = new File(srcFile);
try (FileInputStream fis = new FileInputStream(fileToZip)) {
ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
zos.putNextEntry(zipEntry);
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) >= 0) {
zos.write(buffer, 0, length);
}
}
}
System.out.println("Files compressed successfully: " + zipFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Extracting ZIP Archives
To extract files from a ZIP archive, you can use the ZipInputStream class. The following example demonstrates how to extract all files from a ZIP archive.
Example
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class ExtractZipArchive {
public static void main(String[] args) {
String zipFile = "compressed_files.zip";
String destDir = "output_directory";
File dir = new File(destDir);
if (!dir.exists()) dir.mkdirs();
try (FileInputStream fis = new FileInputStream(zipFile);
ZipInputStream zis = new ZipInputStream(fis)) {
ZipEntry zipEntry = zis.getNextEntry();
while (zipEntry != null) {
File newFile = newFile(dir, zipEntry);
if (zipEntry.isDirectory()) {
if (!newFile.isDirectory() && !newFile.mkdirs()) {
throw new IOException("Failed to create directory " + newFile);
}
} else {
File parent = newFile.getParentFile();
if (!parent.isDirectory() && !parent.mkdirs()) {
throw new IOException("Failed to create directory " + parent);
}
try (FileOutputStream fos = new FileOutputStream(newFile)) {
byte[] buffer = new byte[1024];
int length;
while ((length = zis.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
}
}
zipEntry = zis.getNextEntry();
}
zis.closeEntry();
System.out.println("Files extracted successfully to: " + destDir);
} catch (IOException e) {
e.printStackTrace();
}
}
private static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException {
File destFile = new File(destinationDir, zipEntry.getName());
String destDirPath = destinationDir.getCanonicalPath();
String destFilePath = destFile.getCanonicalPath();
if (!destFilePath.startsWith(destDirPath + File.separator)) {
throw new IOException("Entry is outside of the target dir: " + zipEntry.getName());
}
return destFile;
}
}
Handling Exceptions
When creating and extracting ZIP archives, several exceptions might be thrown:
IOException: If an I/O error occurs.SecurityException: If a security manager exists and denies access to the file.
Example with Exception Handling
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class ZipWithExceptionHandling {
public static void main(String[] args) {
// Creating ZIP Archive
String[] srcFiles = {"file1.txt", "file2.txt", "file3.txt"};
String zipFile = "compressed_files.zip";
try (FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos)) {
for (String srcFile : srcFiles) {
File fileToZip = new File(srcFile);
try (FileInputStream fis = new FileInputStream(fileToZip)) {
ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
zos.putNextEntry(zipEntry);
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) >= 0) {
zos.write(buffer, 0, length);
}
}
}
System.out.println("Files compressed successfully: " + zipFile);
} catch (IOException e) {
System.err.println("I/O error during compression: " + e.getMessage());
} catch (SecurityException e) {
System.err.println("Access denied during compression: " + e.getMessage());
}
// Extracting ZIP Archive
String destDir = "output_directory";
File dir = new File(destDir);
if (!dir.exists()) dir.mkdirs();
try (FileInputStream fis = new FileInputStream(zipFile);
ZipInputStream zis = new ZipInputStream(fis)) {
ZipEntry zipEntry = zis.getNextEntry();
while (zipEntry != null) {
File newFile = newFile(dir, zipEntry);
if (zipEntry.isDirectory()) {
if (!newFile.isDirectory() && !newFile.mkdirs()) {
throw new IOException("Failed to create directory " + newFile);
}
} else {
File parent = newFile.getParentFile();
if (!parent.isDirectory() && !parent.mkdirs()) {
throw new IOException("Failed to create directory " + parent);
}
try (FileOutputStream fos = new FileOutputStream(newFile)) {
byte[] buffer = new byte[1024];
int length;
while ((length = zis.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
}
}
zipEntry = zis.getNextEntry();
}
zis.closeEntry();
System.out.println("Files extracted successfully to: " + destDir);
} catch (IOException e) {
System.err.println("I/O error during decompression: " + e.getMessage());
} catch (SecurityException e) {
System.err.println("Access denied during decompression: " + e.getMessage());
}
}
private static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException {
File destFile = new File(destinationDir, zipEntry.getName());
String destDirPath = destinationDir.getCanonicalPath();
String destFilePath = destFile.getCanonicalPath();
if (!destFilePath.startsWith(destDirPath + File.separator)) {
throw new IOException("Entry is outside of the target dir: " + zipEntry.getName());
}
return destFile;
}
}
Complete Example
Here is a complete example demonstrating how to create and extract ZIP archives using the ZipOutputStream and ZipInputStream classes with proper exception handling.
CreateExtractZipExample.java
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class CreateExtractZipExample {
public static void main(String[] args) {
// Creating ZIP Archive
String[] srcFiles = {"file1.txt", "file2.txt", "file3.txt"};
String zipFile = "compressed_files.zip";
try (FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos)) {
for (String srcFile : srcFiles) {
File fileToZip = new File(srcFile);
try (FileInputStream fis = new FileInputStream(fileToZip)) {
ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
zos.putNextEntry(zipEntry);
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) >= 0) {
zos.write(buffer, 0, length);
}
}
}
System.out.println("Files compressed successfully: " + zipFile);
} catch (IOException e) {
System.err.println("I/O error during compression: " + e.getMessage());
} catch (SecurityException e) {
System.err.println("Access denied during compression: " + e.getMessage());
}
// Extracting ZIP
Archive
String destDir = "output_directory";
File dir = new File(destDir);
if (!dir.exists()) dir.mkdirs();
try (FileInputStream fis = new FileInputStream(zipFile);
ZipInputStream zis = new ZipInputStream(fis)) {
ZipEntry zipEntry = zis.getNextEntry();
while (zipEntry != null) {
File newFile = newFile(dir, zipEntry);
if (zipEntry.isDirectory()) {
if (!newFile.isDirectory() && !newFile.mkdirs()) {
throw new IOException("Failed to create directory " + newFile);
}
} else {
File parent = newFile.getParentFile();
if (!parent.isDirectory() && !parent.mkdirs()) {
throw new IOException("Failed to create directory " + parent);
}
try (FileOutputStream fos = new FileOutputStream(newFile)) {
byte[] buffer = new byte[1024];
int length;
while ((length = zis.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
}
}
zipEntry = zis.getNextEntry();
}
zis.closeEntry();
System.out.println("Files extracted successfully to: " + destDir);
} catch (IOException e) {
System.err.println("I/O error during decompression: " + e.getMessage());
} catch (SecurityException e) {
System.err.println("Access denied during decompression: " + e.getMessage());
}
}
private static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException {
File destFile = new File(destinationDir, zipEntry.getName());
String destDirPath = destinationDir.getCanonicalPath();
String destFilePath = destFile.getCanonicalPath();
if (!destFilePath.startsWith(destDirPath + File.separator)) {
throw new IOException("Entry is outside of the target dir: " + zipEntry.getName());
}
return destFile;
}
}
In this example, the methods for creating and extracting ZIP archives are demonstrated, and exceptions are handled to ensure that informative messages are displayed if an error occurs.
Conclusion
Creating and extracting ZIP archives in Java using ZipOutputStream and ZipInputStream is straightforward. By understanding how to use these classes and handle potential exceptions, you can effectively manage file compression and decompression in your Java applications. Remember to always handle exceptions appropriately to ensure your application can respond to errors gracefully.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment