Here you can find the source of zipDirectory(File dir, String zipDirName)
public static void zipDirectory(File dir, String zipDirName)
//package com.java2s; /*/* w w w . ja v a 2 s . c om*/ * Copyright 2013 The Trustees of Indiana University * * 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.*; import java.util.ArrayList; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { static List<String> filesListInDir; public static void zipDirectory(File dir, String zipDirName) { try { filesListInDir = new ArrayList<String>(); getFilesList(dir); FileOutputStream fos = new FileOutputStream(zipDirName); ZipOutputStream zos = new ZipOutputStream(fos); for (String filePath : filesListInDir) { ZipEntry ze = new ZipEntry( filePath.substring(dir.getAbsolutePath().length() + 1, filePath.length())); zos.putNextEntry(ze); FileInputStream fis = new FileInputStream(filePath); byte[] buffer = new byte[1024]; int len; while ((len = fis.read(buffer)) > 0) { zos.write(buffer, 0, len); } zos.closeEntry(); fis.close(); } zos.close(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } /** * This method populates all the files in a directory to a List * @param dir * @throws IOException */ private static void getFilesList(File dir) throws IOException { File[] files = dir.listFiles(); for (File file : files) { if (file.isFile()) filesListInDir.add(file.getAbsolutePath()); else getFilesList(file); } } }