Java Zip File zip(File[] sourceFiles, File destFile, boolean deleteOriginalFiles)

Here you can find the source of zip(File[] sourceFiles, File destFile, boolean deleteOriginalFiles)

Description

zip

License

Open Source License

Declaration

public static void zip(File[] sourceFiles, File destFile, boolean deleteOriginalFiles) throws IOException 

Method Source Code


//package com.java2s;
/*//w w w. j  av  a2  s .co m
 * Copyright (c) 2002-2006 The European Bioinformatics Institute, and others.
 * All rights reserved. Please see the file LICENSE
 * in the root directory of this distribution.
 */

import java.io.*;

import java.util.zip.*;

public class Main {
    public static void zip(File[] sourceFiles, File destFile, boolean deleteOriginalFiles) throws IOException {
        zip(sourceFiles, destFile, deleteOriginalFiles, false);

    }

    /**
     * Compresses a file (or directory) using GZIP
     *
     * @param sourceFiles         the files to include in the zip
     * @param destFile the zipped file
     * @param deleteOriginalFiles if true, the original file is deleted and only the gzipped file remains
     * @param includeFullPathName if true, then zip file is given full path name, if false, only the file name
     * @throws java.io.IOException thrown if there is a problem finding or writing the files
     */
    public static void zip(File[] sourceFiles, File destFile, boolean deleteOriginalFiles,
            boolean includeFullPathName) throws IOException {

        // Create a buffer for reading the files
        byte[] buf = new byte[1024];

        // Create the ZIP file
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(destFile));

        try {
            // Compress the files
            for (File sourceFile : sourceFiles) {
                if (sourceFile.isDirectory()) {
                    addFolderToZip("", sourceFile.getAbsolutePath(), out, includeFullPathName);
                } else {
                    FileInputStream in = new FileInputStream(sourceFile);

                    try {
                        // Add ZIP entry to output stream.
                        if (includeFullPathName) {
                            out.putNextEntry(new ZipEntry(sourceFile.toString()));
                        } else {
                            out.putNextEntry(new ZipEntry(sourceFile.getName()));
                        }

                        // Transfer bytes from the file to the ZIP file
                        int len;
                        while ((len = in.read(buf)) > 0) {
                            out.write(buf, 0, len);
                        }
                    } finally {
                        // Complete the entry
                        out.closeEntry();
                        in.close();
                    }
                }
            }

            if (deleteOriginalFiles) {
                for (File sourceFile : sourceFiles) {
                    sourceFile.delete();
                }
            }
        } finally {
            // Complete the ZIP file
            out.close();
        }
    }

    /**
     * Zip the subdirectory and exclude already zipped files
     * @param path
     * @param srcFolder
     * @param zip
     * @param includeFullPath
     * @throws java.io.IOException
     */
    static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip, boolean includeFullPath)
            throws IOException {
        File folder = new File(srcFolder);

        for (String fileName : folder.list()) {
            if (path.equals("") && !fileName.endsWith(".zip")) {
                if (includeFullPath) {
                    addFileToZip(folder.toString(), srcFolder + "/" + fileName, zip);
                } else {
                    addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip);
                }
            } else if (!fileName.endsWith(".zip")) {
                addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip);
            }
        }
    }

    static private void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws IOException {

        File folder = new File(srcFile);
        if (folder.isDirectory()) {
            addFolderToZip(path, srcFile, zip, false);
        } else {
            byte[] buf = new byte[1024];
            int len;
            FileInputStream in = new FileInputStream(srcFile);
            try {
                zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
                while ((len = in.read(buf)) > 0) {
                    zip.write(buf, 0, len);
                }
            } finally {
                in.close();
            }
        }
    }
}

Related

  1. zip(File srcDir, File zipFile)
  2. zip(File srcDirectory, File destFile)
  3. zip(File srcFile, File destFile, String archiveRoot)
  4. zip(File theFileToZip)
  5. zip(File toZip, File outFile)
  6. zip(final File dir, final File target)
  7. zip(final File tempLocation, final File targetZipFile)
  8. zip(final File zipFile, final File... files)
  9. zip(final File zipFile, final File[] files)