Java Zip Directory zipDirectory(File directory, File zipFile)

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

Description

Zips a directory to a specified file.

License

Apache License

Parameter

Parameter Description
directory The directory to zip.
zipFile The file to zip to (created if it doesn't exist; otherwise overwritten). The file is assumed to have the appropriate extension (e.g., ".zip").

Exception

Parameter Description
IOException an exception

Declaration

public static void zipDirectory(File directory, File zipFile) throws IOException 

Method Source Code

//package com.java2s;
/**//from   w w w.  j a  va2  s. com
   Copyright 2013 Smartsheet.com
    
   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.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {
    private static final int ZIP_BUFFER_SIZE = 64 * 1024;

    /**
     * Zips a directory to a specified file.
     *
     * @param directory
     *          The directory to zip.
     *
     * @param zipFile
     *          The file to zip to (created if it doesn't exist; otherwise
     *          overwritten). The file is assumed to have the appropriate
     *          extension (e.g., ".zip").
     *
     * @throws IOException
     */
    public static void zipDirectory(File directory, File zipFile) throws IOException {
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
        try {
            zipDirectory(directory /*root*/, directory, zos);
        } finally {
            zos.close();
        }
    }

    private static void zipDirectory(File root, File directory, ZipOutputStream zos) throws IOException {
        for (File item : directory.listFiles()) {
            if (item.isDirectory())
                zipDirectory(root, item, zos);

            else {
                byte[] readBuffer = new byte[ZIP_BUFFER_SIZE];
                InputStream fis = new FileInputStream(item);
                try {
                    String path = item.getAbsolutePath().substring(root.getAbsolutePath().length() + 1);
                    ZipEntry zipEntry = new ZipEntry(path);
                    zos.putNextEntry(zipEntry);
                    int bytesRead;
                    while ((bytesRead = fis.read(readBuffer)) != -1) {
                        zos.write(readBuffer, 0, bytesRead);
                    }
                } finally {
                    fis.close();
                }
            }
        }
    }
}

Related

  1. zipDirectory(File dir, String base, ZipOutputStream zout)
  2. zipDirectory(File dir, String zipDirName)
  3. zipDirectory(File directory, File zip)
  4. zipDirectory(File directory, File zip)
  5. zipDirectory(File directory, File zipFile)
  6. zipDirectory(File directory, File zipFile, Pattern exclusion)
  7. zipDirectory(File directory, ZipOutputStream zipout, final FilenameFilter filter)
  8. zipDirectory(File dirToZip, File outputDir, String fileName)
  9. zipDirectory(File folder, File outputFile)