zip文件压缩、解压工具(JDK)

 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * @Description: zip文件压缩、解压工具类
 * @Title: ZipFileUtil.java
 * @Package com.farer.module.core.utils
 * @author: farer
 */
public class ZipFileUtil {
	
	private ZipFileUtil() {
		throw new IllegalStateException("Utility class");
	}
	
	/***
	 * @Description: 压缩文件
	 * @Title: zip
	 * @param file    源文件
	 * @param zipFile 压缩文件
	 * @author: farer
	 */
	public static void zip(File file, File zipFile) {
		try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));) {
			if (file.isFile()) {
				zos.putNextEntry(new ZipEntry(file.getName()));
				try (FileInputStream fis = new FileInputStream(file);) {
					byte[] buffer = new byte[1024];
					int position = 0;
					while ((position = fis.read(buffer)) != -1) {
						zos.write(buffer, 0, position);
					}
				}
			} else {
				directoryZip(zos, file, "");
			}
		} catch (IOException e) {
			e.getMessage();
		}
	}

	/**
	 * @Description: 压缩文件目录      
	 * @Title: directoryZip
	 * @param out
	 * @param file
	 * @param base
	 * @throws IOException void  
	 * @author: farer 
	 */
	private static void directoryZip(ZipOutputStream out, File file, String base) throws IOException {
		// 如果传入的是目录
		if (file.isDirectory()) {
			File[] fl = file.listFiles();
			if (fl == null) {
				return;
			}
			// 创建压缩的子目录
			out.putNextEntry(new ZipEntry(base + File.separatorChar));
			if (base.length() == 0) {
				base = "";
			} else {
				base = base + File.separatorChar;
			}
			for (int i = 0; i < fl.length; i++) {
				directoryZip(out, fl[i], base + fl[i].getName());
			}
		} else {
			// 把压缩文件加入rar中
			out.putNextEntry(new ZipEntry(base));
			try (FileInputStream in = new FileInputStream(file);) {
				byte[] buffer = new byte[1024];
				int position = 0;
				while ((position = in.read(buffer)) != -1) {
					out.write(buffer, 0, position);
				}
			}
		}
	}

	/**
	 * @Description: zip文件解压到指定的文件夹      
	 * @Title: unZip
	 * @param directory 指定的文件夹
	 * @param zipFile zip文件
	 * @throws IOException void  
	 * @author: farer 
	 */
	public static void unZip(String directory, String zipFile) throws IOException {
		try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));) {
			File f = new File(directory);
			f.mkdirs();
			fileUnZip(zis, f);
		}
	}

	/**
	 * @Description: 解压文件、文件夹      
	 * @Title: fileUnZip
	 * @param zis 压缩文件流
	 * @param file 文件
	 * @throws IOException void  
	 * @author: farer 
	 */
	private static void fileUnZip(ZipInputStream zis, File file) throws IOException {
		ZipEntry zip = zis.getNextEntry();
		if (zip == null) {
			return;
		}
		File f = new File(file.getAbsolutePath() + File.separatorChar + zip.getName());
		if (zip.isDirectory()) {
			f.mkdirs();
			fileUnZip(zis, file);
		} else {
			boolean newFile = f.createNewFile();
			if (newFile) {
				try (FileOutputStream fos = new FileOutputStream(f);) {
					byte[] buffer = new byte[1024];
					int position = 0;
					while ((position = zis.read(buffer)) != -1) {
						fos.write(buffer, 0, position);
					}
				}
			}
		}
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值