Here you can find the source of zipDirectory(String directoryName, String targetName)
public static void zipDirectory(String directoryName, String targetName)
//package com.java2s; /*/*from w w w .j a va 2 s . co m*/ * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { public static void zipDirectory(String directoryName, String targetName) { try { ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(targetName + ".zip")); zipDir(directoryName, zos); zos.close(); } catch (Exception e) { e.printStackTrace(System.out); } } private static void zipDir(String dir2zip, ZipOutputStream zos) { try { File zipDir = new File(dir2zip); String[] dirList = zipDir.list(); byte[] readBuffer = new byte[2156]; int bytesIn = 0; for (int i = 0; i < dirList.length; i++) { File f = new File(zipDir, dirList[i]); if (f.isDirectory()) { zipDir(f.getPath(), zos); continue; } FileInputStream fis = new FileInputStream(f); ZipEntry anEntry = new ZipEntry(f.getPath()); zos.putNextEntry(anEntry); while ((bytesIn = fis.read(readBuffer)) != -1) zos.write(readBuffer, 0, bytesIn); fis.close(); } } catch (Exception e) { e.printStackTrace(System.out); } } }