import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.springframework.stereotype.Component;
import java.io.*;
import java.util.zip.GZIPInputStream;
@Slf4j
@Component
public class TarGzExtractorJie {
public static void extractTarGzFile(String tarGzFilePath, String destinationFolder) {
try {
FileInputStream fileInputStream = new FileInputStream(tarGzFilePath);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
GZIPInputStream gzipInputStream = new GZIPInputStream(bufferedInputStream);
TarArchiveInputStream tarInputStream = new TarArchiveInputStream(gzipInputStream);
TarArchiveEntry entry;
while ((entry = tarInputStream.getNextTarEntry()) != null) {
if (entry.isDirectory()) {
new File(destinationFolder, entry.getName()).mkdirs();
} else {
File outputFile = new File(destinationFolder, entry.getName());
File parentDirectory = outputFile.getParentFile();
if (!parentDirectory.exists()) {
parentDirectory.mkdirs();
}
OutputStream outputFileStream = new FileOutputStream(outputFile);
byte[] buffer = new byte[1024];
int length;
while ((length = tarInputStream.read(buffer)) != -1) {
outputFileStream.write(buffer, 0, length);
}
outputFileStream.close();
}
}
tarInputStream.close();
gzipInputStream.close();
bufferedInputStream.close();
fileInputStream.close();
log.info("文件解压完成!");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String sourceFile = "D:\\tar\\6.tar.gz";
String destinationFolder = "D:\\TestDownload\\jie";
try {
extractTarGzFile(sourceFile, destinationFolder);
System.out.println("Extraction completed successfully.");
} catch (Exception e) {
System.out.println("Error occurred during extraction: " + e.getMessage());
}
}
}
tar.gz文件解压缩
gb42152872023-10-18 23:00
相关推荐
SimonKing几秒前
SpringBoot集成:5分钟实现HTML转PDF功能wuxuanok12 分钟前
苍穹外卖 —— 公共字段填充偷光17 分钟前
浏览器中的隐藏IDE: Console (控制台) 面板串串店藕片打孔员41 分钟前
把List<T>构建一颗树封装工具类用户0332126663671 小时前
自动创建 Word 文档——Java 实现Cikiss1 小时前
图解 bulkProcessor(调度器 + bulkAsync() + Semaphore)LL_break1 小时前
线程1——javaEE 附面题王中阳Go1 小时前
面试官:“聊聊最复杂的项目?”90%的人开口就凉!我面过最牛的回答,就三句话玉衡子1 小时前
一、Java类加载机制virtuousOne1 小时前
线程池详解