All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.arch.util.ZipUtils Maven / Gradle / Ivy

There is a newer version: 18.12.0
Show newest version
package com.arch.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public final class ZipUtils {

    private static final int BYTE_SIZE = 1024;

    private ZipUtils() {

    }

    public static void zipDirectory(String sourceDirPath, String zipFilePath) throws IOException {
        Path p = Files.createFile(Paths.get(zipFilePath));
        try (ZipOutputStream zs = new ZipOutputStream(Files.newOutputStream(p))) {
            Path pp = Paths.get(sourceDirPath);
            Files.walk(pp)
                    .filter(path -> !Files.isDirectory(path))
                    .forEach(path -> {
                        ZipEntry zipEntry = new ZipEntry(pp.relativize(path).toString());
                        try {
                            zs.putNextEntry(zipEntry);
                            Files.copy(path, zs);
                            zs.closeEntry();
                        } catch (IOException e) {
                            System.err.println(e);
                        }
                    });
        }
    }

//    public static void unzipDirectory(File diretory, File file) throws IOException {
//        InputStream in = new BufferedInputStream(new FileInputStream(file));
//        ZipInputStream zin = new ZipInputStream(in);
//        ZipEntry zipEntry;
//
//        while((zipEntry = zin.getNextEntry()) != null) {
//            System.out.println("unzipping " + zipEntry.getName());
//            FileOutputStream out = new FileOutputStream(diretory.getPath() + File.separator + zipEntry.getName());
//            byte [] b = new byte[512];
//            int len = 0;
//
//            while ( (len=zin.read(b))!= -1 ) {
//                out.write(b,0,len);
//            }
//            out.close();
//        }
//
//        zin.close();
//    }


    public static void compress(File arquivoZip, File arquivo) {
        LogUtils.start();

        byte[] buffer = new byte[BYTE_SIZE];

        try (FileOutputStream fos = new FileOutputStream(arquivoZip);
             ZipOutputStream zos = new ZipOutputStream(fos);
             FileInputStream in = new FileInputStream(arquivo)) {

            ZipEntry ze = new ZipEntry(arquivo.getName());
            zos.putNextEntry(ze);

            int len;
            while ((len = in.read(buffer)) > 0) {
                zos.write(buffer, 0, len);
            }

            in.close();
            zos.closeEntry();

            // remember close it
            zos.close();

            LogUtils.end();

        } catch (IOException ex) {
            LogUtils.generate(ex);
        }
    }

    public static void decompress(File arquivoZip, File diretory) throws IOException {
        LogUtils.start();

        try (ZipFile zipFile = new ZipFile(arquivoZip)) {
            Enumeration entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) entries.nextElement();

                String diretoryFile = diretory.getPath() + File.separator + entry.getName().substring(0, entry.getName().lastIndexOf(File.separator));

                new File(diretoryFile).mkdirs();

                if (entry.isDirectory()) {
                    LogUtils.generate("Descompactando diretório: " + entry.getName());
                    (new File(diretory.getAbsoluteFile() + "/" + entry.getName())).mkdir();
                    continue;
                }

                LogUtils.generate("Descompactando arquivo:" + diretory.getAbsoluteFile() + "/" + entry.getName());

                try (FileOutputStream out = new FileOutputStream(diretory.getAbsoluteFile() + "/" + entry.getName())) {
                    copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(out));
                }
            }

            LogUtils.end();
        }
    }

    private static void copyInputStream(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[BYTE_SIZE];
        int len;

        while ((len = in.read(buffer)) >= 0) {
            out.write(buffer, 0, len);
        }

        in.close();
        out.close();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy