Java Zip Directory addDirectoryToZip(ZipOutputStream out, File dir, String prefix)

Here you can find the source of addDirectoryToZip(ZipOutputStream out, File dir, String prefix)

Description

add Directory To Zip

License

Open Source License

Declaration

private static void addDirectoryToZip(ZipOutputStream out, File dir, String prefix) throws IOException 

Method Source Code

//package com.java2s;
/**//  ww  w.ja  va 2 s . com
 * 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.IOException;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    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. addDirectoryToZip(File directory, File base, String dirPrefix, ZipOutputStream zos)
  2. addDirectoryToZip(ZipOutputStream zipOutputStream, File dirToZip, String basePath, File fileToExclude)
  3. addDirectoryToZip(ZipOutputStream zipOutputStream, String dirName)
  4. addDirToArchive(ZipOutputStream zop, File srcFile)
  5. addDirToArchive(ZipOutputStream zos, File srcFile)