Java使用ZIP格式压缩与解压缩

最经因为网站的上传与下载不进行压缩速度不是很理想,尤其是文件特别多的情况所以就研究了一下、java能做的就是zip格式的压缩、不过通常国内都是用的rar。。但是rar是收费的、不过在中国不需要考虑哈,因为盗版、国外用的一般都是zip的格式比较多,我就把我写的东西给大家看看、希望多提提意见哈。

package com.dnion.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

/**
 * 
 * 功能: 1 、实现把指定文件夹下的所有文件压缩为指定文件夹下指定 zip 文件 2 、实现把指定文件夹下的 zip 文件解压到指定目录下
 * 
 * @author ffshi
 * 
 */
public class ZipUtils {
	/**
	 * 
	 * 方法名:Zip 描述: 日期:2012-4-1 下午2:10:13
	 * 
	 * @param sourceDir
	 *            //指定要压缩的文件夹路径
	 * @param outZipFile
	 *            //指定压缩后文件夹输出路径
	 * @return void
	 */
	public static void Zip(String sourceDir, String zipFile) {
		//创建一个输出流
		OutputStream os = null;
		try {
			//打开一个写ZIP文件的输出流
			os = new FileOutputStream(zipFile);
			//接上一个缓冲流、不用频繁读取文件
			BufferedOutputStream bos = new BufferedOutputStream(os);
			//读取ZIP文件当然要用到ZIP的文件打印流
			ZipOutputStream zos = new ZipOutputStream(bos);
			//打开要压缩的文件目录
			File file = new File(sourceDir);
			//基本路径
			String basePath = null;
			//如果有子目录就获得基本路径没有就获得上一节点路径
			if (file.isDirectory()) {
				basePath = file.getPath();
			} else {
				basePath = file.getParent();
			}
			//调用创建ZIP文件方法
			createZip(file, basePath, zos);
			//关掉当前ZIP写入流
			zos.closeEntry();
			//关掉ZIP流和过滤流
			zos.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 
	 * 方法名:createZip 描述: 作者: 日期:2012-4-1 下午2:14:30
	 * 
	 * @param @param source
	 * @param @param basePath
	 * @param @param zos
	 * @return void
	 */
	private static void createZip(File source, String basePath,
			ZipOutputStream zos) {

		// 创建文件对象用于装载要压缩文件
		File[] files = new File[0];

		String pathName;

		int length = 0;
		// 检测是否有子目录
		if (source.isDirectory()) {
			// 获取目录下所有文档
			files = source.listFiles();
		} else {
			files = new File[1];
			files[0] = source;
		}
		byte[] buf = new byte[1024];
		try {
			for (File file : files) {
				if (file.isDirectory()) {
					// 获得文件路径+“/”表示还有子目录
					pathName = file.getPath().substring(basePath.length() + 1)
							+ "/";
					// 创建一个Zip文件目录进行传递给输出流
					zos.putNextEntry(new ZipEntry(pathName));
					// 递归压缩到没有子目录
					createZip(file, basePath, zos);
				} else {

					pathName = file.getPath().substring(basePath.length() + 1);
					// 打开文件输入通道
					InputStream is = new FileInputStream(file);
					// 使用缓冲流对接压缩流
					BufferedInputStream bis = new BufferedInputStream(is);
					// 创建压缩目录
					zos.putNextEntry(new ZipEntry(pathName));
					// 循环写入压缩流
					while ((length = bis.read(buf)) > 0) {
						zos.write(buf, 0, length);
					}
					// 关闭文件流
					is.close();
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	/**
	 * 
	 * 方法名:unZip 描述:不能解压rar 日期:2012-4-1 下午5:33:19
	 * 
	 * @param @param zipFile
	 * @param @param descDir
	 * @return void
	 */
	private static void unZip(String zipfile, String descDir) {
		// 验证目录正确性
		descDir = descDir.endsWith("//") ? descDir : descDir + "//";
		byte b[] = new byte[1024];
		int length;
		ZipFile zipFile;

		try {
			// 打开ZIP压缩文件
			zipFile = new ZipFile(new File(zipfile));
			// 返回文件目录枚举
			Enumeration enumeration = zipFile.entries();
			// 创建Zip目录
			ZipEntry zipEntry = null;
			// 遍历目录枚举
			while (enumeration.hasMoreElements()) {
				// 获得压缩目录
				zipEntry = (ZipEntry) enumeration.nextElement();
				// 创建压缩目录文件
				File loadFile = new File(descDir + zipEntry.getName());

				if (zipEntry.isDirectory()) {
					loadFile.mkdirs();
				} else {

					if (!loadFile.getParentFile().exists()) {
						loadFile.getParentFile().mkdirs();
					}
					OutputStream os = new FileOutputStream(loadFile);
					InputStream zis = zipFile.getInputStream(zipEntry);
					while ((length = zis.read(b)) > 0) {
						os.write(b, 0, length);
					}
					os.close();
				}
			}
			System.out.println(" 文件解压成功 ");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		Zip("D://WebPage","D://WebPage.zip");
		unZip("D://WebPage.zip","D://TestZip");
	}
}

原理就是使用API自带的方法进行压缩。。很简单的一个流程、压缩就是把文件同过zip算法进行压缩,java实现就是通过zip的流来重新写一份文件解压缩就是压缩的逆操作

把zip的流通过缓冲流和文件流对接进行读写~代码注释很详细不细细解释了。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值