有用的java压缩类

package com.zhengjia.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.tools.zip.ZipOutputStream;

public class ZipUtil {

	/**
	 * inputFileName, file or directory waiting for zipping ,outputFileName
	 * 
	 * @param inputFileName
	 * @param outputFileName
	 * @throws Exception
	 */

	public static void zip(String zipFileName, String inputFileName)
			throws Exception {
		ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
				zipFileName));
		zip(out, new File(inputFileName), "");
		// System.out.println("zip done");
		out.close();
	}

	public static void zip(ZipOutputStream out, File f, String base)
			throws Exception {
		if (f.isDirectory()) {
			File[] fl = f.listFiles();
			if (System.getProperty("os.name").startsWith("Windows")) {
				out
						.putNextEntry(new org.apache.tools.zip.ZipEntry(base
								+ "\\"));
				base = base.length() == 0 ? "" : base + "\\";
			} else {

				out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));
				base = base.length() == 0 ? "" : base + "/";

			}

			for (int i = 0; i < fl.length; i++) {
				zip(out, fl[i], base + fl[i].getName());
			}
		} else {
			out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
			FileInputStream in = new FileInputStream(f);
			int b;
			System.out.println(base);
			while ((b = in.read()) != -1) {
				out.write(b);
			}
			in.close();

		}

	}

	public static void main(String args[]) {

		try {
			zip("D:\\Mail.zip", "D:\\Mail");

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}

你可能感兴趣的:(java,apache,windows,OS,F#)