- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 .tar 文件,其中包含许多文件夹和子文件夹。在这些许多文件夹中,有 .7z 文件和其他文件。我想搜索这些文件夹/子文件夹并找到 .7z 文件(将它们分配给一个数组?)并将它们提取到各自的位置。
我正在使用 Apache Commons: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 中引用 []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();
}
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/
我正在使用 tf 函数列出 tar.gz 文件的内容。它非常大~1 GB。大约有 1000 个文件以年/月/日文件结构组织。 列表操作需要一些时间。似乎列表应该很快。谁能告诉我内部原理? 谢谢- 最佳
如何将信息通过管道传输到 tar 中并指定文件名? 最佳答案 类似于: tar cfz foo.tgz --files-from=- 但请记住,这不适用于所有可能的文件名;您应该考虑使用 --null
我总是用 tar czf file.tar.gz dirname而不是 tar -czf file.tar.gz dirname (没有 - )因为更快 我知道......从文档中,我所做的是错误的。
我有一个小型的 bash 单行代码,可以从各种来源下载存档列表。以下是包含该列表的文件示例: http://this-is-url1/url1.tar.gz http://www.this-is-ur
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
假设我在服务器 A 上使用 tar -czvf subdir.tgz subdirectory/* 创建了一个 tar 存档。 .如果我在服务器 B 上有一个并行结构,我已将此 tarball 复制到
我看到Linux tar有一个选项--overwrite。但是覆盖似乎是默认设置。而且,指定tar --no-overwrite不会更改此行为,因为信息文件似乎暗示了这一点。 那么,该选项实际上是做什
我需要创建一个任意大的 tarfile 用于测试,但不希望它写入磁盘。 最简单的方法是什么? 最佳答案 您可以轻松地使用 python 生成这样的 tarfile: mktar.py: #!/usr/
[root@c0002242 lfeng]# tar -zxvf/opt/test/ALLscripts.tar.gz -C/opt/test1 tar:这看起来不像 tar 存档 tar:跳到下一个
tar -xvzf $文件名.tar.gz || { 退出 $?; } 这里我的脚本将退出并显示错误代码 141。我使用的是 Fedora Core 6 和 tar 版本 1.15 这种情况不会一直发
我无法运行 docker-compose up 或 docker-compose build --no-cache。两者都以 ERROR: Error processing tar file(arch
我对 linux 平台非常陌生,我想为 ubuntu 提取 gnutls。如果我做, $ls 然后,它将在下面显示这些文件。 gnutls-3.2.1.tar.lz gnutls-3.2.1.tar.
我有一个巨大的 tarbell 存档,其中包含过大或损坏的 error_log,导致存档在尝试解压缩时挂起。有没有办法在解压缩或提取存档之前从存档中删除它,而无需在 Mac OS X 终端上提取该特定
我正在尝试针对特定用例评估 Artifactory,并且正在寻找可以支持将 Artifactory 用于文件存储库的文档。 我找到的只是一个用于部署 Artifactory 的文档,这些文档已存档在
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我想写一个有两个参数的脚本1. 父目录名称(例如 mainFolder)2.要创建的tar名称 父目录将包含子文件夹(文件夹 1、文件夹 2、文件夹 3 等)。我需要一个脚本来创建“mainFolde
假设我在 /tmp 中。我想压缩 /tmp/aaa/123,/tmp/bbb/222。我使用的命令是:tar -vczf/tmp/my.tar.gz/tmp/aaa/123/tmp/bbb/222。这
我有一个bzip2ed tar 文件和一个包含文件列表的文本文件。我想从 tar 中提取文本文件中列出的文件,将它们添加到一个新的 tar 中,然后从第一个 tar 中删除它们。 例如,如果我有这样一
本文实例讲述了Python打包文件夹的方法。分享给大家供大家参考,具体如下: 1、zip ?
我输入这个命令su -c busybox tar -xvf/storage/emulated/0/Download/andrax.r5-build5.tar.xz -C/data/data/com.t
我是一名优秀的程序员,十分优秀!