In this tutorial, we are going to learn how to zip the given folder and unzip the zip file using Java.
Zip the folder or directory:
Let's create a java class ZipUnzip.java and create the main method.
public static void main(String[] args) {
String folderInputPath = "path/of/folder/to/zip";
String outputPath = "output/path/compressed.zip";
try {
zip(folderInputPath, outputPath);
} catch (IOException e) {
System.out.println("Error due to: " + e.getMessage());
}
}
Now, create a method to zip the folder.
private static void zip(String folderInputPath, String outputPath) throws IOException {
Path sourceFolderPath = Paths.get(folderInputPath);
File zipPath = new File(outputPath);
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipPath));
Files.walkFileTree(sourceFolderPath, new SimpleFileVisitor<Path>() {
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
zos.putNextEntry(new ZipEntry(sourceFolderPath.relativize(file).toString()));
Files.copy(file, zos);
zos.closeEntry();
return FileVisitResult.CONTINUE;
}
});
zos.close();
}
We are using java standard core library classes to zip. Files.walkFileTree() is used to navigate recursively through the directory tree. Once we execute the code we can see the zip file is created inside the output path provided. We can verify the compression manually by extracting it.
Unzip the zip file:
Let's create a method unzip() for unzipping the compressed file
public static void main(String[] args) {
String zipFilePath = "zip/file/path/compressed.zip";
String zipOutputPath = "output/path/to/unzip";
try {
unzip(zipFilePath, zipOutputPath);
} catch (IOException e) {
System.out.println("Error due to: " + e.getMessage());
}
}
private static void unzip(String zipFilePath, String outputDir) throws IOException {
byte[] buffer = new byte[1024];
ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFilePath));
ZipEntry zipEntry = zis.getNextEntry();
while (zipEntry != null) {
File newFile = new File(outputDir + File.separator, zipEntry.getName());
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);
}
FileOutputStream fos = new FileOutputStream(newFile);
int len = 0;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
}
zipEntry = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
}
Here, we are using ZipInputStream to read the file and FileOutputStream to write the file to the provided output directory.
The overall code implementation looks like below:
package io;
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class ZipUnzip {
public static void main(String[] args) {
String folderInputPath = "path/of/folder/to/zip";
String outputPath = "output/path/compressed.zip";
String zipFilePath = "zip/file/path/compressed.zip";
String zipOutputPath = "output/path/to/unzip";
try {
zip(folderInputPath, outputPath);
unzip(zipFilePath, zipOutputPath);
} catch (IOException e) {
System.out.println("Error due to: " + e.getMessage());
}
}
private static void zip(String folderInputPath, String outputPath) throws IOException {
Path sourceFolderPath = Paths.get(folderInputPath);
File zipPath = new File(outputPath);
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipPath));
Files.walkFileTree(sourceFolderPath, new SimpleFileVisitor<Path>() {
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
zos.putNextEntry(new ZipEntry(sourceFolderPath.relativize(file).toString()));
Files.copy(file, zos);
zos.closeEntry();
return FileVisitResult.CONTINUE;
}
});
zos.close();
}
private static void unzip(String zipFilePath, String outputDir) throws IOException {
byte[] buffer = new byte[1024];
ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFilePath));
ZipEntry zipEntry = zis.getNextEntry();
while (zipEntry != null) {
File newFile = new File(outputDir + File.separator, zipEntry.getName());
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);
}
FileOutputStream fos = new FileOutputStream(newFile);
int len = 0;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
}
zipEntry = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
}
}
0 comments:
New comments are not allowed.