Java Zip Files addFileToZip(final String pathInsideZip, final File fileToZip, final ZipOutputStream outZip)

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

Description

add File To Zip

License

Apache License

Declaration

private static void addFileToZip(final String pathInsideZip, final File fileToZip, final ZipOutputStream outZip)
            throws IOException 

Method Source Code

//package com.java2s;
/*#######################################################
 *
 *   Maintained by Gregor Santner, 2017-
 *   https://gsantner.net//*w ww. j a  v  a  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 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) {
                    }
                }
            }
        }
    }

    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);
        }
    }
}

Related

  1. addFileToZip(File file, ZipOutputStream zos)
  2. addFileToZip(File in, File parent, ZipOutputStream out)
  3. addFileToZip(File root, File file, ZipOutputStream zos)
  4. addFileToZip(final File file, final String zipName, final ZipOutputStream zipout)
  5. addFileToZip(final String path, final String srcFile, final ZipOutputStream zip, boolean flag)
  6. addFileToZip(int skipprefix, File file, ZipOutputStream zipper)
  7. addFileToZip(String path, File srcFile, ZipOutputStream zip, boolean flag)
  8. addFileToZip(String path, File srcFile, ZipOutputStream zip, String destZipFile)
  9. addFileToZip(String path, String srcFile, ZipOutputStream zip)