Java 实现zip格式的文件压缩与解压

学习了两周的IO操作,感觉就对压缩流感兴趣,顺便写点东西练练~.大神请无视~~

1.工具类

package foundation.io.zip;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * zip格式文件压缩与解压
 * 
 * @author Administrator
 *
 */
public class ZipUtils {
	//扩展名
	private static final String ZIP_EXTENSION = ".zip";

	/**
	 * 压缩 
	 * 
	 * @param srcPath 源路径
	 * @param descPath 目标路径  null 默认为源路径
	 * @param descName 目标文件名 null 默认源文件名
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static void enZip(String srcPath, String descPath, String descName) throws FileNotFoundException, IOException {
		//数据检测处理
		if (srcPath == null) throw new FileNotFoundException("源路径为空!");
		File file = new File(srcPath);
		if (!file.exists()) throw new FileNotFoundException("源路径不存在!");
		if (descPath == null || descPath.trim().equals("")) descPath = file.getParent();
		if (descName == null || descName.trim().equals("")) descName = file.getName();
		
		//获取系统文件编码
		Charset charset = Charset.forName(System.getProperty("file.encoding"));
		
		//构建压缩流
		ZipOutputStream zos = new ZipOutputStream(
									new FileOutputStream(
											new File(descPath + File.separator + descName + ZIP_EXTENSION)), charset);
		
		//压缩文件
		enZipDetail(file, zos, "");
		
		//关闭资源
		zos.close();
	}
	
	private static void enZipDetail(File file, ZipOutputStream zos, String parent) throws FileNotFoundException, IOException {
		int len = 0;
		ZipEntry entry = null;
		InputStream is = null;
		byte[] bytes = new byte[1024];
		
		if (file.isDirectory()) {
			if (!parent.equals("")) parent += File.separator;
			parent += file.getName();
			
			for(File subFile : file.listFiles()) {
				enZipDetail(subFile, zos, parent);
			}
		} else {
			//初始化输入流
			is = new FileInputStream(file);
			
			//构建ZipEntry
			if (parent.equals("")) {
				entry = new ZipEntry(file.getName());
			} else {
				entry = new ZipEntry(parent + File.separator + file.getName());
			}
			
			//压缩文件
			zos.putNextEntry(entry);
			while((len = is.read(bytes)) != -1) {
				zos.write(bytes, 0, len);
			}
			
			//关闭资源
			is.close();
		}
	}
	
	/**
	 * 解压文件
	 * 
	 * @param srcPath 源路径
	 * @param descPath 目标路径 null 默认源路径
	 * @param descName 解压后的文件夹名  null 默认无文件夹
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static void unZip(String srcPath, String descPath, String descName) throws FileNotFoundException, IOException {
		//数据监测处理
		if (srcPath == null) throw new FileNotFoundException("源路径为空!");
		File file = new File(srcPath);
		if (!file.exists()) throw new FileNotFoundException("源路径不存在!");
		if (descPath == null || descPath.trim().equals("")) descPath = file.getParent();
		if (descName == null || descName.trim().equals("")) descName = "";
		
		//解压文件
		unZipDetail(srcPath, descPath, descName);
	}
	
	/**
	 * 
	 * @param srcPath
	 * @param descPath
	 * @param descName
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	private static void unZipDetail(String srcPath, String descPath, String descName) throws FileNotFoundException, IOException {
		//获取系统文件编码 (zis , zipFile 不设置charset, 处理中文时可能会抛出 IllegalArgumentException 异常)
		Charset charset = Charset.forName(System.getProperty("file.encoding"));
		//获取对应ZipEntry 的输入流
		ZipFile zipFile = new ZipFile(srcPath, charset);
		//遍历每一个 ZipEntry
		ZipInputStream zis = new ZipInputStream(new FileInputStream(srcPath), charset);
		
		//定义使用的变量
		int len = 0;
		String parent = null;
		ZipEntry entry = null;
		InputStream is = null;
		File parentFile = null;
		String fileName = null;
		OutputStream os = null;
		String entryName = null;
		byte[] bytes = new byte[1024];
		
		//遍历处理每一个 ZipEntry
		while((entry = zis.getNextEntry()) != null) {
			entryName = entry.getName();
			
			//构建 ZipEntry所处的目录及文件名
			if (entryName.contains("\\")) {
				entryName = entryName.replace("\\", "/");
				fileName = entryName.substring(entryName.lastIndexOf("/") + 1, entryName.length());
				parent = entryName.substring(0, entryName.lastIndexOf("/"));
				if (!descName.equals("")) parent = descName + File.separator + parent;
				parentFile = new File(descPath + File.separator + parent);
			} else {
				fileName = entryName;
				parentFile = new File(descPath);
				if (!descName.equals("")) parentFile = new File(parentFile, descName);
			}
			
			//初始化路径、输入流和输出流
			if (!parentFile.exists()) parentFile.mkdirs();
			is = zipFile.getInputStream(entry);
			os = new FileOutputStream(new File(parentFile, fileName));
			
			//解压文件
			while((len = is.read(bytes)) != -1) {
				os.write(bytes, 0, len);
			}
			
			//关闭资源
			os.close();
			is.close();
			zis.closeEntry();
		}
		//关闭资源
		zis.close();
		zipFile.close();
	}
}


2.测试类

package foundation.io.zip;

import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * ZipUtils 测试
 * 
 * @author Administrator
 *
 */
public class Client {
	public static void main(String[] args) throws FileNotFoundException, IOException {
		long start = System.currentTimeMillis();
		//ZipUtils.enZip("C:/Users/Administrator/Desktop/src", null, null);
		ZipUtils.unZip("C:/Users/Administrator/Desktop/src.zip", null, "srcTemp");
		System.out.println("用时:" + (System.currentTimeMillis() - start) + "ms");
	}
}

压缩和解压的是JDK源文件:

压缩用时:4497ms

解压用时:6902ms
类似压缩工具,可以压缩单个文件或者单个目录下的所有文件,没有实现多个文件遍历压缩!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值