700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Java 将文本文件压缩为 .tar.gz 并实现.tar.gz 文件的解压

Java 将文本文件压缩为 .tar.gz 并实现.tar.gz 文件的解压

时间:2023-04-05 22:05:27

相关推荐

Java 将文本文件压缩为 .tar.gz 并实现.tar.gz 文件的解压

Java 将文本文件压缩为 .tar.gz 并实现.tar.gz 文件的解压,需要引入如下依赖包。

<dependency><groupId>org.apache.ant</groupId><artifactId>ant</artifactId><version>1.10.5</version></dependency><dependency><groupId>mons</groupId><artifactId>commons-compress</artifactId><version>1.5</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.6</version></dependency>

Java 实现方法及测试方法如下所示。

import lombok.extern.slf4j.Slf4j;import press.utils.IOUtils;import org.apache.tools.tar.TarEntry;import org.apache.tools.tar.TarInputStream;import org.apache.tools.tar.TarOutputStream;import org.springframework.util.StringUtils;import java.io.*;import java.nio.file.Files;import java.nio.file.Paths;import java.util.ArrayList;import java.util.List;import java.util.zip.GZIPInputStream;import java.util.zip.GZIPOutputStream;@Slf4jpublic class TarGzUtil {/*** 压缩 .tar.gz 文件** @param resourceList 源文件集合* @param outPath目标文件* @throws Exception 异常*/public static void packet(List<File> resourceList, String outPath) throws Exception {// 参数验证,初始化输出路径if (resourceList == null || resourceList.size() < 1 || StringUtils.isEmpty(outPath)) {log.error("文件压缩执行异常, 非法参数!");}// 迭代源文件集合,将文件打包为 tartry (FileOutputStream fileOutputStream = new FileOutputStream(outPath + ".tmp");BufferedOutputStream bufferedOutput = new BufferedOutputStream(fileOutputStream);TarOutputStream tarOutputStream = new TarOutputStream(bufferedOutput)) {// 可以改成的那个File类型for (File resourceFile : resourceList) {if (!resourceFile.isFile()) {continue;}FileInputStream fileInputStream = new FileInputStream(resourceFile);BufferedInputStream bufferedInput = new BufferedInputStream(fileInputStream);TarEntry entry = new TarEntry(new File(resourceFile.getName()));entry.setSize(resourceFile.length());tarOutputStream.putNextEntry(entry);IOUtils.copy(bufferedInput, tarOutputStream);tarOutputStream.closeEntry();}} catch (Exception e) {Files.delete(Paths.get(outPath + ".tmp"));log.error("文件压缩至 {} 执行异常,嵌套异常!", outPath, e);}// 读取打包好的 tar 临时文件,使用 GZIP 方式压缩try (FileInputStream fileInputStream = new FileInputStream(outPath + ".tmp");BufferedInputStream bufferedInput = new BufferedInputStream(fileInputStream);FileOutputStream fileOutputStream = new FileOutputStream(outPath);GZIPOutputStream gzipOutputStream = new GZIPOutputStream(fileOutputStream);BufferedOutputStream bufferedOutput = new BufferedOutputStream(gzipOutputStream);) {byte[] cache = new byte[1024];for (int index = bufferedInput.read(cache); index != -1; index = bufferedInput.read(cache)) {bufferedOutput.write(cache, 0, index);}log.info("文件 {} 压缩执行完毕", outPath);} catch (Exception e) {log.error("文件压缩至 {} 执行异常,嵌套异常!", outPath, e);} finally {Files.delete(Paths.get(outPath + ".tmp"));}}/*** 解压 .tar.gz文件** @param targzFile .tar.gz 压缩文件* @param outPath 存放解压后文件的目录*/public static void unpack(File targzFile, String outPath) {// 验证参数,初始化输出路径if (targzFile == null || !targzFile.isFile() || StringUtils.isEmpty(outPath)) {log.error("文件解压缩执行异常,非法参数!");} else {// 读取 .tar.gz 文件转换为 tar 文件try (FileInputStream fileInputStream = new FileInputStream(targzFile);BufferedInputStream bins = new BufferedInputStream(fileInputStream);GZIPInputStream gzipInputStream = new GZIPInputStream(bins);TarInputStream tarIn = new TarInputStream(gzipInputStream, 1024 * 2)) {// 迭代 tar 文件集合,解压文件for (TarEntry entry = tarIn.getNextEntry(); entry != null; entry = tarIn.getNextEntry()) {File targetFileName = new File(outPath + "/" + entry.getName());IOUtils.copy(tarIn, new FileOutputStream(targetFileName));}log.info("文件 {} 解压执行完毕", targzFile);} catch (Exception e) {log.error("{} 解压执行异常!", targzFile, e);}}}public static void main(String[] args) {File file1 = new File("D:\\test\\testfile1");File file2 = new File("D:\\test\\testfile2");List<File> resourceList = new ArrayList<>();resourceList.add(file1);resourceList.add(file2);String outPath = "D:\\test\\testfile.tar.gz";try {packet(resourceList, outPath);} catch (Exception e) {log.error("打包压缩文件出现异常", e);}unpack(new File(outPath), "D:\\test\\test2");}}

我的其他文章

亲身分享 一次 字节跳动 真实面试经历和面试题

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。