Java Zip Folder zipDir(File zipFile, File dir, boolean includeRoot)

Here you can find the source of zipDir(File zipFile, File dir, boolean includeRoot)

Description

zip Dir

License

Open Source License

Declaration

public static void zipDir(File zipFile, File dir, boolean includeRoot) throws IOException 

Method Source Code

//package com.java2s;
/*//from  w ww. jav  a2 s.c  om
 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
 *
 * 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:
 *     bstefanescu
 */

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

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

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    public static void zipDir(File zipFile, File dir, boolean includeRoot) throws IOException {
        if (!dir.isDirectory()) {
            return;
        }
        ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(zipFile));
        try {
            if (includeRoot) {
                _zip(dir.getName(), dir, zip);
            } else {
                File[] files = dir.listFiles();
                if (files != null) {
                    for (File file : files) {
                        _zip(file.getName(), file, zip);
                    }
                }
            }
        } finally {
            zip.finish();
            zip.close();
        }
    }

    protected static void _zip(String entryName, File file, ZipOutputStream out) throws IOException {
        if (file.isDirectory()) {
            entryName += '/';
            ZipEntry zentry = new ZipEntry(entryName);
            out.putNextEntry(zentry);
            out.closeEntry();
            File[] files = file.listFiles();
            if (files != null) {
                for (int i = 0, len = files.length; i < len; i++) {
                    _zip(entryName + files[i].getName(), files[i], out);
                }
            }
        } else {
            InputStream in = null;
            try {
                in = new FileInputStream(file);
                ZipEntry zentry = new ZipEntry(entryName);
                out.putNextEntry(zentry);
                // Transfer bytes from the input stream to the ZIP file
                copy(in, out);
                out.closeEntry();
            } finally {
                if (in != null) {
                    in.close();
                }
            }
        }
    }

    public static void copy(InputStream in, OutputStream out) throws IOException {
        byte[] buf = new byte[1024 * 1024];
        int r = -1;
        while ((r = in.read(buf)) > -1) {
            if (r > 0) {
                out.write(buf, 0, r);
            }
        }
    }

    public static String read(InputStream in) throws IOException {
        StringBuilder sb = new StringBuilder();
        int size = in.available();
        if (size < 1 || size > 64 * 1024) {
            size = 64 * 1024;
        }
        byte[] buffer = new byte[size];
        try {
            int read;
            while ((read = in.read(buffer)) != -1) {
                sb.append(new String(buffer, 0, read));
            }
        } finally {
            in.close();
        }
        return sb.toString();
    }
}

Related

  1. zipDir(File sourceDir, File destFile)
  2. zipDir(File zipDir, ZipOutputStream zos, boolean recurse)
  3. zipDir(File zipDir, ZipOutputStream zos, String archiveSourceDir)
  4. zipDir(File zipDir, ZipOutputStream zos, String basePath)
  5. zipDir(File zipDir, ZipOutputStream zos, String name)
  6. zipDir(File zipFile, File dirObj)
  7. zipDir(final String dir2zip, final ZipOutputStream zos, final String root)
  8. zipDir(String baseDir, String dir2zip, ZipOutputStream zos)
  9. zipDir(String dir, ZipOutputStream zos)