【发布时间】:2011-10-30 23:41:20
【问题描述】:
谁能告诉我在我一直在搜索的 java 中压缩和解压缩 tar.gzip 文件的正确方法,但我能找到的最多的是 zip 或 gzip(单独)。
【问题讨论】:
-
tgz 文件没有什么特别之处——你先解压缩它,然后解压缩它。
标签: java gzip tar compression
谁能告诉我在我一直在搜索的 java 中压缩和解压缩 tar.gzip 文件的正确方法,但我能找到的最多的是 zip 或 gzip(单独)。
【问题讨论】:
标签: java gzip tar compression
我最喜欢的是 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>
【讨论】:
如果您打算在 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;
【讨论】:
使用TrueVFS 提取 Tar.GZip 存档是单行的:
new TFile("archive.tar.gz").cp_rp(new File("dest/folder"));
但要小心dependencies issue。
【讨论】:
为了提取 .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!");
}
}
【讨论】:
dest.close(); 和 tarIn.close();
它适用于我,使用GZIPInputStream、https://www.mkyong.com/java/how-to-decompress-file-from-gzip-file/
【讨论】:
我为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);
【讨论】:
根据我的经验,Apache Compress 比Plexus Archiver 成熟得多,特别是因为http://jira.codehaus.org/browse/PLXCOMP-131 之类的问题。
我相信 Apache Compress 也有更多的活动。
【讨论】: