压缩解压工具类

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

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;

/**
 * 压缩解压工具类
 * @author zql
 * @createTime 2020-11-21 13:23:35
 * @version 1.1
 * @modifyLog 1.1 优化代码
 *
 */
public class ZipUtil {

	/**
	 * 压缩成zip文件
	 * @author zql
	 * @createTime 2020-11-21 13:42:07
	 *
	 * @param srcDir
	 * @param out
	 * @param keepDir
	 * @throws Exception 
	 */
	public static void toZip(String srcDir, OutputStream out, boolean keepDir) throws Exception {
		long start = System.currentTimeMillis();
		ZipOutputStream zos = null;
		try {
			zos = new ZipOutputStream(out);
			File sourceFile = new File(srcDir);
			ZipUtil.compress(sourceFile, zos, sourceFile.getName(), keepDir);
			long end = System.currentTimeMillis();
			System.out.println("压缩完成,耗时:" + (end - start) + "ms");
		} catch (Exception e) {
			throw new Exception("压缩失败", e);
		}
	}
	
	/**
	 * 压缩文件
	 * @author zql
	 * @createTime 2020-11-21 13:41:34
	 *
	 * @param sourseFile
	 * @param zos
	 * @param name
	 * @param keepDir
	 * @throws Exception
	 */
	private static void compress(File sourseFile, ZipOutputStream zos, String name, boolean keepDir) throws Exception {
		byte[] buf = new byte[2048];
		FileInputStream in = null;
		try {
			if (sourseFile.isFile()) {
				zos.putNextEntry(new ZipEntry(name));
				int len;
				in = new FileInputStream(sourseFile);
				while((len = in.read(buf)) != -1) {
					zos.write(buf, 0, len);
				}
				zos.closeEntry();
				in.close();
			} else {
				File[] listFiles = sourseFile.listFiles();
				if (Objects.isNull(listFiles) || listFiles.length == 0) {
					if (keepDir) {
						zos.putNextEntry(new ZipEntry(name + "/"));
						zos.closeEntry();
					}
				} else {
					for (File file : listFiles) {
						if (keepDir) {
							ZipUtil.compress(file, zos, name + "/" + file.getName(), keepDir);
						} else {
							ZipUtil.compress(file, zos, file.getName(), keepDir);
						}
					}
				}
			}
		} catch (Exception e) {
			throw new Exception("系统异常", e);
		} finally {
			if (Objects.nonNull(in)) {
				try {
					in.close();
				} catch (Exception e2) {
					throw new Exception("关闭文化流失败", e2);
				}
			}
		}
	}
	
	/**
	 * zip解压缩
	 * @author zql
	 * @createTime:2020-11-21 13:43:51
	 *
	 * @param filePath 压缩文件路径
	 * @param resDirPath 解压位置路径
	 * @throws Exception 
	 */
	public static void unZip(String filePath, String resDirPath) throws Exception {
		ZipUtil.unZip(new File(filePath), resDirPath);
	}
	
	/**
	 * zip解压缩
	 * @author zql
	 * @createTime 2020-11-21 13:57:25
	 *
	 * @param file 压缩文件
	 * @param resDirPath 解压位置路径
	 * @throws Exception
	 */
	public static void unZip(File file, String resDirPath) throws Exception {
		ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
		ZipEntry ze = zis.getNextEntry();
		File parent = new File(resDirPath);
		if (!parent.exists() && parent.mkdirs()){
			zis.close();
			throw new IOException("创建解压目录\"" + parent.getAbsolutePath() + "\"失败");
		}
		try {
			while (Objects.nonNull(ze)) {
				String name = ze.getName();
				File child = new File(parent, name);
				if (ze.isDirectory()) {
					child.mkdirs();
				} else {
					FileOutputStream output = new FileOutputStream(child);
					byte[] buffer = new byte[1024];
					int bytesRead = 0;
					while ((bytesRead = zis.read(buffer)) > 0) {
						output.write(buffer, 0, bytesRead);
					}
					output.flush();
					output.close();
				}
				String suffix = ".tar";
				if (child.getName().endsWith(suffix)) {
					ZipUtil.unTar(child, resDirPath);
				}
				ze = zis.getNextEntry();
			}
		} catch (Exception e) {
			// 防止最后一次空读时出现异常:Unexpected end of ZLIB input stream
		}
		zis.close();
	}
	
	/**
	 * tar解压缩
	 * @author zql
	 * @createTime 2020-11-21 13:58:59
	 *
	 * @param filePath 压缩文件路径
	 * @param resDirPath 解压位置路径
	 * @throws Exception
	 */
	public static void unTar(String filePath, String resDirPath) throws Exception {
		ZipUtil.unTar(new File(filePath), resDirPath);
	}
	
	/**
	 * tar解压缩
	 * @author zql
	 * @createTime 2020-11-29 23:29:34
	 *
	 * @param file 压缩文件
	 * @param resDirPath 解压位置路径
	 * @throws Exception
	 */
	public static void unTar(File file, String resDirPath) throws Exception {
		TarArchiveInputStream tais = null;
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(file);
			tais = new TarArchiveInputStream(fis);
			TarArchiveEntry tae = null;
			
			while (Objects.nonNull((tae = tais.getNextTarEntry()))) {
				BufferedOutputStream bos = null;
				FileOutputStream fos = null;
				try {
					String dir = resDirPath + "/" + tae.getName();
					File dirFile = new File(dir);
					if (tae.isDirectory()) {
						dirFile.mkdirs();
					} else {
						fos = new FileOutputStream(dirFile);
						bos = new BufferedOutputStream(fos);
						int count;
						byte[] date = new byte[1024];
						while ((count = tais.read(date, 0, 1024)) != -1) {
							bos.write(date, 0, count);
						}
						String suffix = ".zip";
						if (dirFile.getName().endsWith(suffix)) {
							ZipUtil.unZip(dirFile, dirFile.getParent());
						}
					}
				} finally {
					if (Objects.nonNull(bos)) {
						bos.close();
					}
					if (Objects.nonNull(fos)) {
						fos.close();
					}
				}
			}
		} finally {
			if (Objects.nonNull(tais)) {
				tais.close();
			}
			if (Objects.nonNull(fis)) {
				fis.close();
			}
		}
	}
	
}

jar包:commons-compress-1.18.jar

maven依赖

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-compress</artifactId>
    <version>1.18</version>
</dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值