Here you can find the source of zipFiles(String output_dir, List
public static void zipFiles(String output_dir, List<File> files)
//package com.java2s; /**/*w ww . ja v a2 s. c o m*/ * Copyright [2014-2016] PRAGMA, AIST, Data To Insight Center (IUB) * * 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.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { public static void zipFiles(String output_dir, List<File> files) { FileOutputStream fos = null; ZipOutputStream zipOut = null; FileInputStream fis = null; try { fos = new FileOutputStream(output_dir); zipOut = new ZipOutputStream(new BufferedOutputStream(fos)); for (File input : files) { fis = new FileInputStream(input); ZipEntry ze = new ZipEntry(input.getName()); zipOut.putNextEntry(ze); byte[] tmp = new byte[4 * 1024]; int size = 0; while ((size = fis.read(tmp)) != -1) { zipOut.write(tmp, 0, size); } zipOut.flush(); fis.close(); } zipOut.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (fos != null) fos.close(); } catch (Exception ex) { } } } }