0
我正在使用TarInputStream()来读取tar文件的内容并将其存储在特定位置的所有文件。我想创建一个名称与tar文件类似的文件夹,并将所有文件保存在该文件夹中。例如,如果我有一个包含文件test1和test2的tar文件test.tar.gz,我的代码应该通过名称test创建一个文件夹,并将tar文件解压到该文件夹。使用压缩文件的名称命名Zip文件夹
这是我写的代码。
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();
}
字符串tarFileName = “/ TMP/lpa_1_454_20111117011749.tar.gz”; File dest = new File(“/ tmp/test /”); 所以,我想要在文件夹名称lpa_1_454_20111117011749下提取所有文件。但是我无法提取这个名字! – RFT 2011-12-27 20:47:27
你的代码结合这个答案应该可以达到你的期望。你能否详细说明“无法提取该名称”的含义?假设'lpa _...'是TAR文件中的文件名,应该由'tarEntry.getName()'返回,并导致'/ tmp/test/tmp/lpa _...“的'destPath'。当然,这里有第二个“tmp”,但这是做这个的唯一“安全”方式 - 除非您提供从TAR文件名剥离已知数量的路径组件的选项 - 但这会让一些漂亮关于您期望处理的每个TAR文件的严重假设。 – ziesemer 2011-12-27 20:52:50
lpa_ ..是我从中提取文件的tar文件。我可以使用tarEntry.getName()提取文件的名称,但不能提取tar文件的名称lpa _... – RFT 2011-12-27 20:57:35