Java Zip Directory addDirectoryToZip(File directory, File base, String dirPrefix, ZipOutputStream zos)

Here you can find the source of addDirectoryToZip(File directory, File base, String dirPrefix, ZipOutputStream zos)

Description

Recursively inserts all files in a directory into a zipstream.

License

Apache License

Parameter

Parameter Description
directory the source directory
base optional parent directory that should serve as the entry root. Only path info after this base will be included as part of the entry name. By default, the directory parameter serves as root.
dirPrefix optional directory prefix to prepend onto each entry name.
zos the zip stream to add the files to.

Exception

Parameter Description
IOException an exception

Declaration

public static final void addDirectoryToZip(File directory, File base, String dirPrefix, ZipOutputStream zos)
        throws IOException 

Method Source Code

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

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 {
    /**//  w  ww  . j  a v a 2s .c o  m
     * Recursively inserts all files in a directory into a zipstream.
     * 
     * @param directory
     *            the source directory
     * @param base
     *            optional parent directory that should serve as the entry root.
     *            Only path info after this base will be included as part of the
     *            entry name. By default, the directory parameter serves as
     *            root.
     * @param dirPrefix
     *            optional directory prefix to prepend onto each entry name.
     * @param zos
     *            the zip stream to add the files to.
     * @throws IOException
     */
    public static final void addDirectoryToZip(File directory, File base, String dirPrefix, ZipOutputStream zos)
            throws IOException {
        if (base == null)
            base = directory;
        if (dirPrefix == null)
            dirPrefix = "";

        //add an entry for the directory itself
        if (!base.equals(directory) && directory.list().length == 0) {
            String dirEntryPath = dirPrefix
                    + directory.getPath().substring(base.getPath().length() + 1).replace('\\', '/');
            ZipEntry dirEntry = new ZipEntry(dirEntryPath.endsWith("/") ? dirEntryPath : dirEntryPath + "/");
            zos.putNextEntry(dirEntry);
        }

        File[] files = directory.listFiles();
        byte[] buffer = new byte[8192];
        int read = 0;
        for (int i = 0, n = files.length; i < n; i++) {
            if (!files[i].isHidden()) {
                if (files[i].isDirectory()) {
                    addDirectoryToZip(files[i], base, dirPrefix, zos);
                } else {
                    FileInputStream in = new FileInputStream(files[i]);
                    ZipEntry entry = new ZipEntry(dirPrefix
                            + files[i].getPath().substring(base.getPath().length() + 1).replace('\\', '/'));
                    zos.putNextEntry(entry);
                    while (-1 != (read = in.read(buffer))) {
                        zos.write(buffer, 0, read);
                    }
                    in.close();
                }
            }
        }
    }
}

Related

  1. addDirectoryToZip(ZipOutputStream out, File dir, String prefix)
  2. addDirectoryToZip(ZipOutputStream zipOutputStream, File dirToZip, String basePath, File fileToExclude)
  3. addDirectoryToZip(ZipOutputStream zipOutputStream, String dirName)
  4. addDirToArchive(ZipOutputStream zop, File srcFile)