Java Zip Directory zip(File dir, ZipOutputStream out, String prefix)

Here you can find the source of zip(File dir, ZipOutputStream out, String prefix)

Description

zip

License

Apache License

Declaration

private static void zip(File dir, ZipOutputStream out, String prefix) throws Exception 

Method Source Code

//package com.java2s;
/**************************************************************
 * // w  ww.j  a  v a  2s  . c  o 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.BufferedInputStream;
import java.io.BufferedOutputStream;

import java.io.File;
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 {
    private static void zip(File dir, ZipOutputStream out, String prefix) throws Exception {
        File[] files = dir.listFiles();
        for (File f : files) {
            if (f.isFile()) {
                BufferedInputStream bis = null;
                try {
                    bis = new BufferedInputStream(new FileInputStream(f));
                    ZipEntry entry = new ZipEntry(prefix + f.getName());
                    out.putNextEntry(entry);
                    int count;
                    byte data[] = new byte[8192];
                    while ((count = bis.read(data)) != -1) {
                        out.write(data, 0, count);
                    }
                    bis.close();
                } finally {
                    if (bis != null)
                        bis.close();
                }

            } else {
                zip(f, out, prefix + f.getName() + "/");
            }
        }
    }

    public static void zip(File workingDir, File zipFile) {
        zip(workingDir, zipFile, null);
    }

    public static void zip(File workingDir, File zipFile, String prefix) {
        if (!workingDir.isDirectory())
            return;

        if (prefix == null)
            prefix = "";

        ZipOutputStream out = null;
        try {
            out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
            zip(workingDir, out, prefix);
        } catch (Exception e) {

        } finally {
            if (out != null)
                try {
                    out.close();
                } catch (IOException e) {
                }
        }

    }
}

Related

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