Java Zip Folder zipDir(File dir, String relativePath, ZipOutputStream zos)

Here you can find the source of zipDir(File dir, String relativePath, ZipOutputStream zos)

Description

Zips a local directory, recursively, into a ZIP stream.

License

Apache License

Parameter

Parameter Description
dir directory to ZIP.
relativePath basePath in the ZIP for the files, normally "/".
zos the ZIP output stream to ZIP the directory.

Exception

Parameter Description

Declaration

public static void zipDir(File dir, String relativePath,
        ZipOutputStream zos) throws IOException 

Method Source Code

//package com.java2s;
/**//from  w w w .  ja v  a2 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.IOException;
import java.io.InputStream;

import java.io.File;
import java.io.FileInputStream;

import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;

public class Main {
    /**
     * Zips a local directory, recursively, into a ZIP stream.
     *
     * @param dir directory to ZIP.
     * @param relativePath basePath in the ZIP for the files, normally "/".
     * @param zos the ZIP output stream to ZIP the directory.
     * @throws java.io.IOException thrown if the directory could not be zipped.
     */
    public static void zipDir(File dir, String relativePath,
            ZipOutputStream zos) throws IOException {
        zipDir(dir, relativePath, zos, true);
        zos.close();
    }

    private static void zipDir(File dir, String relativePath,
            ZipOutputStream zos, boolean start) throws IOException {
        String[] dirList = dir.list();
        for (String aDirList : dirList) {
            File f = new File(dir, aDirList);
            if (!f.isHidden()) {
                if (f.isDirectory()) {
                    if (!start) {
                        ZipEntry dirEntry = new ZipEntry(relativePath
                                + f.getName() + "/");
                        zos.putNextEntry(dirEntry);
                        zos.closeEntry();
                    }
                    String filePath = f.getPath();
                    File file = new File(filePath);
                    zipDir(file, relativePath + f.getName() + "/", zos,
                            false);
                } else {
                    ZipEntry anEntry = new ZipEntry(relativePath
                            + f.getName());
                    zos.putNextEntry(anEntry);
                    InputStream is = new FileInputStream(f);
                    byte[] arr = new byte[4096];
                    int read = is.read(arr);
                    while (read > -1) {
                        zos.write(arr, 0, read);
                        read = is.read(arr);
                    }
                    is.close();
                    zos.closeEntry();
                }
            }
        }
    }
}

Related

  1. doZip(String inFilePath, String outFilePath)
  2. doZipFile(ZipOutputStream zipOut, File file, String dirPath)
  3. zip(File rootDir, String zipPath)
  4. zip(File rootDir, String zipPath)
  5. zipDir(File dir, String classPackage, String zipFile)
  6. zipDir(File dir, String relativePath, ZipOutputStream zos, boolean start)
  7. zipDir(File dir, ZipOutputStream zos, String prefix)
  8. zipDir(File directory, ZipOutputStream zos, String path, Set exclusions)
  9. zipDir(File sourceDir, File destFile)