Java压缩解压文件/文件夹

package com.zip;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

/**
 * 解压缩文件
 * 
 * @author baiyang
 */
public class Zip {
	/**
	 * 压缩文件为zip格式
	 * 
	 * @paramzipFile 压缩文件后的文件名
	 * @paramsrcPathName 待压缩文件
	 * @throws IOException
	 *             文件不存在异常
	 */
	public static void zipFile(String zipFile, String srcPathName) throws IOException {
		File file = new File(srcPathName);
		if (!file.exists())
			throw new RuntimeException(srcPathName + "not exist!");
		FileOutputStream fileOutputStream = new FileOutputStream(zipFile + File.separator + srcPathName.substring(srcPathName.lastIndexOf("/")) + ".zip");
		CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream, new CRC32());
		ZipOutputStream out = new ZipOutputStream(cos);
		out.setEncoding("GBK");
		String basedir = "";
		compress(file, out, basedir);
		out.flush();
		out.close();
	}

	private static void compress(File file, ZipOutputStream out, String basedir) throws IOException {
		if (file.isDirectory()) {
			compressDirectory(file, out, basedir);
		} else {
			compressFile(file, out, basedir);
		}
	}

	private static void compressDirectory(File dir, ZipOutputStream out, String basedir) throws IOException {
		if (!dir.exists())
			return;
		File[] files = dir.listFiles();
		for (int i = 0; i < files.length; i++) {
			compress(files[i], out, basedir + dir.getName() + "/");
		}
	}

	private static void compressFile(File file, ZipOutputStream out, String basedir) throws IOException {
		if (!file.exists()) {
			return;
		}
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
		ZipEntry entry = new ZipEntry(basedir + file.getName());
		out.putNextEntry(entry);
		int len = 0;
		byte data[] = new byte[1024];
		while ((len = bis.read(data)) != -1) {
			out.write(data, 0, len);
		}
		bis.close();
	}

	private static void createDir(String path) {
		File dir = new File(path);
		if (dir.exists() == false)
			dir.mkdir();
	}

	private static String getSuffixName(String name) {
		return name.substring(0, name.lastIndexOf("."));
	}

	/**
	 * 解压缩文件
	 * 
	 * @paramzipFilePath 压缩文件路径
	 * @paramunzipDirectory 要解压到的目录
	 * @throws IOException
	 *             文件读写异常
	 */
	public static void unZip(String zipFilePath, String unzipDirectory) throws IOException {
		File file = new File(zipFilePath);
		ZipFile zipFile = new ZipFile(file);
		File unzipFile = new File(unzipDirectory + "/" + getSuffixName(file.getName()));
		if (unzipFile.exists())
			unzipFile.delete();
		unzipFile.mkdirs();
		@SuppressWarnings("rawtypes")
		Enumeration zipEnum = zipFile.getEntries();
		InputStream input = null;
		OutputStream output = null;
		ZipEntry entry = null;
		while (zipEnum.hasMoreElements()) {
			entry = (ZipEntry) zipEnum.nextElement();
			String entryName = new String(entry.getName());
			String names[] = entryName.split("\\/");
			int length = names.length;
			String path = unzipFile.getAbsolutePath();
			for (int v = 0; v < length; v++) {
				if (v < length - 1) {
					path += "/" + names[v] + "/";
					createDir(path);
				} else {
					if (entryName.endsWith("/"))
						createDir(unzipFile.getAbsolutePath() + "/" + entryName);
					else {
						input = zipFile.getInputStream(entry);
						output = new FileOutputStream(new File(unzipFile.getAbsolutePath() + "/" + entryName));
						byte[] buffer = new byte[1024 * 8];
						int readLen = 0;
						while ((readLen = input.read(buffer, 0, 1024 * 8)) != -1)
							output.write(buffer, 0, readLen);
						input.close();
						output.flush();
						output.close();
					}
				}
			}
		}
	}
}

为了解决汉字乱码问题,代码中用到了ant

下载所需Ant

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值