Here you can find the source of zipFile(File file, File output)
Parameter | Description |
---|---|
file | the file to be archived |
output | the ZIP archived file |
public static void zipFile(File file, File output)
//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(); } } }