如何在java中压缩/解压缩tar.gz文件

时间:2023-01-15 07:48:47

Can anyone show me the correct way to compress and decompress tar.gzip files in java i've been searching but the most i can find is either zip or gzip(alone).

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

6 个解决方案

#1


24  

My favorite is plexus-archiver - see sources on GitHub.

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

Another option is Apache commons-compress - (see mvnrepository).

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

With plexus-utils, the code for unarchiving looks like this:

使用plexus-utils,unarchiving的代码如下所示:

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();

Similar *Archiver classes are there for archiving.

类似的* Archiver类可用于存档。

With Maven, you can use this dependency:

使用Maven,您可以使用此依赖项:

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

#2


27  

I've written a wrapper for commons-compress called jarchivelib that makes it easy to extract or compress from and into File objects.

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

Example code would look like this:

示例代码如下所示:

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);

#3


7  

In my experience Apache Compress is much more mature than Plexus Archiver, specifically because of issues like http://jira.codehaus.org/browse/PLXCOMP-131.

根据我的经验,Apache Compress比Plexus Archiver更成熟,特别是因为像http://jira.codehaus.org/browse/PLXCOMP-131这样的问题。

I believe Apache Compress has more activity as well.

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

#4


6  

To extract the contents of .tar.gz format, I successfully use apache commons-compress ('org.apache.commons:commons-compress:1.12'). Take a look at this example method:

要提取.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!");
    }
}

#5


0  

It works for me, using GZIPInputStream, https://www.mkyong.com/java/how-to-decompress-file-from-gzip-file/

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

#6


0  

If you are planning to compress/decompress on Linux, you can call the shell command line:

如果您计划在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;

#1


24  

My favorite is plexus-archiver - see sources on GitHub.

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

Another option is Apache commons-compress - (see mvnrepository).

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

With plexus-utils, the code for unarchiving looks like this:

使用plexus-utils,unarchiving的代码如下所示:

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();

Similar *Archiver classes are there for archiving.

类似的* Archiver类可用于存档。

With Maven, you can use this dependency:

使用Maven,您可以使用此依赖项:

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

#2


27  

I've written a wrapper for commons-compress called jarchivelib that makes it easy to extract or compress from and into File objects.

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

Example code would look like this:

示例代码如下所示:

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);

#3


7  

In my experience Apache Compress is much more mature than Plexus Archiver, specifically because of issues like http://jira.codehaus.org/browse/PLXCOMP-131.

根据我的经验,Apache Compress比Plexus Archiver更成熟,特别是因为像http://jira.codehaus.org/browse/PLXCOMP-131这样的问题。

I believe Apache Compress has more activity as well.

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

#4


6  

To extract the contents of .tar.gz format, I successfully use apache commons-compress ('org.apache.commons:commons-compress:1.12'). Take a look at this example method:

要提取.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!");
    }
}

#5


0  

It works for me, using GZIPInputStream, https://www.mkyong.com/java/how-to-decompress-file-from-gzip-file/

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

#6


0  

If you are planning to compress/decompress on Linux, you can call the shell command line:

如果您计划在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;