Java Zip Directory zipDirectory(String directoryName, String targetName)

Here you can find the source of zipDirectory(String directoryName, String targetName)

Description

zip Directory

License

Apache License

Declaration

public static void zipDirectory(String directoryName, String targetName) 

Method Source Code

//package com.java2s;
/*/*from   w  w w .j  a  va  2  s . co  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.
 */

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 {
    public static void zipDirectory(String directoryName, String targetName) {
        try {
            ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(targetName + ".zip"));
            zipDir(directoryName, zos);
            zos.close();
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
    }

    private static void zipDir(String dir2zip, ZipOutputStream zos) {
        try {
            File zipDir = new File(dir2zip);

            String[] dirList = zipDir.list();
            byte[] readBuffer = new byte[2156];
            int bytesIn = 0;
            for (int i = 0; i < dirList.length; i++) {
                File f = new File(zipDir, dirList[i]);
                if (f.isDirectory()) {
                    zipDir(f.getPath(), zos);
                    continue;
                }
                FileInputStream fis = new FileInputStream(f);
                ZipEntry anEntry = new ZipEntry(f.getPath());
                zos.putNextEntry(anEntry);
                while ((bytesIn = fis.read(readBuffer)) != -1)
                    zos.write(readBuffer, 0, bytesIn);
                fis.close();
            }
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
    }
}

Related

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