日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java中解压tar.gz文件

發布時間:2024/3/24 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java中解压tar.gz文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在開發中我們經常需要對gz文件進行解壓縮,在java中解壓gz文件還是比較繁瑣的,為此寫了一個工具類方便需要的時候可以直接拿過來用。代碼如下:

package com.eggsl.utils;import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import org.apache.commons.compress.utils.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory;import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.zip.GZIPInputStream;/*** @author: xiaobaitu88* @Date: 2019/10/23 21:19* @Description:*/ public class FileUtils {private static final Logger LOGGER = LoggerFactory.getLogger(FileUtils.class);public static void main(String[] args) {deCompressGZipFile("D:\\hello2.tar.gz", "D:\\ssl");}/*** Tar文件解壓方法** @param tarGzFile 要解壓的壓縮文件名稱(絕對路徑名稱)* @param destDir 解壓后文件放置的路徑名(絕對路徑名稱)當路徑不存在,會自動創建* @return 解壓出的文件列表*/public static void deCompressGZipFile(String tarGzFile, String destDir) {// 建立輸出流,用于將從壓縮文件中讀出的文件流寫入到磁盤TarArchiveEntry entry = null;TarArchiveEntry[] subEntries = null;File subEntryFile = null;try (FileInputStream fis = new FileInputStream(tarGzFile);GZIPInputStream gis = new GZIPInputStream(fis);TarArchiveInputStream taris = new TarArchiveInputStream(gis);) {while ((entry = taris.getNextTarEntry()) != null) {StringBuilder entryFileName = new StringBuilder();entryFileName.append(destDir).append(File.separator).append(entry.getName());File entryFile = new File(entryFileName.toString());if (entry.isDirectory()) {if (!entryFile.exists()) {entryFile.mkdir();}subEntries = entry.getDirectoryEntries();for (int i = 0; i < subEntries.length; i++) {try (OutputStream out = new FileOutputStream(subEntryFile)) {subEntryFile = new File(entryFileName + File.separator + subEntries[i].getName());IOUtils.copy(taris, out);} catch (Exception e) {LOGGER.error("deCompressing file failed:" + subEntries[i].getName() + "in" + tarGzFile);}}} else {checkFileExists(entryFile);OutputStream out = new FileOutputStream(entryFile);IOUtils.copy(taris, out);out.close();//如果是gz文件進行遞歸解壓if (entryFile.getName().endsWith(".gz")) {deCompressGZipFile(entryFile.getPath(), destDir);}}}//如果需要刪除之前解壓的gz文件,在這里進行} catch (Exception e) {LOGGER.warn("decompress failed", e);}}public static void checkFileExists(File file) {//判斷是否是目錄if (file.isDirectory()) {if (!file.exists()) {file.mkdir();}} else {//判斷父目錄是否存在,如果不存在,則創建if (file.getParentFile() != null && !file.getParentFile().exists()) {file.getParentFile().mkdirs();}try {file.createNewFile();} catch (IOException e) {e.printStackTrace();}}} }

工具類依賴:

<dependency><groupId>org.apache.commons</groupId><artifactId>commons-compress</artifactId><version>1.19</version> </dependency>

?

總結

以上是生活随笔為你收集整理的java中解压tar.gz文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。