Java Zip Folder zipFolder(File root)

Here you can find the source of zipFolder(File root)

Description

zip Folder

License

Apache License

Declaration

public static File zipFolder(File root) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {
    public static File zipFolder(File root) throws IOException {

        if (root == null)
            return null;
        if (!root.isDirectory())
            return null;

        File out = File.createTempFile("SHA", null);

        ZipOutputStream os = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(out)));

        addFolder("", root, os);
        os.flush();//  w w w.j  a va  2  s. co  m
        os.close();

        return out;
    }

    public static File zipFolder(File root, Set<String> topLevelDirFilter) throws IOException {

        if (root == null)
            return null;
        if (!root.isDirectory())
            return null;

        File out = File.createTempFile("SHA", null);

        ZipOutputStream os = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(out)));
        if (topLevelDirFilter == null) {
            addFolder("", root, os);
        } else {
            addFolder("", root, os, topLevelDirFilter);
        }
        os.flush();
        os.close();

        return out;
    }

    private static void addFolder(String path, File folder, ZipOutputStream os, Set<String> topLevelDirFilter)
            throws IOException {
        for (File f : folder.listFiles()) {
            if (f.isDirectory()) {
                if (topLevelDirFilter != null && !topLevelDirFilter.contains(f.getName()))
                    continue;
                if (path.equals(""))
                    addFolder(f.getName(), f, os);
                else
                    addFolder(path + "/" + f.getName(), f, os);
            } else {
                byte[] buff = new byte[32 * 1024];

                BufferedInputStream is = new BufferedInputStream(new FileInputStream(f));
                try {
                    os.putNextEntry(new ZipEntry(path + "/" + f.getName()));
                    int procitano = -1;
                    while ((procitano = is.read(buff)) != -1) {
                        os.write(buff, 0, procitano);
                    }
                    os.flush();
                    os.closeEntry();
                } finally {
                    try {
                        is.close();
                    } catch (Exception ignorable) {
                    }
                }
            }
        }
    }

    private static void addFolder(String path, File folder, ZipOutputStream os) throws IOException {
        for (File f : folder.listFiles()) {
            if (f.isDirectory()) {
                if (path.equals(""))
                    addFolder(f.getName(), f, os);
                else
                    addFolder(path + "/" + f.getName(), f, os);
            } else {
                byte[] buff = new byte[32 * 1024];

                BufferedInputStream is = new BufferedInputStream(new FileInputStream(f));
                try {
                    os.putNextEntry(new ZipEntry(path + "/" + f.getName()));
                    int procitano = -1;
                    while ((procitano = is.read(buff)) != -1) {
                        os.write(buff, 0, procitano);
                    }
                    os.flush();
                    os.closeEntry();
                } finally {
                    try {
                        is.close();
                    } catch (Exception ignorable) {
                    }
                }
            }
        }
    }
}

Related

  1. zipDir(String dir2zip, String zipFileName)
  2. zipDir(String dir2zip, ZipOutputStream zipOut, String zipFileName)
  3. zipDir(String dir2zip, ZipOutputStream zos)
  4. zipDir(String directory, String zipName)
  5. zipDir(String origDir, File dirObj, ZipOutputStream zos)
  6. zipFolder(File root)
  7. zipFolder(File source, File destination)
  8. zipFolder(File srcFolder, File destZipFile, boolean zipOnlySrcFolderContentsAndNotSrcFolder)
  9. zipFolder(File srcFolder, String destZipFile)