Here you can find the source of zipDir(String origDir, File dirObj, ZipOutputStream zos)
private static void zipDir(String origDir, File dirObj, ZipOutputStream zos) throws IOException
//package com.java2s; /******************************************************************************* * Copyright 2014 Umesh Kanitkar// www .jav a 2 s . co m * * 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.File; import java.io.FileInputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { private static void zipDir(String origDir, File dirObj, ZipOutputStream zos) throws IOException { File[] files = dirObj.listFiles(); byte[] tmpBuf = new byte[1024]; for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { zipDir(origDir, files[i], zos); continue; } String wAbsolutePath = files[i].getAbsolutePath().substring( origDir.length() + 1, files[i].getAbsolutePath().length()); FileInputStream in = new FileInputStream( files[i].getAbsolutePath()); zos.putNextEntry(new ZipEntry(wAbsolutePath)); int len; while ((len = in.read(tmpBuf)) > 0) { zos.write(tmpBuf, 0, len); } zos.closeEntry(); in.close(); } } }