【问题标题】:How to Compress/Decompress tar.gz files in java如何在 java 中压缩/解压缩 tar.gz 文件
【发布时间】:2011-10-30 23:41:20
【问题描述】:

谁能告诉我在我一直在搜索的 java 中压缩和解压缩 tar.gzip 文件的正确方法,但我能找到的最多的是 zip 或 gzip(单独)。

【问题讨论】:

标签: java gzip tar compression


【解决方案1】:

我最喜欢的是 plexus-archiver - 请参阅 GitHub 上的资源。

另一种选择是 Apache commons-compress -(请参阅 mvnrepository)。

使用 plexus-utils,取消归档的代码如下所示:

final TarGZipUnArchiver ua = new TarGZipUnArchiver();
// Logging - as @Akom noted, logging is mandatory in newer versions, so you can use a code like this to configure it:
ConsoleLoggerManager manager = new ConsoleLoggerManager();
manager.initialize();
ua.enableLogging(manager.getLoggerForComponent("bla"));
// -- end of logging part
ua.setSourceFile(sourceFile);
destDir.mkdirs();
ua.setDestDirectory(destDir);
ua.extract();

类似的 *Archiver 类用于存档。

有了 Maven,你可以使用这个dependency

<dependency>
  <groupId>org.codehaus.plexus</groupId>
  <artifactId>plexus-archiver</artifactId>
  <version>2.2</version>
</dependency>

【讨论】:

    【解决方案2】:

    如果您打算在 Linux 上压缩/解压缩,您可以调用 shell 命令行为您执行此操作:

    Files.createDirectories(Paths.get(target));
    ProcessBuilder builder = new ProcessBuilder();
    builder.command("sh", "-c", String.format("tar xfz %s -C %s", tarGzPathLocation, target));
    builder.directory(new File("/tmp"));
    Process process = builder.start();
    int exitCode = process.waitFor();
    assert exitCode == 0;
    

    【讨论】:

      【解决方案3】:

      使用TrueVFS 提取 Tar.GZip 存档是单行的:

      new TFile("archive.tar.gz").cp_rp(new File("dest/folder"));
      

      但要小心dependencies issue

      【讨论】:

        【解决方案4】:

        为了提取 .tar.gz 格式的内容,我成功使用了 apache commons-compress ('org.apache.commons:commons-compress:1.12')。看看这个示例方法:

        public void extractTarGZ(InputStream in) {
            GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(in);
            try (TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn)) {
                TarArchiveEntry entry;
        
                while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {
                    /** If the entry is a directory, create the directory. **/
                    if (entry.isDirectory()) {
                        File f = new File(entry.getName());
                        boolean created = f.mkdir();
                        if (!created) {
                            System.out.printf("Unable to create directory '%s', during extraction of archive contents.\n",
                                    f.getAbsolutePath());
                        }
                    } else {
                        int count;
                        byte data[] = new byte[BUFFER_SIZE];
                        FileOutputStream fos = new FileOutputStream(entry.getName(), false);
                        try (BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER_SIZE)) {
                            while ((count = tarIn.read(data, 0, BUFFER_SIZE)) != -1) {
                                dest.write(data, 0, count);
                            }
                        }
                    }
                }
        
                System.out.println("Untar completed successfully!");
            }
        }
        

        【讨论】:

        • 由于您使用的是 try-with-resources 语法,因此您不需要 dest.close();tarIn.close();
        【解决方案5】:

        它适用于我,使用GZIPInputStreamhttps://www.mkyong.com/java/how-to-decompress-file-from-gzip-file/

        【讨论】:

        • 我是不是遗漏了什么,这将解压 gzip 文件并留下 tar 文件?
        【解决方案6】:

        我为commons-compress 编写了一个名为jarchivelib 的包装器,它可以轻松地从File 对象中提取或压缩。

        示例代码如下所示:

        File archive = new File("/home/thrau/archive.tar.gz");
        File destination = new File("/home/thrau/archive/");
        
        Archiver archiver = ArchiverFactory.createArchiver("tar", "gz");
        archiver.extract(archive, destination);
        

        【讨论】:

        • 这很棒 - 这与命令行实用程序的工作方式相同 - 解压缩 ,从我这里抽象出所有样板细节(也许如果我需要担心我会使用的性能commons-compress 库,但我几乎不需要)。
        • @thrau :它给了我一个编译时错误,因为:方法 createArchiver(String, String) 未定义 String 类型
        • 并将演员表添加到存档器
        • 看看网页上的其他例子rauschig.org/jarchivelib/examples.html
        • @thrau 文件提取有回调吗?提取完成后我想知道。
        【解决方案7】:

        根据我的经验,Apache CompressPlexus Archiver 成熟得多,特别是因为http://jira.codehaus.org/browse/PLXCOMP-131 之类的问题。

        我相信 Apache Compress 也有更多的活动。

        【讨论】:

        • Apache Compress 无法提取某些 tar.gz 档案,因为缺乏支持。这个错误从未得到解决:jfrog.com/jira/browse/HAP-651
        • @didile 如果将错误报告给 jfrog 而不是 apache compress,您希望如何解决此问题?
        • 它也已报告给apache问题跟踪器。
        • @didile 请提供链接。
        • @didile 我没有看到任何向issues.apache.org/jira/browse/COMPRESS 报告的与 HAP-651 匹配的错误。如果您可以打开一个并在 Compress 失败的地方附加一个 tar,那就太好了。
        猜你喜欢
        • 2018-06-26
        • 2013-04-27
        • 2011-11-01
        • 1970-01-01
        • 2013-04-29
        • 1970-01-01
        • 2022-07-27
        • 2021-09-12
        • 1970-01-01
        相关资源
        最近更新 更多