Windows本地目录下使用Java 将文本文件压缩为[.tar.gz],并实现文件解压
Posted 经理,天台风好大
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Windows本地目录下使用Java 将文本文件压缩为[.tar.gz],并实现文件解压相关的知识,希望对你有一定的参考价值。
Java 将文本文件压缩为 .tar.gz 并实现.tar.gz 文件的解压,需要引入如下依赖包
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.10.5</version>
</dependency>
<dependency>
<groupId>org.apache.commons</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 实现方法及测试方法如下所示。
package com.yx.outside.utils;
import com.google.protobuf.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.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;
@Slf4j
public class TarGzUtil
/**
* 压缩 .tar.gz 文件
*
* @param resourceList 源文件集合
* @param outPath 目标文件
* @return 返回压缩结果
* @throws Exception 异常
*/
public static void packet(List<File> resourceList, String outPath) throws Exception
// 1.参数验证,初始化输出路径
if (resourceList == null || resourceList.size() < 1 || StringUtils.isEmpty(outPath))
log.error("文件压缩执行异常, 非法参数!");
long startTime = System.currentTimeMillis();
// 2.迭代源文件集合,将文件打包为 tar
try (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;
try (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);
catch (Exception e)
throw new ServiceException("文件[" +resourceFile+ "]压缩执行异常, 嵌套异常: \\n" + e.toString());
finally
tarOutputStream.closeEntry();
catch (Exception e)
Files.delete(Paths.get(outPath + ".tmp"));
throw new ServiceException("文件压缩至[" +outPath+ "]执行异常, 嵌套异常: \\n" + e.toString());
// log.error("文件压缩至 执行异常,嵌套异常!", outPath, e);
// 3.读取打包好的 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);
long endTime = System.currentTimeMillis();
log.info("文件[" +outPath+ "]压缩执行完毕, 耗时:" + (endTime - startTime) + "ms");
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 存放解压后文件的目录
* @return 返回结果
*/
public static void unpack(File targzFile, String outPath)
// 1.验证参数,初始化输出路径
if (targzFile == null || !targzFile.isFile() || StringUtils.isEmpty(outPath))
log.error("文件解压缩执行异常,非法参数!");
else
long startTime = System.currentTimeMillis();
// 2.读取 .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))
// 3.迭代 tar 文件集合,解压文件
for (TarEntry entry = tarIn.getNextEntry(); entry != null; entry = tarIn.getNextEntry())
File targetFileName = new File(outPath + "/" + entry.getName());
IOUtils.copy(tarIn, new FileOutputStream(targetFileName));
long endTime = System.currentTimeMillis();
log.info("文件["+targzFile+"]解压执行完毕, 耗时:" + (endTime - startTime) + "ms");
log.info("文件 解压执行完毕", targzFile);
catch (Exception e)
log.error(" 解压执行异常!", targzFile, e);
//Windows本地目录下测试
public static void main(String[] args)
File file1 = new File("D:\\\\test专区\\\\B临时文件\\\\test\\\\90.txt");
File file2 = new File("D:\\\\test专区\\\\B临时文件\\\\test\\\\91.txt");
List<File> resourceList = new ArrayList<>();
resourceList.add(file1);
resourceList.add(file2);
String outPath = "D:\\\\test专区\\\\B临时文件\\\\test\\\\testfile.tar.gz";
try
packet(resourceList, outPath);
catch (Exception e)
log.error("打包压缩文件出现异常", e);
unpack(new File(outPath), "D:\\\\test专区\\\\B临时文件\\\\test\\\\test2");
以上是关于Windows本地目录下使用Java 将文本文件压缩为[.tar.gz],并实现文件解压的主要内容,如果未能解决你的问题,请参考以下文章