Java Zip Folder zipDir(File sourceDir, File destFile)

Here you can find the source of zipDir(File sourceDir, File destFile)

Description

zip Dir

License

Apache License

Declaration

public static void zipDir(File sourceDir, File destFile) throws IOException 

Method Source Code

//package com.java2s;
/*/*from   w w w .  j  av a  2s.  com*/
 * Copyright 2009-2012 by The Regents of the University of California
 * 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 from
 *
 *     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.FileFilter;
import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    public static void zipDir(File sourceDir, File destFile) throws IOException {
        FileOutputStream fos = new FileOutputStream(destFile);
        ZipOutputStream zos = new ZipOutputStream(fos);
        zipDir(sourceDir, destFile, zos);
        zos.close();
    }

    private static void zipDir(File sourceDir, final File destFile, ZipOutputStream zos) throws IOException {
        File[] dirList = sourceDir.listFiles(new FileFilter() {
            public boolean accept(File f) {
                return !f.getName().endsWith(destFile.getName());
            }
        });
        for (int i = 0; i < dirList.length; i++) {
            File f = dirList[i];
            if (f.isDirectory()) {
                zipDir(f, destFile, zos);
            } else {
                int bytesIn = 0;
                byte[] readBuffer = new byte[2156];
                FileInputStream fis = new FileInputStream(f);
                ZipEntry entry = new ZipEntry(sourceDir.getName() + File.separator + f.getName());
                zos.putNextEntry(entry);
                while ((bytesIn = fis.read(readBuffer)) != -1) {
                    zos.write(readBuffer, 0, bytesIn);
                }
                fis.close();
            }
        }
    }
}

Related

  1. zipDir(File dir, String classPackage, String zipFile)
  2. zipDir(File dir, String relativePath, ZipOutputStream zos)
  3. zipDir(File dir, String relativePath, ZipOutputStream zos, boolean start)
  4. zipDir(File dir, ZipOutputStream zos, String prefix)
  5. zipDir(File directory, ZipOutputStream zos, String path, Set exclusions)
  6. zipDir(File zipDir, ZipOutputStream zos, boolean recurse)
  7. zipDir(File zipDir, ZipOutputStream zos, String archiveSourceDir)
  8. zipDir(File zipDir, ZipOutputStream zos, String basePath)
  9. zipDir(File zipDir, ZipOutputStream zos, String name)