vlambda博客
学习文章列表

Java 解压/压缩 tar.gz文件

/** * @功能描述 压缩tar.gz 文件 * @param resourceList 源文件集合要压缩的文件的全路径(数组) * @param outPath 压缩后的文件全文件名(.tar) */ public static void packet(List<File> resourceList, String outPath) throws Exception {
long startTime = System.currentTimeMillis(); // 2. 迭代源文件集合, 将文件打包为Tar try (FileOutputStream fileOutputStream = new FileOutputStream(outPath + ".tmp"); BufferedOutputStream bufferedOutput = new BufferedOutputStream(fileOutputStream); TarOutputStream tarOutputStream = new TarOutputStream(bufferedOutput);) { 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) { Logger.getLogger("DUT").info("文件[" + resourceFile + "]压缩执行异常, 嵌套异常: \n" + e.toString()); } finally { tarOutputStream.closeEntry(); } } } catch (Exception e) { Files.delete(Paths.get(outPath + ".tmp")); Logger.getLogger("DUT").info("文件压缩至[" + outPath + "]执行异常, 嵌套异常: \n" + e.toString()); } //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(); Logger.getLogger("DUT").info("文件[" + outPath + "]压缩执行完毕, 耗时:" + (endTime - startTime) + "ms"); } catch (Exception e) { Logger.getLogger("DUT").info("文件压缩至[" + outPath + "]执行异常, 嵌套异常: \n" + e.toString()); } finally { Files.delete(Paths.get(outPath + ".tmp")); } }
/** * @功能描述 解压tar.gz文件 * @param targzFile tar.gz压缩文件 * @param outPath 存放解压后文件的目录 /opt/java/ */ public static void unpack(File targzFile, String outPath) throws IOException {
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)); } targzFile.delete(); long endTime = System.currentTimeMillis(); Logger.getLogger("DUT").info("文件[" + targzFile + "]解压执行完毕, 耗时:" + (endTime - startTime) + "ms"); } catch (Exception e) { Logger.getLogger("DUT").info("[" + targzFile + "] 解压执行异常, " + e.toString()); } }

jar包:commons-compress-1.18.jar