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

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

Description

zip Dir

License

Apache License

Declaration

private static void zipDir(File dir, String relativePath, ZipOutputStream zos, boolean start)
            throws IOException 

Method Source Code

//package com.java2s;
/**/*  w w w  .  j av  a 2s . 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. See accompanying LICENSE file.
 */

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

import java.io.IOException;
import java.io.InputStream;

import java.util.jar.JarFile;

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

public class Main {
    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 {
                    String path = relativePath + f.getName();
                    if (!path.equals(JarFile.MANIFEST_NAME)) {
                        ZipEntry anEntry = new ZipEntry(path);
                        InputStream is = new FileInputStream(f);
                        copyToZipStream(is, anEntry, zos);
                    }
                }
            }
        }
    }

    private static void copyToZipStream(InputStream is, ZipEntry entry, ZipOutputStream zos) throws IOException {
        zos.putNextEntry(entry);
        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. doZipFile(ZipOutputStream zipOut, File file, String dirPath)
  2. zip(File rootDir, String zipPath)
  3. zip(File rootDir, String zipPath)
  4. zipDir(File dir, String classPackage, String zipFile)
  5. zipDir(File dir, String relativePath, ZipOutputStream zos)
  6. zipDir(File dir, ZipOutputStream zos, String prefix)
  7. zipDir(File directory, ZipOutputStream zos, String path, Set exclusions)
  8. zipDir(File sourceDir, File destFile)
  9. zipDir(File zipDir, ZipOutputStream zos, boolean recurse)