我正在使用 TarInputStream() 读取 tar 文件的内容并将其中的所有文件存储在特定位置。我想创建一个名称类似于 tar 文件的文件夹,并将我的所有文件保存在该文件夹中。例如,如果我有一个包含文件 test1 和 test2 的 tar 文件 test.tar.gz,我的代码应该创建一个名为 test 的文件夹并将 tar 文件解压缩到该文件夹。
这是我写的代码。
TarInputStream tin = new TarInputStream(new GZIPInputStream(new FileInputStream(new File(tarFileName))));
TarEntry tarEntry = tin.getNextEntry();
while (tarEntry != null) {// create a file with the same name as tar entry
File destPath = new File(dest.toString() + File.separatorChar
+ tarEntry.getName());
FileOutputStream fout = new FileOutputStream(destPath);
tin.copyEntryContents(fout);
fout.close();
///services/advert/lpa/dimenions/data/advertiser/
Path inputFile = new Path(destPath.getAbsolutePath());
//To remove the local files set the flag to true
fs.copyFromLocalFile(inputFile, filenamepath);
tarEntry = tin.getNextEntry();
}