Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

compress a directory in java

package org.kodejava.util.zip;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipDirectoryExample {
    private final List<String> fileList = new ArrayList<>();

    public static void main(String[] args) {
        String dir = "D:/Data";
        String zipFile = "D:/Data.zip";

        ZipDirectoryExample zip = new ZipDirectoryExample();
        zip.compressDirectory(dir, zipFile);
    }

    private void compressDirectory(String dir, String zipFile) {
        File directory = new File(dir);
        getFileList(directory);

        try (FileOutputStream fos = new FileOutputStream(zipFile);
             ZipOutputStream zos = new ZipOutputStream(fos)) {

            for (String filePath : fileList) {
                System.out.println("Compressing: " + filePath);

                // Creates a zip entry.
                String name = filePath.substring(directory.getAbsolutePath().length() + 1);

                ZipEntry zipEntry = new ZipEntry(name);
                zos.putNextEntry(zipEntry);

                // Read file content and write to zip output stream.
                try (FileInputStream fis = new FileInputStream(filePath)) {
                    byte[] buffer = new byte[1024];
                    int length;
                    while ((length = fis.read(buffer)) > 0) {
                        zos.write(buffer, 0, length);
                    }

                    // Close the zip entry.
                    zos.closeEntry();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Get files list from the directory recursive to the subdirectory.
     */
    private void getFileList(File directory) {
        File[] files = directory.listFiles();
        if (files != null && files.length > 0) {
            for (File file : files) {
                if (file.isFile()) {
                    fileList.add(file.getAbsolutePath());
                } else {
                    getFileList(file);
                }
            }
        }

    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: change attributes of player spigot 
Java :: Iterating Through the Java Map 
Java :: android java show hide keyboard in AndroidManifest 
Java :: Print Text Using Java 
Java :: make quotation a string 
Java :: Explain JDK, JRE and JVM? 
Java :: java gmt zoneid 
Java :: difference between maven plugin and dependency 
Java :: draw triangle in android studio xml 
Java :: Sample HashMap 
Java :: border swing 
Java :: how to make jenkins pipeline choose specific java version 
Java :: android java turn off night mode 
Java :: java noverify 
Java :: pythagoras method in java 
Java :: HashMap to store key and value pair. 
Java :: java long data type 
Java :: spring-boot-maven-plugin not found 
Java :: java swing draw centered text 
Java :: mongodb spring boot index usage 0 
Java :: Spigot how to get block player is looking at 
Java :: java developer interview questions 
Java :: how to print each element of an arraylist on a new line in java 
Java :: find first and last position of element in sorted array 
Java :: access array elements java 
Java :: next line java does not take input 
Java :: switch expression java 
Java :: java to kotlin converter android studio 
Java :: java byte data type 
Java :: private method in interface java 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =