Java Zip File zipFile(File file, File output)

Here you can find the source of zipFile(File file, File output)

Description

Create a ZIP archive of a file

License

Open Source License

Parameter

Parameter Description
file the file to be archived
output the ZIP archived file

Declaration

public static void zipFile(File file, File output) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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 {
    /**/*from   w  w  w.  j  a  v  a2s. c o  m*/
     * Create a ZIP archive of a file
     * @param file the file to be archived
     * @param output the ZIP archived file
     */
    public static void zipFile(File file, File output) {
        try {
            FileOutputStream fos = new FileOutputStream(output);
            ZipOutputStream zos = new ZipOutputStream(fos);
            ZipEntry ze = new ZipEntry(file.getName());
            zos.putNextEntry(ze);
            FileInputStream in = new FileInputStream(file);

            byte[] buffer = new byte[1024];
            int len;
            while ((len = in.read(buffer)) > 0) {
                zos.write(buffer, 0, len);

            }

            in.close();
            zos.closeEntry();
            zos.close();

        } catch (IOException ioe) {
            ioe.printStackTrace();

        }
    }
}

Related

  1. zip(String zipFileName, String[] zipEntries)
  2. zip(String zipName, String[] fileNames, boolean path)
  3. zip(String zipPath, Map input)
  4. zipFile(File aFileToZip)
  5. zipFile(File file)
  6. zipFile(File file, File zipFile)
  7. zipFile(File file, String zipFile)
  8. zipFile(File fileToZip, String fileName, ZipOutputStream zipOut)
  9. zipFile(File input, File output)