Zip File Folder Example Using Java 8 (2020) | TechGeekNext >>







Zip File Folder Example Using Java 8 (2020)

In this tutorial, will show you how to zip the folder 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











Recommendation for Top Popular Post :