Java Zip File zip(File theFileToZip)

Here you can find the source of zip(File theFileToZip)

Description

zip

License

Open Source License

Parameter

Parameter Description
theFileToZip a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void zip(File theFileToZip) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) JavaPEG developers//  w w  w  . j  a va 2  s  .  c  o m
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

import java.io.*;
import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    /**
     * @param theFileToZip
     * @throws IOException
     */
    public static void zip(File theFileToZip) throws IOException {

        byte[] buf = new byte[8096];

        File zipFile = new File(theFileToZip.getParent(), theFileToZip.getName() + ".zip");
        try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) {

            try (FileInputStream fis = new FileInputStream(theFileToZip)) {

                zos.putNextEntry(new ZipEntry(theFileToZip.getName()));

                int lenght;
                while ((lenght = fis.read(buf)) > 0) {
                    zos.write(buf, 0, lenght);
                }
                zos.closeEntry();
            }
        }
    }
}

Related

  1. zip(File sourceDir, OutputStream targetStream)
  2. zip(File src, File target)
  3. zip(File srcDir, File zipFile)
  4. zip(File srcDirectory, File destFile)
  5. zip(File srcFile, File destFile, String archiveRoot)
  6. zip(File toZip, File outFile)
  7. zip(File[] sourceFiles, File destFile, boolean deleteOriginalFiles)
  8. zip(final File dir, final File target)
  9. zip(final File tempLocation, final File targetZipFile)