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;}