Here you can find the source of zip(String srcPath)
public static void zip(String srcPath) throws IOException
//package com.java2s; //License from project: Apache License import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { public static void zip(String srcPath) throws IOException { int end = srcPath.lastIndexOf(File.separator); String targetPath = srcPath.substring(0, end); String zipName = srcPath.substring(end + 1); zip(srcPath, targetPath, zipName); }/*from w w w . j a v a2 s . c o m*/ public static void zip(String srcPath, String targetPath) throws IOException { String zipName = srcPath.substring(srcPath.lastIndexOf(File.separator)); zip(srcPath, targetPath, zipName); } public static void zip(String srcPath, String targetPath, String zipName) throws IOException { ZipOutputStream out = new ZipOutputStream( new FileOutputStream(targetPath + File.separator + zipName + ".zip")); File f = new File(srcPath); recursiveZip(out, f, ""); out.close(); } public static void zip(String[] fileFullPathList, String targetPath, String zipName) throws IOException { ZipOutputStream out = null; FileInputStream in = null; try { byte[] buf = new byte[1024]; out = new ZipOutputStream(new FileOutputStream(targetPath + File.separator + zipName + ".zip")); for (String nowFilePath : fileFullPathList) { File nowFile = new File(nowFilePath); in = new FileInputStream(nowFile); //out.putNextEntry(new ZipEntry(new String(nowFile.getName().getBytes("gb2312"),"ISO8859-1"))); out.putNextEntry(new ZipEntry(nowFile.getName())); int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); in.close(); in = null; } } catch (IOException e) { throw e; } finally { if (out != null) out.close(); if (in != null) in.close(); } } public static void zip(List<String> fileFullPathList, OutputStream outputstream) throws IOException { ZipOutputStream out = null; FileInputStream in = null; try { byte[] buf = new byte[1024]; out = new ZipOutputStream(outputstream); for (String nowFilePath : fileFullPathList) { File nowFile = new File(nowFilePath); in = new FileInputStream(nowFile); //out.putNextEntry(new ZipEntry(new String(nowFile.getName().getBytes("gb2312"),"ISO8859-1"))); out.putNextEntry(new ZipEntry(nowFile.getName())); int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); in.close(); in = null; } } catch (IOException e) { throw e; } finally { try { if (out != null) out.close(); } catch (Exception e) { } try { if (in != null) in.close(); } catch (Exception e) { } } } private static void recursiveZip(ZipOutputStream out, File currentF, String currentPath) throws IOException { if (currentF.isDirectory()) { out.putNextEntry(new ZipEntry(currentPath + "/")); currentPath = currentPath.length() == 0 ? "" : currentPath + "/"; File[] _fs = currentF.listFiles(); for (File element : _fs) { recursiveZip(out, element, currentPath + element.getName()); } } else { out.putNextEntry(new ZipEntry(currentPath)); FileInputStream in = new FileInputStream(currentF); int b; while ((b = in.read()) != -1) out.write(b); in.close(); } } }