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
相关推荐
终极定律4 分钟前
qt:输入控件操作JenKinJia19 分钟前
Windows10配置C++版本的Kafka,并进行发布和订阅测试煤炭里de黑猫21 分钟前
Lua C API :lua_insert 函数详解笨鸟笃行23 分钟前
爬虫第七篇数据爬取及解析s_fox_23 分钟前
Nginx Embedded Variables 嵌入式变量解析(4)编程乐趣24 分钟前
一文掌握DeepSeek本地部署+Page Assist浏览器插件+C#接口调用+局域网访问!全攻略来了!java1234_小锋29 分钟前
一周学会Flask3 Python Web开发-response响应格式Jelena1577958579229 分钟前
使用Java爬虫获取1688 item_get_company 接口的公司档案信息java1234_小锋31 分钟前
一周学会Flask3 Python Web开发-flask3模块化blueprint配置数据小小爬虫32 分钟前
Jsoup解析商品详情时,如何确保数据准确性?