Here you can find the source of zipFile(String filedir, String zippath)
public static void zipFile(String filedir, String zippath)
//package com.java2s; /**// w w w. j av a2 s .co m * Automatic Subtitle Downloader * http://code.google.com/p/autosubdown/ * * Copyright 2010-2011 Raphael Medeiros. * * 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 */ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { private final static int BUFSIZE = 4096; public static void zipFile(String filedir, String zippath) { @SuppressWarnings("rawtypes") List fl = getAllFilePath(filedir); File f = new File(zippath); try { FileOutputStream fo = new FileOutputStream(f); ZipOutputStream zo = new ZipOutputStream(fo); for (int i = 0; i < fl.size(); i++) { File ff = (File) fl.get(i); ZipEntry z = new ZipEntry(getZipEntryPath(ff.getPath(), filedir)); zo.putNextEntry(z); FileInputStream fi = new FileInputStream(ff); byte inbuf[] = new byte[BUFSIZE]; int n = 0; while ((n = fi.read(inbuf, 0, BUFSIZE)) != -1) { zo.write(inbuf, 0, n); } fi.close(); } zo.close(); } catch (Exception e) { e.printStackTrace(); } } @SuppressWarnings("rawtypes") private static List getAllFilePath(String filedir) { File f = new File(filedir); List<File> l = new ArrayList<File>(); getFilePaths(f, l); return l; } private static String getZipEntryPath(String filepath, String filedir) { if (null == filedir) { return filepath; } if (filedir.substring(filedir.length() - 1).equals("\\") || filedir.substring(filedir.length() - 1).equals("/")) { } else { filedir += "/"; } String zipEntryPath = filepath.substring(filedir.length()); zipEntryPath = zipEntryPath.replace('\\', '/'); return zipEntryPath; } private static void getFilePaths(File dir, List<File> l) { if (dir.isDirectory()) { File d[] = dir.listFiles(); for (int i = 0; i < d.length; i++) { getFilePaths(d[i], l); } } else if (dir.isFile()) { l.add(dir); } } }