Java Zip Directory zip(File dir2zip, File zipFile)

Here you can find the source of zip(File dir2zip, File zipFile)

Description

zip

License

Open Source License

Declaration

public static void zip(File dir2zip, File zipFile) throws Exception 

Method Source Code

//package com.java2s;
/*/*from w  ww. j  a va  2  s  . c om*/
 * Copyright (c) 2008-2016, GigaSpaces Technologies, Inc. All Rights Reserved.
 *
 * Licensed 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.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    final private static Logger logger = Logger
            .getLogger("com.gigaspaces.zip");

    public static void zip(File dir2zip, File zipFile) throws Exception {
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(
                zipFile));
        internalZipDir(dir2zip.getAbsolutePath(),
                dir2zip.getAbsolutePath(), zos);
        zos.close();
    }

    private static void internalZipDir(String baseDir2zip, String dir2zip,
            ZipOutputStream zos) throws Exception {
        //create a new File object based on the directory we have to zip
        File zipDir = new File(dir2zip);
        if (!zipDir.exists()) {
            throw new IllegalArgumentException("Directory to zip ["
                    + zipDir.getAbsolutePath() + "] does not exists");
        }
        if (!zipDir.isDirectory()) {
            throw new IllegalArgumentException("Directory to zip ["
                    + zipDir.getAbsolutePath() + "] is not a directory");
        }
        //get a listing of the directory content
        String[] dirList = zipDir.list();
        if (dirList == null) {
            return;
        }
        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()) {
                //if the File object is a directory, call this
                //function again to add its content recursively
                String filePath = f.getAbsolutePath();
                internalZipDir(baseDir2zip, filePath, zos);
                //loop again
                continue;
            }
            //if we reached here, the File object f was not a directory
            //create a FileInputStream on top of f
            FileInputStream fis = new FileInputStream(f);
            // create a new zip entry
            String path = f.getAbsolutePath().substring(
                    baseDir2zip.length() + 1);
            path = path.replace('\\', '/');
            ZipEntry anEntry = new ZipEntry(path);
            if (logger.isLoggable(Level.FINEST)) {
                logger.finest("Compressing zip entry [" + anEntry.getName()
                        + "] with size [" + anEntry.getSize() + "] at ["
                        + f.getAbsolutePath() + "]");
            }
            //place the zip entry in the ZipOutputStream object
            zos.putNextEntry(anEntry);
            //now write the content of the file to the ZipOutputStream
            while ((bytesIn = fis.read(readBuffer)) != -1) {
                zos.write(readBuffer, 0, bytesIn);
            }
            //close the Stream
            fis.close();
        }
    }
}

Related

  1. addDirToArchive(ZipOutputStream zop, File srcFile)
  2. addDirToArchive(ZipOutputStream zos, File srcFile)
  3. addDirToArchive(ZipOutputStream zos, String path, File initialDir)
  4. zip(File dir, File base, ZipOutputStream out)
  5. zip(File dir, ZipOutputStream out, String prefix)
  6. zip(File directory, File base, ZipOutputStream zos)
  7. zip(File directory, File base, ZipOutputStream zos, byte[] buffer)
  8. zip(File directory, File file)
  9. zip(File directory, File zipFile)