Java Zip Folder zipDir(String origDir, File dirObj, ZipOutputStream zos)

Here you can find the source of zipDir(String origDir, File dirObj, ZipOutputStream zos)

Description

zip Dir

License

Apache License

Declaration

private static void zipDir(String origDir, File dirObj,
            ZipOutputStream zos) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2014 Umesh Kanitkar//  www  .jav  a  2  s  . co m
 * 
 * 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.
 ******************************************************************************/

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

import java.io.IOException;

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

public class Main {
    private static void zipDir(String origDir, File dirObj,
            ZipOutputStream zos) throws IOException {
        File[] files = dirObj.listFiles();
        byte[] tmpBuf = new byte[1024];

        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                zipDir(origDir, files[i], zos);
                continue;
            }
            String wAbsolutePath = files[i].getAbsolutePath().substring(
                    origDir.length() + 1,
                    files[i].getAbsolutePath().length());
            FileInputStream in = new FileInputStream(
                    files[i].getAbsolutePath());
            zos.putNextEntry(new ZipEntry(wAbsolutePath));
            int len;
            while ((len = in.read(tmpBuf)) > 0) {
                zos.write(tmpBuf, 0, len);
            }
            zos.closeEntry();
            in.close();
        }
    }
}

Related

  1. zipDir(String dir2zip, FilenameFilter filter, java.util.zip.ZipOutputStream zos, int bufferSize)
  2. zipDir(String dir2zip, String zipFileName)
  3. zipDir(String dir2zip, ZipOutputStream zipOut, String zipFileName)
  4. zipDir(String dir2zip, ZipOutputStream zos)
  5. zipDir(String directory, String zipName)
  6. zipFolder(File root)
  7. zipFolder(File root)
  8. zipFolder(File source, File destination)
  9. zipFolder(File srcFolder, File destZipFile, boolean zipOnlySrcFolderContentsAndNotSrcFolder)