Java Zip Directory zipDirectory(String dbDumpPath)

Here you can find the source of zipDirectory(String dbDumpPath)

Description

zip Directory

License

Open Source License

Declaration

public static void zipDirectory(String dbDumpPath) throws IOException 

Method Source Code

//package com.java2s;
/**/*  w ww  . j a  v a 2  s  .co  m*/
 * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com
 *
 * The software in this package is published under the terms of the CPAL v1.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE.md file.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {
    public static void zipDirectory(String dbDumpPath) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream(dbDumpPath + ".zip");
        ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);

        File dumpDirectory = new File(dbDumpPath);

        addDirectory(zipOutputStream, dumpDirectory);

        zipOutputStream.close();

    }

    private static void addDirectory(ZipOutputStream zipOutputStream, File dumpDirectory) throws IOException {
        File[] files = dumpDirectory.listFiles();

        for (File file : files) {
            if (file.isDirectory()) {
                addDirectory(zipOutputStream, file);
                continue;
            }
            FileInputStream fileInputStream = new FileInputStream(file);
            try {
                byte[] buffer = new byte[1024];
                zipOutputStream.putNextEntry(new ZipEntry(file.getName()));

                int length;
                while ((length = fileInputStream.read(buffer)) > 0) {
                    zipOutputStream.write(buffer, 0, length);
                }
                zipOutputStream.closeEntry();
            } finally {
                fileInputStream.close();
            }
        }
    }
}

Related

  1. zipDirectory(File inputDirectory, File zipFile)
  2. zipDirectory(File root, File directory, ZipOutputStream zos)
  3. zipDirectory(File srcDir, File destFile)
  4. zipDirectory(File zipDir, ZipOutputStream zos)
  5. zipDirectory(final String sourceFolder, final String targetFolder, final String zipExtension)
  6. zipDirectory(String dir2zip, ZipOutputStream zos, String zipPath)
  7. zipDirectory(String directoryName, int iBaseFolderLength, ZipOutputStream zos, CRC32 crc)
  8. zipDirectory(String directoryName, String targetName)
  9. zipDirectory(String dirName, String zipFileName)