Java Zip Folder zipDir(String directory, String zipName)

Here you can find the source of zipDir(String directory, String zipName)

Description

Zip up a directory

License

Apache License

Parameter

Parameter Description
directory a parameter
zipName a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void zipDir(String directory, String zipName) throws IOException 

Method Source Code


//package com.java2s;
/*//  w w w. j  ava2  s .  c  o  m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * (modified version)
 */

import java.io.*;

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

public class Main {
    public final static String FILE_SEPARATOR = System.getProperty("file.separator");

    /**
     * Zip up a directory
     *
     * @param directory
     * @param zipName
     * @throws IOException
     */
    public static void zipDir(String directory, String zipName) throws IOException {
        // Make sure name is correct.
        if (!zipName.endsWith(".zip")) {
            zipName += ".zip";
        }
        // create a ZipOutputStream to zip the data to
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipName));
        zipDir(directory, zos, "");
        // close the stream
        closeQuietly(zos);
    }

    /**
     * Zip up a directory path
     *
     * @param directory
     * @param zos
     * @param path
     * @throws IOException
     */
    private static void zipDir(String directory, ZipOutputStream zos, String path) throws IOException {
        File zipDir = new File(directory);
        // get a listing of the directory content
        String[] dirList = zipDir.list();
        byte[] readBuffer = new byte[2156];
        int bytesIn = 0;
        // loop through dirList, and zip the files
        for (int i = 0; i < dirList.length; ++i) {
            File f = new File(zipDir, dirList[i]);
            if (f.isDirectory()) {
                zipDir(f.getPath(), zos, path.concat(f.getName()).concat(FILE_SEPARATOR));
                continue;
            }
            FileInputStream fis = new FileInputStream(f);
            try {
                zos.putNextEntry(new ZipEntry(path.concat(f.getName())));
                bytesIn = fis.read(readBuffer);
                while (bytesIn != -1) {
                    zos.write(readBuffer, 0, bytesIn);
                    bytesIn = fis.read(readBuffer);
                }
            } finally {
                closeQuietly(fis);
            }
        }

    }

    /**
     * Unconditionally close a
     * <code>Closeable</code>. <p> Equivalent to {@link Closeable#close()},
     * except any exceptions will be ignored. This is typically used in finally
     * blocks. <p> Example code:
     * <pre>
     *   Closeable closeable = null;
     *   try {
     *       closeable = new FileReader("foo.txt");
     *       // process closeable
     *       closeable.close();
     *   } catch (Exception e) {
     *       // error handling
     *   } finally {
     *       IOUtils.closeQuietly(closeable);
     *   }
     * </pre>
     *
     * @param closeable the object to close, may be null or already closed
     * @since Commons IO 2.0
     */
    private static void closeQuietly(Closeable closeable) {
        try {
            if (closeable != null) {
                closeable.close();
            }
        } catch (IOException ioe) {
            // ignore
        }
    }
}

Related

  1. zipDir(String dir, ZipOutputStream zos)
  2. zipDir(String dir2zip, FilenameFilter filter, java.util.zip.ZipOutputStream zos, int bufferSize)
  3. zipDir(String dir2zip, String zipFileName)
  4. zipDir(String dir2zip, ZipOutputStream zipOut, String zipFileName)
  5. zipDir(String dir2zip, ZipOutputStream zos)
  6. zipDir(String origDir, File dirObj, ZipOutputStream zos)
  7. zipFolder(File root)
  8. zipFolder(File root)
  9. zipFolder(File source, File destination)