我有一个.tar文件,其中包含许多文件夹和子文件夹。在这许多文件夹中,除了其他文件之外,还有.7z文件。我想搜索这些文件夹/子文件夹并找到.7z文件(将它们分配给数组?)并将它们提取到各自的位置。
我正在使用Apache : 1) org.apache.commons.compress.archivers.sevenz提供了使用7z格式读取和写入档案的类。2) org.apache.commons.compress.archivers.tar
提供流类,用于使用TAR格式读取和写入档案。
我在3.数组调用/分配的步骤中遇到了问题:/您能帮忙吗?(非常感谢:)
/**
* uncompresses .tar file
* @param in
* @param out
* @throws IOException
*/
public static void decompressTar(String in, File out) throws IOException {
try (TarArchiveInputStream tin = new TarArchiveInputStream(new FileInputStream(in))){
TarArchiveEntry entry;
while ((entry = tin.getNextTarEntry()) != null) {
if (entry.isDirectory()) {
continue;
}
File curfile = new File(out, entry.getName());
File parent = curfile.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
IOUtils.copy(tin, new FileOutputStream(curfile));
}
}
}
/**
* uncompresses .7z file
* @param in
* @param destination
* @throws IOException
*/
public static void decompressSevenz(String in, File destination) throws IOException {
//@SuppressWarnings("resource")
SevenZFile sevenZFile = new SevenZFile(new File(in));
SevenZArchiveEntry entry;
while ((entry = sevenZFile.getNextEntry()) != null){
if (entry.isDirectory()){
continue;
}
File curfile = new File(destination, entry.getName());
File parent = curfile.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
FileOutputStream out = new FileOutputStream(curfile);
byte[] content = new byte[(int) entry.getSize()];
sevenZFile.read(content, 0, content.length);
out.write(content);
out.close();
}
sevenZFile.close();
}
public void run()
{
//1) uncompress .tar
try {
JThreadTar.decompressTar(RECURSIVE_DIRECTORY_PATH, new File(RECURSIVE_DIRECTORY));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//2) go through the extracted .tar file directory and look for .7z (recursively?)
File[] files = new File(RECURSIVE_DIRECTORY).listFiles();
for (File file : files) {
if (file.isDirectory()) {
File[] matches = file.listFiles(new FilenameFilter()
{
public boolean accept(File dir, String name)
{
return name.endsWith(".7z");
}
});
for (File element: matches) {
System.out.println(element);
}
}
else {
continue;
}
}
//3) Feed the array above to decompressSevenz method
for (int i = 0; i < matches.length; i++)
{
if (matches[i].isFile())
{
try {
JThreadTar.decompressSevenz(matches[i].toString(), new File(RECURSIVE_DIRECTORY));
}
catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
}我的问题是:我不能引用步骤3中的[]匹配,我没有正确地使用它。我只想为.7z文件匹配创建一个数组[]匹配。每次找到一个.7z时,我都想将它添加到这个数组中。在第3步中,我想将每个.7z提取到它的相对位置。
我走得更远了一点:
//1) uncompress .tar
try {
JThreadTar.decompressTar(RECURSIVE_DIRECTORY_PATH, new File(RECURSIVE_DIRECTORY));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//2) go through the extracted .tar file directory and look for .7z (recursively?)
File dir = new File(RECURSIVE_DIRECTORY);
File[] dirFiles = dir.listFiles();
ArrayList<File> matches2 = new ArrayList<File>();
for (File file : dirFiles) {
if (file.isDirectory()) {
File[] matches = dir.listFiles(new FilenameFilter()
{
public boolean accept(File dir, String name)
{
return name.endsWith(".7z");
}
});
matches2.addAll(Arrays.asList(matches));
}
else if (file.isFile()) {
if (file.getName().endsWith(".7z")){
matches2.add(file);
};
}
};
//3) Feed the arraylist above to decompressSevenz method
for (int counter = 0; counter < matches2.size(); counter++) {
if (matches2.get(counter).isFile())
{
try {
JThreadTar.decompressSevenz(matches2.get(counter).toString(), new File(RECURSIVE_DIRECTORY));
}
catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
}下面是@Joop Eggen第2步和第3步的最后形式
Path topDir = Paths.get(RECURSIVE_DIRECTORY);
try {
Files.walk(topDir)
.filter(path -> path.getFileName().toString().endsWith(".7z"))
.forEach(path -> {
try {
JThreadTar.decompressSevenz(path.toString(), topDir.toFile());
} catch (IOException e2) {
e2.printStackTrace();
}
});
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}recursively:
Path toptopDir = Paths.get(RECURSIVE_DIRECTORY_PATH);
try {
Files.walk(toptopDir)
.filter(path -> path.getFileName().toString().endsWith(".tar"))
.forEach(path -> {
try {
JThreadTar.decompressTar(RECURSIVE_DIRECTORY_PATH, new File(RECURSIVE_DIRECTORY));
} catch (IOException e2) {
e2.printStackTrace();
}
});
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}发布于 2019-12-12 15:54:17
我利用这个机会使用了更新的路径和文件。Files.listFiles()可能返回null。而Arrays.asList等的使用会导致大量的数据。
所有这些都将简化为:
Path topDir = Paths.get(RECURSIVE_DIRECTORY);
Files.walk(topDir)
.filter(path -> path.getFileName().toString().endsWith(".7z"))
.forEach(path -> {
try {
JThreadTar.decompressSevenz(path.toString(), topDir.toFile());
} catch (IOException e2) {
e2.printStackTrace();
}
});https://stackoverflow.com/questions/59305412
复制相似问题