Java Zip Folder addFolderToZip(String pathInsideZip, final File folderToZip, final ZipOutputStream outZip)

Here you can find the source of addFolderToZip(String pathInsideZip, final File folderToZip, final ZipOutputStream outZip)

Description

add Folder To Zip

License

Apache License

Declaration

private static void addFolderToZip(String pathInsideZip, final File folderToZip, final ZipOutputStream outZip)
            throws IOException 

Method Source Code

//package com.java2s;
/*#######################################################
 *
 *   Maintained by Gregor Santner, 2017-
 *   https://gsantner.net///from w  ww.j  ava 2 s  . co  m
 *
 *   License: Apache 2.0
 *  https://github.com/gsantner/opoc/#licensing
 *  https://www.apache.org/licenses/LICENSE-2.0
 *
#########################################################*/

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 final int BUFFER_SIZE = 4096;

    private static void addFolderToZip(String pathInsideZip, final File folderToZip, final ZipOutputStream outZip)
            throws IOException {
        pathInsideZip = pathInsideZip.isEmpty() ? folderToZip.getName()
                : pathInsideZip + "/" + folderToZip.getName();

        File[] files = folderToZip.listFiles();
        if (files != null) {
            for (File file : files)
                addFileToZip(pathInsideZip, file, outZip);
        }
    }

    private static void addFileToZip(final String pathInsideZip, final File fileToZip, final ZipOutputStream outZip)
            throws IOException {
        if (fileToZip.isDirectory()) {
            addFolderToZip(pathInsideZip, fileToZip, outZip);
        } else {
            FileInputStream in = null;
            try {
                in = new FileInputStream(fileToZip);
                outZip.putNextEntry(new ZipEntry(pathInsideZip + "/" + fileToZip.getName()));

                int count;
                byte[] buffer = new byte[BUFFER_SIZE];
                while ((count = in.read(buffer)) > 0)
                    outZip.write(buffer, 0, count);

            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException ignored) {
                    }
                }
            }
        }
    }
}

Related

  1. addFolderToZip(String path, String srcFolder, ZipOutputStream zip)
  2. addFolderToZip(String path, String srcFolder, ZipOutputStream zip)
  3. addFolderToZip(String path, String srcFolder, ZipOutputStream zip)
  4. addFolderToZip(String path, String srcFolder, ZipOutputStream zip, boolean addFolder)
  5. addFolderToZip(String path, String srcFolder, ZipOutputStream zip, boolean includeFullPath)
  6. doZip(File fileIn, String fileOut)
  7. doZip(Map zipStructure, File zipFile)
  8. doZip(Properties properties, String name, File f)
  9. doZip(String baseDir, String fileName)