前言
之前在代码上一直使用的是对zip的解压,最近对接方居然使用了tgz的压缩包,在网上找了一个工具类,在项目测试,使用。
直接贴上tgz解压代码。
public class PackDecompressor { public static int BUFFER_SIZE = 2048; public static void main(String[] args) throws Exception { unTarGZ("D:\\test\\payresult_fix_.txt.tgz", "D:\\test\\aaa"); } public static void unTarGZ(String file, String destDir) throws Exception { File tarFile = new File(file); unTarGZ(tarFile, destDir); } public static void unTarGZ(File tarFile, String destDir) throws Exception { if (StringUtils.isBlank(destDir)) { destDir = tarFile.getParent(); } destDir = destDir.endsWith(File.separator) ? destDir : destDir + File.separator; unTar(new GzipCompressorInputStream(new FileInputStream(tarFile)), destDir); } private static void unTar(InputStream inputStream, String destDir) throws Exception { TarArchiveInputStream tarIn = new TarArchiveInputStream(inputStream, BUFFER_SIZE); TarArchiveEntry entry = null; try { while ((entry = tarIn.getNextTarEntry()) != null) { if (entry.isDirectory()) {//是目录 createDirectory(destDir, entry.getName());//创建空目录 } else {//是文件 File tmpFile = new File(destDir + File.separator + entry.getName()); createDirectory(tmpFile.getParent() + File.separator, null);//创建输出目录 OutputStream out = null; try { out = new FileOutputStream(tmpFile); int length = 0; byte[] b = new byte[2048]; while ((length = tarIn.read(b)) != -1) { out.write(b, 0, length); } } finally { IOUtils.closeQuietly(out); } } } } catch (Exception e) { e.printStackTrace(); throw e; } finally { IOUtils.closeQuietly(tarIn); } } public static void createDirectory(String outputDir, String subDir) { File file = new File(outputDir); if (!(subDir == null || subDir.trim().equals(""))) {//子目录不为空 file = new File(outputDir + File.separator + subDir); } if (!file.exists()) { file.mkdirs(); } } }
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/218091.html原文链接:https://javaforall.net
