java 解压tar.gz示例代码
来源:互联网 发布:mac jenkins 如何重启 编辑:程序博客网 时间:2024/05/18 15:29
/** * Tar文件解压方法 * * @param tarGzFile * 要解压的压缩文件名称(绝对路径名称) * @param destDir * 解压后文件放置的路径名(绝对路径名称) * @return 解压出的文件列表 */public static List<String> deCompressGZipFile(String tarGzFile, String destDir) { List<String> fileList = new ArrayList<String>(); OutputStream out = null; // 建立输出流,用于将从压缩文件中读出的文件流写入到磁盘 FileInputStream fis = null; // 建立输入流,用于从压缩文件中读出文件 GZIPInputStream gis = null; TarArchiveInputStream taris = null; TarArchiveEntry entry = null; TarArchiveEntry[] subEntries = null; File entryFile = null; File subEntryFile = null; String entryFileName = null; int entryNum = 0; try { fis = new FileInputStream(tarGzFile); gis = new GZIPInputStream(fis); taris = new TarArchiveInputStream(gis); while ((entry = taris.getNextTarEntry()) != null) { entryFileName = destDir + FILE_PATH_SEPARATOR + entry.getName(); entryFile = new File(entryFileName); entryNum++; if (entry.isDirectory()) { if (!entryFile.exists()) { entryFile.mkdir(); } subEntries = entry.getDirectoryEntries(); for (int i = 0; i < subEntries.length; i++) { try { subEntryFile = new File(entryFileName + FILE_PATH_SEPARATOR + subEntries[i].getName()); fileList.add(entryFileName + FILE_PATH_SEPARATOR + subEntries[i].getName()); out = new FileOutputStream(subEntryFile); byte[] buf = new byte[1024]; int len = 0; while ((len = taris.read(buf)) != -1) { out.write(buf, 0, len); } } catch (Exception e) { log.error("deCompressing file failed:" + subEntries[i].getName() + "in" + tarGzFile); } finally { out.close(); out = null; } } } else { fileList.add(entryFileName); out = new FileOutputStream(entryFile); try { byte[] buf = new byte[1024]; int len = 0; while ((len = taris.read(buf)) != -1) { out.write(buf, 0, len); } } catch (Exception e) { log.error("deCompressing file failed:" + entryFileName + "in" + tarGzFile); } finally { out.close(); out = null; } } } if (entryNum == 0) { log.warn("there is no entry in " + tarGzFile); } } catch (Exception e) { log.warn("decompress failed", e); } finally { if (taris != null) { try { taris.close(); } catch (Exception ce) { taris = null; } } if (gis != null) { try { gis.close(); } catch (Exception ce) { gis = null; } } if (fis != null) { try { fis.close(); } catch (Exception ce) { fis = null; } } if (out != null) { try { out.close(); } catch (Exception ce) { out = null; } } } return fileList;}
阅读全文
0 0
- java 解压tar.gz示例代码
- java 解压 .tar.gz
- JAVA 解压tar.gz格式文件
- java 压缩/解压【tar.gz】
- 解压.tar.gz文件
- 解压tar.gz
- 解压.tar.gz文件
- 解压*.tar.gz文件
- tar.gz解压,压缩
- 解压tar.gz文件
- linux解压.tar.gz
- .tar.gz的解压
- tar.gz 解压指令
- tar.gz解压命令
- 解压tar.gz,tar.bz2,tar.xz
- java解压xx.tar.gz格式的压缩包(转)
- java 压缩以及解压文件,tar,zip,gz(gizp)
- java tar.gz 格式多文件打包压缩与解压
- java07_几张图了解count(*)与count(列名)的区别
- TLS/SSL 协议详解 (24) CFCA蛋疼的事
- 文件处理(压缩与解压)
- Pointers on C——8 Arrays.22
- K-means聚类算法详解
- java 解压tar.gz示例代码
- dex2oat
- Myeclipse的web项目导入eclipse中部署
- 动脉硬化检测分析部分指标说明
- Git更新远程仓库代码到本地 git fetch
- Java-DatabaseConnectionPool工具类
- Python 运行出现Process finished with exit code -1073741515 (0xC0000135)错误
- win7下python3.6通过pip安装scipy报错的解决办法
- 概率背景知识