Here you can find the source of zipDir(String dir2zip, ZipOutputStream zos)
public static void zipDir(String dir2zip, ZipOutputStream zos)
//package com.java2s; /*/*from ww w. j a v a 2s.c o m*/ * Nokia Data Gathering * * Copyright (C) 2011 Nokia Corporation * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/ */ import java.io.File; import java.io.FileInputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { public 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()) { String filePath = f.getPath(); zipDir(filePath, 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(); } } }