Java Zip Directory zipDirectory(File dir, File file)

Here you can find the source of zipDirectory(File dir, File file)

Description

zip Directory

License

Open Source License

Declaration

public static void zipDirectory(File dir, File file) throws FileNotFoundException, IOException 

Method Source Code

//package com.java2s;
/**//from   ww  w .j a va 2  s .c  om
 * This file is part of VisiCut.
 * Copyright (C) 2011 - 2013 Thomas Oster <thomas.oster@rwth-aachen.de>
 * RWTH Aachen University - 52062 Aachen, Germany
 *
 *     VisiCut is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     VisiCut is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 *
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with VisiCut.  If not, see <http://www.gnu.org/licenses/>.
 **/

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    public static void zipDirectory(File dir, File file) throws FileNotFoundException, IOException {
        // Create the ZIP file
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file));
        addDirectoryToZip(out, dir, "");
        out.close();
    }

    private static void addDirectoryToZip(ZipOutputStream out, File dir, String prefix) throws IOException {
        FileInputStream in;
        byte[] buf = new byte[1024];
        int len;
        if (dir.isDirectory()) {
            for (File f : dir.listFiles()) {
                if (f.isDirectory()) {
                    addDirectoryToZip(out, f, ("".equals(prefix) ? "" : prefix + "/") + f.getName());
                } else if (f.isFile()) {
                    out.putNextEntry(new ZipEntry(("".equals(prefix) ? "" : prefix + "/") + f.getName()));
                    in = new FileInputStream(f);
                    // Transfer bytes from the file to the ZIP file
                    while ((len = in.read(buf)) > 0) {
                        out.write(buf, 0, len);
                    }
                    in.close();
                    // Complete the entry
                    out.closeEntry();
                }
            }
        }
    }
}

Related

  1. zip(File directory, File zipFile)
  2. zip(File directory, File zipFile)
  3. zipDirectories(List directories, File baseDirectory, File zipFile)
  4. zipDirectory(File baseDirectory, File output)
  5. zipDirectory(File dir, boolean includeDirInZip, boolean includeHidden)
  6. zipDirectory(File dir, File zipFile)
  7. zipDirectory(File dir, String base, ZipOutputStream zout)
  8. zipDirectory(File dir, String zipDirName)
  9. zipDirectory(File directory, File zip)