标签: apache-commons-compress

使用Commons Compress将目录压缩到tar.gz

我正在使用commons压缩库来创建目录的tar.gz.我有一个目录结构如下.

parent/
    child/
        file1.raw
        fileN.raw
Run Code Online (Sandbox Code Playgroud)

我正在使用以下代码进行压缩.它运行良好,没有例外.但是,当我尝试解压缩tar.gz时,我得到一个名为"childDirToCompress"的文件.它的大小正确,因此文件在tarring过程中显然已经相互附加.所需的输出将是一个目录.我无法弄清楚我做错了什么.任何有智慧的公共审判者都可以让我走上正确的道路吗?

CreateTarGZ() throws CompressorException, FileNotFoundException, ArchiveException, IOException {
            File f = new File("parent");
            File f2 = new File("parent/childDirToCompress");

            File outFile = new File(f2.getAbsolutePath() + ".tar.gz");
            if(!outFile.exists()){
                outFile.createNewFile();
            }
            FileOutputStream fos = new FileOutputStream(outFile);

            TarArchiveOutputStream taos = new TarArchiveOutputStream(new GZIPOutputStream(new BufferedOutputStream(fos)));
            taos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR); 
            taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
            addFilesToCompression(taos, f2, ".");
            taos.close();

        }

        private static void addFilesToCompression(TarArchiveOutputStream taos, File file, String dir) throws IOException{
            taos.putArchiveEntry(new TarArchiveEntry(file, dir));

            if (file.isFile()) {
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
                IOUtils.copy(bis, taos);
                taos.closeArchiveEntry();
                bis.close();
            } …
Run Code Online (Sandbox Code Playgroud)

java compression tar apache-commons apache-commons-compress

11
推荐指数
3
解决办法
3万
查看次数

使用Commons-compression读取Java中的tar.gz

好的,我想阅读tar.gz文件(或xy)的内容,但这是一回事.我正在做的或多或少是这样的:

TarArchiveInputStream tarInput = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream("c://temp//test.tar.gz")));
TarArchiveEntry currentEntry = tarInput.getNextTarEntry();
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
while (currentEntry != null) {
    File f = currentEntry.getFile();
    br = new BufferedReader(new FileReader(f));
    System.out.println("For File = " + currentEntry.getName());
    String line;
    while ((line = br.readLine()) != null) {
        System.out.println("line="+line);
    }
}
if (br!=null) {
    br.close();
}
Run Code Online (Sandbox Code Playgroud)

但是当我调用getFile方法时,我得到null TarArchiveEntry.
我正在使用Apache commons compress 1.8.1

java apache-commons-compress

8
推荐指数
1
解决办法
8803
查看次数

apache commons使用7zip压缩

我试图使用下面的代码,我从apache commons压缩示例网页创建一个zip文件使用sevenZ类希望它比常规java zip压缩更快.这就是我的代码

public static void main(String[] args) {
    // TODO Auto-generated method stub
    try{
    BufferedInputStream instream = new BufferedInputStream(new FileInputStream("c:/temp/test.txt"));

    SevenZOutputFile sevenZOutput = new SevenZOutputFile(new File("c:/temp/7ztest.zip"));
    SevenZArchiveEntry entry = sevenZOutput.createArchiveEntry(new File("c:/temp/test.txt"),"blah.txt");
    sevenZOutput.putArchiveEntry(entry);
    byte[] buffer = new byte[1024];
    int len;
    while ((len = instream.read(buffer)) > 0) {sevenZOutput.write(buffer, 0, len);}

    sevenZOutput.closeArchiveEntry();
    sevenZOutput.close();
    instream.close();
    }catch(IOException ioe) {
        System.out.println(ioe.toString());

    }
}
Run Code Online (Sandbox Code Playgroud)

我得到这个看起来如此无关的错误

线程"main"中的异常java.lang.NoClassDefFoundError:org.tukaani.xz.FilterOptions at java.lang.J9VMInternals.verifyImpl(Native Method)at java.lang.J9VMInternals.verify(J9VMInternals.java:93)

我有导入的apache包

import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry; import org.apache.commons.compress.archivers.sevenz.SevenZOutputFile;

但不确定org.tukaani.xz.FilterOptions是什么,它看起来不像是apace commons compress的一部分.有什么想法吗?

compression apache zip 7zip apache-commons-compress

8
推荐指数
1
解决办法
6938
查看次数

Gradle不包含可选的依赖项

我有一个项目,它将apache-compress库作为编译时依赖项.该库似乎使用Maven,并且具有一个POM文件,其依赖项设置为"可选".以下是POM文件的相关部分:

<dependency>
  <groupId>org.tukaani</groupId>
  <artifactId>xz</artifactId>
  <version>1.5</version>
  <optional>true</optional>
</dependency>
Run Code Online (Sandbox Code Playgroud)

Gradle似乎没有将这个库包含在我的项目中,我猜它是因为"可选"属性.有没有办法告诉Gradle包含这个依赖项而不自己明确地包含xz库?

这是我的Gradle依赖声明: compile group: 'org.apache.commons', name:'commons-compress', version:'1.8.1'

java dependency-management gradle maven apache-commons-compress

7
推荐指数
1
解决办法
2535
查看次数

如何将 ArchiveEntry 转换为 InputStream?

我正在阅读tar.gz档案使用

ArchiveEntry entry = tarArchiveInputStream.getNextEntry();

问题:如何将此ArchiveEntry转换为 ,InputStream以便我可以实际读取文件并将其处理为String

java apache-commons-compress

7
推荐指数
1
解决办法
3213
查看次数

用于嵌套ZIP文件处理的Java实用程序库

我知道Oracle 在其网站上注明了ZIP/GZIP文件压缩器/解压缩器方法.但我有一个场景,我需要扫描并找出是否涉及任何嵌套的ZIP/RAR.例如,以下情况:

-MyFiles.zip
   -MyNestedFiles.zip
        -MyMoreNestedFiles.zip
           -MoreProbably.zip
        -Other_non_zips
   -Other_non_zips
-Other_non_zips
Run Code Online (Sandbox Code Playgroud)

我知道apache commons压缩包和java.util.zip是广泛使用的包,其中commons压缩实际上迎合了java.util.zip中缺少的功能,例如在进行压缩时的一些字符设置.但我不确定的是通过嵌套zip文件递归的实用程序和SO提供的答案并不是很好的例子.我尝试了以下代码(我从Oracle博客获得),但是我怀疑,嵌套目录递归失败,因为它无法找到文件:

public static void processZipFiles(String pathName) throws Exception{
        ZipInputStream zis  = null;
        InputStream  is = null;
        try {
          ZipFile zipFile = new ZipFile(new File(pathName));
          String nestPathPrefix = zipFile.getName().substring(0, zipFile.getName().length() -4);
          for(Enumeration e = zipFile.entries(); e.hasMoreElements();){
           ZipEntry ze = (ZipEntry)e.nextElement();
            if(ze.getName().contains(".zip")){
              is = zipFile.getInputStream(ze);
              zis = new ZipInputStream(is);
              ZipEntry zentry = zis.getNextEntry();

              while (zentry!=null){
                  System.out.println(zentry.getName());
                  zentry = zis.getNextEntry();
                  ZipFile nestFile = new ZipFile(nestPathPrefix+"\\"+zentry.getName());
                  if (zentry.getName().contains(".zip")) {
                      processZipFiles(nestPathPrefix+"\\"+zentry.getName());
                  }
              }
              is.close(); …
Run Code Online (Sandbox Code Playgroud)

java recursion zip apache-commons-compress apache-tika

7
推荐指数
1
解决办法
1897
查看次数

如何使用 Apache Commons Compress 将文件附加到 .tar 存档?

我阅读了如何将文件附加到 java 中的 tar 存档?,将文件附加到存档而不读取/重写整个存档并将条目添加到 tar 文件而不覆盖其现有内容但没有给出好的答案。此外,我没有足够的声誉发表评论。所以我在这里创建了一个新问题。

有没有办法在 tar 存档中附加文件?如果文件已经存在,我想替换它。

我已经开始编写以下方法,但是当添加文件时,它会删除存档内容。我在apache compress 网站上没有找到任何示例。

    static final Logger LOG = Logger.getLogger(ShellClient.class);

public void appendFileInTarArchive(String tarPath, String tarFileName, String file2WriteName, String file2WriteContent) throws IOException {
    if (tarPath == null || tarFileName == null || tarFileName.isEmpty()) {
        LOG.warn("The path or the name of the tar archive is null or empty.");
        return;
    }
    final File tarFile = new File(tarPath, tarFileName);
    final File fileToAdd = new File(tarPath, file2WriteName); …
Run Code Online (Sandbox Code Playgroud)

java compression tar archive apache-commons-compress

5
推荐指数
1
解决办法
4655
查看次数

Apache Commons 压缩:打开 .tar.gz

我正在开发一个可以从 tar.gz 文件中获取信息的软件,我正在使用 Apache commons-compress 库。但我收到以下错误:

Caused by: java.lang.IllegalArgumentException: Invalid byte 4 at offset 0 in 'O?!?C' len=8
at org.apache.commons.compress.archivers.tar.TarUtils.parseOctal(TarUtils.java:134)
at org.apache.commons.compress.archivers.tar.TarUtils.parseOctalOrBinary(TarUtils.java:166)
at org.apache.commons.compress.archivers.tar.TarArchiveEntry.parseTarHeader(TarArchiveEntry.java:953)
at org.apache.commons.compress.archivers.tar.TarArchiveEntry.parseTarHeader(TarArchiveEntry.java:940)
at org.apache.commons.compress.archivers.tar.TarArchiveEntry.<init>(TarArchiveEntry.java:324)
at org.apache.commons.compress.archivers.tar.TarArchiveInputStream.getNextTarEntry(TarArchiveInputStream.java:274)
... 2 more
Run Code Online (Sandbox Code Playgroud)

使用的示例 tar.gz 文件是 eclipse-jee-luna-SR1-linux-gtk-x86_64.tar.gz

这是使用 lib 的类:

public class TarGzBuildAdapter extends BuildAdapter {
    public TarGzBuildAdapter(File build) {
        super(build);
    }

    @Override
    public List<ArtifactInfo> getArtifactInfos() throws IOException {
        TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(
                new FileInputStream(this.build));
        TarArchiveEntry tarArchiveEntry;
        List<ArtifactInfo> artifactInfos = new LinkedList<ArtifactInfo>();

        while ((tarArchiveEntry = tarArchiveInputStream.getNextTarEntry()) != null) { …
Run Code Online (Sandbox Code Playgroud)

java apache-commons-compress

5
推荐指数
1
解决办法
5207
查看次数

无需提取即可预览 .7z 内容和子文件夹

我想预览 .7z 的内容而不用 Java 提取它,所以我尝试使用 Apache Commons Compress:

public static void main(String[] args) {
    try {
        SevenZFile sevenZFile = new SevenZFile(new File("C://test.7z"));
        SevenZArchiveEntry entry;
        while ((entry = sevenZFile.getNextEntry()) != null) {
            System.out.println("Name: " + entry.getName());
            System.out.println("Size : " + entry.getSize());
            System.out.println("--------------------------------");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

它可以工作,但我还想将文件预览到其他 7z 或原始 7z 的 zip 文件中:

test.7z
--- file1.txt
--- otherInner.7z
    ---- innerFile1.txt
    ---- innerFile2.txt
Run Code Online (Sandbox Code Playgroud)

有没有办法在不提取 7z 的情况下做到这一点?谢谢你的帮助。

java 7zip apache-commons-compress

5
推荐指数
1
解决办法
1300
查看次数

java.io.IOException:在解析头文件时检测到错误,在 Java 中解压 .tgz 文件

我正在使用 apache commons 来解压 .tgz 文件。我从压缩库中收到错误消息。我已经尝试过压缩 1.9 和 1.8.1 版本,但我仍然遇到相同的错误。

这仅发生在某些文件上,但问题是当我手动下载文件并验证它时,没有问题。

$ gunzip -c foo.tgz | tar t > /dev/null
$ 
Run Code Online (Sandbox Code Playgroud)

但是我看到这个堆栈跟踪来自公共库。

Severe:   java.io.IOException: Error detected parsing the header
at org.apache.commons.compress.archivers.tar.TarArchiveInputStream.getNextTarEntry(TarArchiveInputStream.java:257)    
...
Caused by: java.lang.IllegalArgumentException: Invalid byte 0 at offset 5 in '05412{NUL}11' len=8
at org.apache.commons.compress.archivers.tar.TarUtils.parseOctal(TarUtils.java:138)
Run Code Online (Sandbox Code Playgroud)

问题来自线路

entry = tarIn.getNextTarEntry();
Run Code Online (Sandbox Code Playgroud)

这是代码:

try {
        TarArchiveInputStream tarIn = new TarArchiveInputStream(
                new GZIPInputStream(
                        new BufferedInputStream(
                                new FileInputStream(
                                        tempDirPath + fileName))));

        TarArchiveEntry entry = tarIn.getNextTarEntry();

        while (entry != null) {
            File path = new …
Run Code Online (Sandbox Code Playgroud)

java apache tar apache-commons-compress

3
推荐指数
1
解决办法
4154
查看次数

将字节数组写入ZipArchiveOutputStream

我需要创建一个zip文件,并受以下条件的限制:

  • 条目以byte [](或ByteArrayOutputStream)形式出现,而不是File.
  • 条目的文件名可以是非ascii/UTF-8.
  • JDK 1.6或更早版本

由于java.util.zip仅支持JDK 1.7及更高版本的UTF-8文件名,因此使用commons-compress ZipArchiveOutputStream似乎更好.但是如何基于字节数组或ByteArrayOutputStream而不是File创建ZipEntryArchive?

谢谢!

java zip apache-commons-compress

1
推荐指数
1
解决办法
4307
查看次数

如何检查 TarArchiveEntry 是否设置了 3 个执行位中的任何一个?

在 commons-compress TarArchiveEntry 中,可以使用 getMode() 请求模式,但是这会返回一个 int。

检查是否设置了任何执行位(用户、组、每个人)的最佳方法是什么?

java bit-manipulation apache-commons-compress

1
推荐指数
1
解决办法
336
查看次数