Java Zip File zip(final File tempLocation, final File targetZipFile)

Here you can find the source of zip(final File tempLocation, final File targetZipFile)

Description

Zip a list of file into one zip file.

License

Open Source License

Parameter

Parameter Description
targetZipFile The target location to be exported to.

Exception

Parameter Description
IOException This exception may be thrown when copying.

Declaration

public static void zip(final File tempLocation, final File targetZipFile) throws IOException 

Method Source Code


//package com.java2s;
/*/*from  w ww  .j av a2s  .  c  o  m*/
 *     Schematic Utilities, a program used to convert between schematic formats.
 *     Copyright (C) 2016  Jeff Chen
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Affero General Public License as published
 *     by the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program 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 Affero General Public License for more details.
 *
 *     You should have received a copy of the GNU Affero General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    /**
     * Zip a list of file into one zip file. Credits for this code go to <a
     * href="http://www.java2s.com/Code/Java/File-Input-Output/Zipalistoffileintoonezipfile.htm">Java2s</a>.
     *
     * @param targetZipFile The target location to be exported to.
     * @throws IOException This exception may be thrown when copying.
     */
    public static void zip(final File tempLocation, final File targetZipFile) throws IOException {

        File[] files = tempLocation.listFiles();
        int fileLength = files.length;

        try {

            FileOutputStream fos = new FileOutputStream(targetZipFile);
            ZipOutputStream zos = new ZipOutputStream(fos);

            byte[] buffer = new byte[128];

            for (int i = 0; i < fileLength; i++) {
                File currentFile = files[i];
                if (!currentFile.isDirectory()) {
                    ZipEntry entry = new ZipEntry(currentFile.getName());
                    FileInputStream fis = new FileInputStream(currentFile);
                    zos.putNextEntry(entry);
                    int read = 0;
                    while ((read = fis.read(buffer)) != -1) {
                        zos.write(buffer, 0, read);
                    }
                    zos.closeEntry();
                    fis.close();
                }
            }

            zos.close();
            fos.close();

        } catch (FileNotFoundException e) {
            System.out.println("File not found : " + e);
        }

    }
}

Related

  1. zip(File srcFile, File destFile, String archiveRoot)
  2. zip(File theFileToZip)
  3. zip(File toZip, File outFile)
  4. zip(File[] sourceFiles, File destFile, boolean deleteOriginalFiles)
  5. zip(final File dir, final File target)
  6. zip(final File zipFile, final File... files)
  7. zip(final File zipFile, final File[] files)
  8. zip(final File[] filesToZip, final File zipFile)
  9. zip(final Map files)