java代码如何将多个文件打包压缩成tar.gz文件及解压呢?
下文笔者讲述使用java代码将文件压缩为tar.gz或解压的方法分享,如下所示
压缩或解压为tar.gz的实现思路
1.引入commons-compress jar包 2.借助GZIPOutputStream对象即可实现将文件转换为tar.gz 使用 org.apache.ant jar包 借助GZIPInputStream 和 TarInputStream 对象即可实现文件的压缩和解压操作例:压缩文件
引入jar包
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-compress</artifactId> <version>1.21</version> </dependency>
//先用tar打包 //再使用gz进行压缩 package com.java265.utils; import lombok.extern.slf4j.Slf4j; import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; import org.apache.commons.compress.utils.IOUtils; import org.apache.commons.lang3.RandomStringUtils; import java.io.*; import java.util.zip.GZIPOutputStream; @Slf4j public class FileUtils { /** * * 压缩文件 * * @param sourceFolder 指定打包的源目录 * @param tarGzPath 指定目标 tar 包的位置 */ private static void compress(String sourceFolder, String tarGzPath) { log.info("压缩后文件名:{}", tarGzPath); TarArchiveOutputStream tarOs = null; try { // 创建一个 FileOutputStream 到输出文件(.tar.gz) FileOutputStream fos = new FileOutputStream(tarGzPath); // 创建一个 GZIPOutputStream,用来包装 FileOutputStream 对象 GZIPOutputStream gos = new GZIPOutputStream(new BufferedOutputStream(fos)); // 创建一个 TarArchiveOutputStream,用来包装 GZIPOutputStream 对象 tarOs = new TarArchiveOutputStream(gos); // 使文件名支持超过 100 个字节 tarOs.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX); File sourceFile = new File(sourceFolder); //遍历源目录的文件,将所有文件迁移到新的目录tarGzPath下 File[] sources = sourceFile.listFiles(); for (File oneFile : sources) { addFilesToTarGZ(oneFile.getPath(), "", tarOs); } } catch (IOException e) { log.error("压缩失败,", e); } finally { try { tarOs.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * @param sourcePath 源文件 * @param parent 源目录 * @param tarArchive 压缩输出流 * @throws IOException */ public static void addFilesToTarGZ(String sourcePath, String parent, TarArchiveOutputStream tarArchive) throws IOException { File sourceFile = new File(sourcePath); // 获取新目录下的文件名称 String fileName = parent.concat(sourceFile.getName()); //打包压缩该文件 tarArchive.putArchiveEntry(new TarArchiveEntry(sourceFile, fileName)); if (sourceFile.isFile()) { FileInputStream fis = new FileInputStream(sourceFile); BufferedInputStream bis = new BufferedInputStream(fis); // 写入文件 IOUtils.copy(bis, tarArchive); tarArchive.closeArchiveEntry(); bis.close(); } else if (sourceFile.isDirectory()) { // 因为是个文件夹,无需写入内容,关闭即可 tarArchive.closeArchiveEntry(); // 遍历文件夹下的文件 for (File f : sourceFile.listFiles()) { // 递归遍历文件目录树 addFilesToTarGZ(f.getAbsolutePath(), fileName + File.separator, tarArchive); } } } }
解压文件
//引入jar包依赖 <!--解压--> <dependency> <groupId>org.apache.ant</groupId> <artifactId>ant</artifactId> <version>1.10.5</version> </dependency>
/** * 解压tar.gz 文件 * @param targzFile 要解压的tar.gz文件对象 * @param outPath 要解压到某个指定的目录下 * @throws IOException */ public static void unpack(File targzFile, String outPath) { // 验证参数 if (targzFile == null || !targzFile.isFile() || StringUtils.isEmpty(outPath)) { log.error("文件解压缩执行异常,请检查输入参数!"); return; } // 读取 .tar.gz 文件转换为 tar 文件 try (FileInputStream is = new FileInputStream(targzFile); BufferedInputStream bis = new BufferedInputStream(is); GZIPInputStream gzipIs = new GZIPInputStream(bis); TarInputStream tarIs = new TarInputStream(gzipIs, 1024 * 2)) { // 迭代 tar 文件集合,解压文件 for (TarEntry entry = tarIs.getNextEntry(); entry != null; entry = tarIs.getNextEntry()) { File targetFileName = new File(outPath + "/" + entry.getName()); IOUtils.copy(tarIs, new FileOutputStream(targetFileName)); } log.info("文件 {} 解压完毕", targzFile); } catch (Exception e) { log.error("{} 解压异常!", targzFile, e); } }例
public static void main(String[] args) throws IOException { // 把F:\img\source内的文件及其文件夹打包成名为文件夹F:\img\target下的、名字为六位随机数的 gz 压缩包 String targetPath = "F:\\img\\target\\" + RandomStringUtils.randomAlphanumeric(6) + ".tar.gz"; compress("F:\\img\\source", targetPath); log.info("=====done===="); unpack(new File(targetPath) , "F:\\img\\unpack"); }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。