Java Zip Directory zipDirectory(String dirName, String zipFileName)

Here you can find the source of zipDirectory(String dirName, String zipFileName)

Description

Zips a directory into a zip-archive

License

Open Source License

Parameter

Parameter Description
dirName directory to zip
zipFileName name of the resulting zip-file

Declaration

public static void zipDirectory(String dirName, String zipFileName) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Caleydo - Visualization for Molecular Biology - http://caleydo.org
 * Copyright (c) The Caleydo Team. All rights reserved.
 * Licensed under the new BSD license, available at http://caleydo.org/license
 ******************************************************************************/

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    /**//w  ww .j  a  va2  s .co m
     * Zips a directory into a zip-archive
     *
     * @param dirName
     *            directory to zip
     * @param zipFileName
     *            name of the resulting zip-file
     */
    public static void zipDirectory(String dirName, String zipFileName) {
        try {
            ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFileName));
            zipDirectory(dirName, zos, "");
            zos.close();
        } catch (Exception ex) {
            throw new RuntimeException("Error saving project files (zip)", ex);
        }
    }

    /**
     * Browses through a given directory and writes the files to a given {@link ZipOutputStream}
     *
     * @param dir2zip
     *            directory to zip
     * @param zos
     *            stream to write to
     * @param base
     *            base path
     */
    public static void zipDirectory(String dir2zip, ZipOutputStream zos, String basePath) {
        try {
            // create a new File object based on the directory we have to zip
            File zipDir = new File(dir2zip);

            // 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()) {
                    String path = basePath + f.getName() + "/";
                    zos.putNextEntry(new ZipEntry(path));

                    zipDirectory(f.getPath(), zos, path);

                    zos.closeEntry();
                } else {
                    // if we reached here, the File object f was not a directory
                    // create a FileInputStream on top of f
                    FileInputStream fis = new FileInputStream(f);
                    zos.putNextEntry(new ZipEntry(basePath + f.getName()));

                    while ((bytesIn = fis.read(readBuffer)) != -1) {
                        zos.write(readBuffer, 0, bytesIn);
                    }

                    zos.closeEntry();
                    fis.close();
                }
            }
        } catch (Exception ex) {
            throw new RuntimeException("Error saving project files (zip)", ex);
        }
    }
}

Related

  1. zipDirectory(final String sourceFolder, final String targetFolder, final String zipExtension)
  2. zipDirectory(String dbDumpPath)
  3. zipDirectory(String dir2zip, ZipOutputStream zos, String zipPath)
  4. zipDirectory(String directoryName, int iBaseFolderLength, ZipOutputStream zos, CRC32 crc)
  5. zipDirectory(String directoryName, String targetName)
  6. zipDirectory(ZipOutputStream out, String stripPath, File dir, char pathSeparator)
  7. zipDirectoryEntry(ZipOutputStream output, IPath entry, long time, Set directoryEntries)
  8. zipDirectoryRecursive(File directory, String zipPath, ZipOutputStream zos, ArrayList avoidingFiles)
  9. zipSubDirectory(String basePath, File dir, ZipOutputStream zout)