Java Zip Folder zipDir(final String dir2zip, final ZipOutputStream zos, final String root)

Here you can find the source of zipDir(final String dir2zip, final ZipOutputStream zos, final String root)

Description

Create a structured zip archive recursively.

License

Open Source License

Parameter

Parameter Description
dir2zip a parameter
zos a parameter
root a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void zipDir(final String dir2zip, final ZipOutputStream zos, final String root)
        throws IOException 

Method Source Code

//package com.java2s;
/**//from  w  w w.j  a v  a2 s  .  c  o  m
 * Copyright (C) 2015 BonitaSoft S.A.
 * BonitaSoft, 32 rue Gustave Eiffel - 38000 Grenoble
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation
 * version 2.1 of the License.
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
 * Floor, Boston, MA 02110-1301, USA.
 **/

import java.io.BufferedOutputStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.util.Scanner;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    private static final String LINE_SEPARATOR = System.getProperty("line.separator");
    private static final int BUFFER_SIZE = 100000;
    public static final String FILE_ENCODING = "UTF-8";

    /**
     * Create a structured zip archive recursively.
     * The string must be OS specific String to represent path.
     *
     * @param dir2zip
     * @param zos
     * @param root
     * @throws IOException
     */
    public static void zipDir(final String dir2zip, final ZipOutputStream zos, final String root)
            throws IOException {
        final File zipDir = new File(dir2zip);
        final byte[] readBuffer = new byte[BUFFER_SIZE];
        int bytesIn = 0;

        for (final String pathName : zipDir.list()) {
            final File file = new File(zipDir, pathName);
            final String path = file.getPath();
            if (file.isDirectory()) {
                zipDir(path, zos, root);
                continue;
            }

            try {
                final ZipEntry anEntry = new ZipEntry(path.substring(root.length() + 1, path.length())
                        .replace(String.valueOf(File.separatorChar), "/"));
                zos.putNextEntry(anEntry);
                bytesIn = +copyFileToZip(zos, readBuffer, file, bytesIn);
                zos.flush();
            } finally {
                zos.closeEntry();
            }
        }
    }

    private static int copyFileToZip(final ZipOutputStream zos, final byte[] readBuffer, final File file,
            final int bytesInOfZip) throws FileNotFoundException, IOException {
        final FileInputStream fis = new FileInputStream(file);
        int bytesIn = bytesInOfZip;
        try {
            while ((bytesIn = fis.read(readBuffer)) != -1) {
                zos.write(readBuffer, 0, bytesIn);
            }
        } finally {
            fis.close();
        }
        return bytesIn;
    }

    /**
     * Read the contents from the given FileInputStream. Return the result as a String.
     *
     * @param inputStream
     *        the stream to read from
     * @return the content read from the inputStream, as a String
     */
    public static String read(final InputStream inputStream) {
        if (inputStream == null) {
            throw new IllegalArgumentException("Input stream is null");
        }
        Scanner scanner = null;
        try {
            scanner = new Scanner(inputStream, FILE_ENCODING);
            return read(scanner);
        } finally {
            if (scanner != null) {
                scanner.close();
            }
        }
    }

    private static String read(final Scanner scanner) {
        final StringBuilder text = new StringBuilder();
        boolean isFirst = true;
        while (scanner.hasNextLine()) {
            if (isFirst) {
                text.append(scanner.nextLine());
            } else {
                text.append(LINE_SEPARATOR + scanner.nextLine());
            }
            isFirst = false;
        }
        return text.toString();
    }

    /**
     * Read the contents of the given file.
     * 
     * @param file
     */
    public static String read(final File file) throws IOException {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            return read(fileInputStream);
        } finally {
            if (fileInputStream != null) {
                fileInputStream.close();
            }
        }
    }

    public static void write(final File file, final byte[] content) throws FileNotFoundException, IOException {
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        try {
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(content);
            bos.flush();
        } finally {
            if (bos != null) {
                bos.close();
            }
            if (fos != null) {
                fos.close();
            }
        }
    }
}

Related

  1. zipDir(File zipDir, ZipOutputStream zos, String archiveSourceDir)
  2. zipDir(File zipDir, ZipOutputStream zos, String basePath)
  3. zipDir(File zipDir, ZipOutputStream zos, String name)
  4. zipDir(File zipFile, File dir, boolean includeRoot)
  5. zipDir(File zipFile, File dirObj)
  6. zipDir(String baseDir, String dir2zip, ZipOutputStream zos)
  7. zipDir(String dir, ZipOutputStream zos)
  8. zipDir(String dir2zip, FilenameFilter filter, java.util.zip.ZipOutputStream zos, int bufferSize)
  9. zipDir(String dir2zip, String zipFileName)