gpt4 book ai didi

java - 数据提取 Tar 和 7z

转载 作者:行者123 更新时间:2023-12-02 09:12:22 24 4
gpt4 key购买 nike

我有一个 .tar 文件,其中包含许多文件夹和子文件夹。在这些许多文件夹中,有 .7z 文件和其他文件。我想搜索这些文件夹/子文件夹并找到 .7z 文件(将它们分配给一个数组?)并将它们提取到各自的位置。

我正在使用 Apache Commons:1) org.apache.commons.compress.archivers.sevenz提供使用 7z 格式读取和写入文件的类。2) org.apache.commons.compress.archivers.tar
提供用于使用 TAR 格式读写文件的流类。

  1. 步骤我想提取 .tar 文件
  2. 步骤我想递归地遍历提取的 .tar 文件夹及其子文件夹并找到 .7z 文件。
  3. 在第 3 步中,我想向数组提供我找到的 .7z 文件数组,并将它们一一提取到各自的位置。

我在第 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 中引用 []matches。我没有正确使用它。我只想为 .7z 文件匹配创建一个数组 []matches 。每次找到 .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();
}
  1. 递归地执行:
        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();
}

最佳答案

我借此机会使用了较新的路径和文件。 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();
}
});

关于java - 数据提取 Tar 和 7z,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59305412/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com