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
相关推荐
芊寻(嵌入式)15 分钟前
C转C++学习笔记--基础知识摘录总结WaaTong16 分钟前
《重学Java设计模式》之 原型模式m0_7430484416 分钟前
初识Java EE和Spring BootAskHarries18 分钟前
Java字节码增强库ByteBuddy一颗松鼠23 分钟前
JavaScript 闭包是什么?简单到看完就理解!有梦想的咸鱼_25 分钟前
go实现并发安全hashtable 拉链法海阔天空_201331 分钟前
Python pyautogui库:自动化操作的强大工具天下皆白_唯我独黑38 分钟前
php 使用qrcode制作二维码图片小灰灰__38 分钟前
IDEA加载通义灵码插件及使用指南夜雨翦春韭42 分钟前
Java中的动态代理