java中解压tar.gz文件
生活随笔
收集整理的這篇文章主要介紹了
java中解压tar.gz文件
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在開發(fā)中我們經(jīng)常需要對(duì)gz文件進(jìn)行解壓縮,在java中解壓gz文件還是比較繁瑣的,為此寫了一個(gè)工具類方便需要的時(shí)候可以直接拿過(guò)來(lái)用。代碼如下:
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 要解壓的壓縮文件名稱(絕對(duì)路徑名稱)* @param destDir 解壓后文件放置的路徑名(絕對(duì)路徑名稱)當(dāng)路徑不存在,會(huì)自動(dòng)創(chuàng)建* @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文件進(jìn)行遞歸解壓if (entryFile.getName().endsWith(".gz")) {deCompressGZipFile(entryFile.getPath(), destDir);}}}//如果需要?jiǎng)h除之前解壓的gz文件,在這里進(jìn)行} catch (Exception e) {LOGGER.warn("decompress failed", e);}}public static void checkFileExists(File file) {//判斷是否是目錄if (file.isDirectory()) {if (!file.exists()) {file.mkdir();}} else {//判斷父目錄是否存在,如果不存在,則創(chuàng)建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>?
總結(jié)
以上是生活随笔為你收集整理的java中解压tar.gz文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: CISP、CISSP、CISA、CISP
- 下一篇: 查看并杀死Tomcat进程