Adblocker detected! Please consider whitelist or disable this site.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

To keep the site operating, we need funding, and practically all of it comes from internet advertising.

If you still want to continue, Please add techgeeknext.com to your ad blocking whitelist or disable your adblocking software.

×
Zip File Folder Example Using Java 8 (2024) | TechGeekNxt >>

Zip File Folder Example Using Java 8 (2024)

In this tutorial, will show you how to zip the folder (compress recursively all files and child folders) using Java 8 and using Core Java libraries. The core libraries belongs to java.util.zip package. Lets start with zipping the folder. I have created some text files in Notes folder as shown below:
Folder to zip
Will give source folder path as 'D:\Notes' and program will zip/compress all files into zip called Notes.zip. We wrapped Files.walk in try with resources block so that stream can be closed.

Example to Zip Folder

package com.techgeeknext;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipUtility {

    private List <String> fileList;
    private static final String NOTES_ZIP = "D:\\Notes.zip";
    private static final String FOLDER_TO_ZIP = "D:\\Notes";

    public static void main(String[] args) throws IOException {
        zip(FOLDER_TO_ZIP,NOTES_ZIP);
    }

    public static void zip(final String sourcNoteseDirPath, final String zipFilePath) throws IOException {
        Path zipFile = Files.createFile(Paths.get(zipFilePath));

        Path sourceDirPath = Paths.get(sourcNoteseDirPath);
        try (ZipOutputStream zipOutputStream = new ZipOutputStream(Files.newOutputStream(zipFile));
             Stream<Path> paths = Files.walk(sourceDirPath)) {
            paths
                    .filter(path -> !Files.isDirectory(path))
                    .forEach(path -> {
                        ZipEntry zipEntry = new ZipEntry(sourceDirPath.relativize(path).toString());
                        try {
                            zipOutputStream.putNextEntry(zipEntry);
                            Files.copy(path, zipOutputStream);
                            zipOutputStream.closeEntry();
                        } catch (IOException e) {
                            System.err.println(e);
                        }
                    });
        }

        System.out.println("Zip is created at : "+zipFile);
    }
}

Output:

Zip is created at : D:\Notes.zip


Java Unzip File Folder Example :

In next tutorial, will learn how to Unzip the folder using Core Java libraries. To unzip a zip file, we will make use of ZipInputStream to read the zip file.
Checkout next post, we will see how to unzip files or directory using core Java features.










Recommendation for Top Popular Post :