Java Zip Folder zipDir(File dir, String classPackage, String zipFile)

Here you can find the source of zipDir(File dir, String classPackage, String zipFile)

Description

zip Dir

License

Open Source License

Declaration

public static void zipDir(File dir, String classPackage, String zipFile)
            throws Exception 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2016 Chen Chao(cnfree2000@hotmail.com).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*  w  ww  .ja va2 s  . c o  m*/
 *  Chen Chao  - initial API and implementation
 *******************************************************************************/

import java.io.BufferedInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    public static void zipDir(File dir, String classPackage, String zipFile)
            throws Exception {
        File[] files = dir.listFiles();
        if (files != null) {
            ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(
                    zipFile));
            ZipEntry ze = null;
            byte[] buf = new byte[1024];
            int readLen = 0;
            for (int i = 0; i < files.length; i++) {
                File file = files[i];
                if (file.isDirectory())
                    continue;
                ze = new ZipEntry(
                        (classPackage.length() > 0 ? (classPackage + "/") : "") + file.getName()); //$NON-NLS-1$ //$NON-NLS-2$
                ze.setSize(file.length());
                ze.setTime(file.lastModified());
                zos.putNextEntry(ze);
                InputStream is = new BufferedInputStream(
                        new FileInputStream(file));
                while ((readLen = is.read(buf, 0, 1024)) != -1) {
                    zos.write(buf, 0, readLen);
                }
                is.close();
            }
            zos.close();
        }
    }
}

Related

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