Java tutorial
//package com.java2s; /* Copyright [2013-2014] eBay Software Foundation Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FilenameFilter; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { private final static int BUFFER = 2048; /** * Compress files to a zip, and includes other operations such as filter,clean files * * @param zip * @param directory * @param filter * @throws IOException */ public static void compress(String zip, String directory, FilenameFilter filter) throws IOException { File fzip = new File(zip); File fdir = new File(directory); File files[] = fdir.listFiles(filter); if (files.length <= 0) { cleanZip(fzip); return; } else { compressToZip(fzip, files); cleanFiles(files); } } /** * @param file */ static void cleanZip(File file) { file.delete(); } /** * Compress files to a zip * @param zip * @param files * @throws IOException */ public static void compressToZip(File zip, File[] files) throws IOException { byte data[] = new byte[BUFFER]; FileOutputStream fozip = new FileOutputStream(zip); ZipOutputStream zo = new ZipOutputStream(new BufferedOutputStream(fozip)); for (int i = 0; i < files.length; i++) { System.out.println("Adding:" + files[i]); FileInputStream fi = new FileInputStream(files[i]); BufferedInputStream origin = new BufferedInputStream(fi, BUFFER); ZipEntry zipentry = new ZipEntry(files[i].getName()); zo.putNextEntry(zipentry); int count; while ((count = origin.read(data, 0, BUFFER)) != -1) { zo.write(data, 0, count); } origin.close(); } zo.close(); } /** * @param dir * @param filter */ static void cleanFiles(File[] files) { for (int i = 0; i < files.length; i++) { files[i].delete(); } } }