Java Zip Directory zipDirectory(File dir, File zipFile)

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

Description

Zip a directory and its subdirectories, preserving the name of the root directory (dir) in all paths in the zip file.

License

Open Source License

Parameter

Parameter Description
dir the directory to zip.
zipFile the output zip file.

Return

true if the operation succeeded completely; false otherwise.

Declaration

public static synchronized boolean zipDirectory(File dir, File zipFile) 

Method Source Code

//package com.java2s;
/*---------------------------------------------------------------
*  Copyright 2005 by the Radiological Society of North America
*
*  This source software is released under the terms of the
*  RSNA Public License (http://mirc.rsna.org/rsnapubliclicense)
*----------------------------------------------------------------*/

import java.io.File;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import java.io.OutputStream;

import java.io.Reader;

import java.io.Writer;

import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class Main {
    /**/*ww  w  .j  a  v  a  2 s  .com*/
     * Zip a directory and its subdirectories, preserving the name of
     * the root directory (dir) in all paths in the zip file.
     * @param dir the directory to zip.
     * @param zipFile the output zip file.
     * @return true if the operation succeeded completely; false otherwise.
     */
    public static synchronized boolean zipDirectory(File dir, File zipFile) {
        return zipDirectory(dir, zipFile, false);
    }

    /**
     * Zip a directory and its subdirectories.
     * @param dir the directory to zip.
     * @param zipFile the output zip file.
     * @param suppressRoot true if the path to the root directory (dir) is
     * to be suppressed; false if all paths in the zip file are to start
     * with the name of the root directory.
     * @return true if the operation succeeded completely; false otherwise.
     */
    public static synchronized boolean zipDirectory(File dir, File zipFile, boolean suppressRoot) {
        try {
            dir = dir.getCanonicalFile();

            int rootLength;
            if (suppressRoot) {
                //Get the length of the dir path
                rootLength = dir.getAbsolutePath().length();
            } else {
                //Get the length of the parent path
                File parent = dir.getParentFile();
                rootLength = parent.getAbsolutePath().length();
            }
            rootLength++; //allow for the slash that will appear in files that are zipped

            //Get the streams
            FileOutputStream fout = new FileOutputStream(zipFile);
            ZipOutputStream zout = new ZipOutputStream(fout);

            zipDirectory(zout, dir, rootLength);
            zout.close();
            return true;
        } catch (Exception ex) {
            return false;
        }
    }

    private static synchronized void zipDirectory(ZipOutputStream zout, File dir, int rootLength) throws Exception {
        if (dir.isDirectory()) {
            String name = dir.getAbsolutePath() + "/";
            if (name.length() > rootLength) {
                name = name.substring(rootLength);
                if (!name.endsWith("/"))
                    name += "/";
                ZipEntry ze = new ZipEntry(name);
                zout.putNextEntry(ze);
            }
            File[] files = dir.listFiles();
            for (File file : files) {
                if (file.isDirectory())
                    zipDirectory(zout, file, rootLength);
                else
                    zipFile(zout, file, rootLength);
            }
        }
    }

    /**
     * Close an InputStream and ignore Exceptions.
     * @param stream the stream to close.
     */
    public static void close(InputStream stream) {
        if (stream != null) {
            try {
                stream.close();
            } catch (Exception ignore) {
            }
        }
    }

    /**
     * Close an OutputStream and ignore Exceptions.
     * @param stream the stream to close.
     */
    public static void close(OutputStream stream) {
        if (stream != null) {
            try {
                stream.close();
            } catch (Exception ignore) {
            }
        }
    }

    /**
     * Close a Writer and ignore Exceptions.
     * @param writer the writer to close.
     */
    public static void close(Writer writer) {
        if (writer != null) {
            try {
                writer.close();
            } catch (Exception ignore) {
            }
        }
    }

    /**
     * Close a Reader and ignore Exceptions.
     * @param reader the reader to close.
     */
    public static void close(Reader reader) {
        if (reader != null) {
            try {
                reader.close();
            } catch (Exception ignore) {
            }
        }
    }

    /**
     * Close a JarFile and ignore Exceptions.
     * @param jarFile the JarFile to close.
     */
    public static void close(JarFile jarFile) {
        if (jarFile != null) {
            try {
                jarFile.close();
            } catch (Exception ignore) {
            }
        }
    }

    /**
     * Close a ZipFile and ignore Exceptions.
     * @param zipFile the zipFile to close.
     */
    public static void close(ZipFile zipFile) {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (Exception ignore) {
            }
        }
    }

    private static synchronized void zipFile(ZipOutputStream zout, File file, int rootLength) throws Exception {
        FileInputStream fin;
        ZipEntry ze;
        byte[] buffer = new byte[10000];
        int bytesread;
        String entryname = file.getAbsolutePath().substring(rootLength);
        entryname = entryname.replaceAll("\\\\", "/");
        ze = new ZipEntry(entryname);
        if (file.exists()) {
            fin = new FileInputStream(file);
            zout.putNextEntry(ze);
            while ((bytesread = fin.read(buffer)) > 0)
                zout.write(buffer, 0, bytesread);
            zout.closeEntry();
            fin.close();
        }
    }
}

Related

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