
TAR.GZ 和 ZIP 通常用于文件/目录的归档和压缩。这两种文件格式在几乎所有流行的操作系统上都能很好地工作。然而,TAR.GZ 在压缩较大目录及其他元数据时提供了增强的性能。因此,在这篇博客文章中,我们将学习如何以编程方式 在 Java 中将 ZIP 转换为 TAR.GZ。为此,我们将选择 Aspose.ZIP for Java,这是一个高度可靠的 API,用于创建 TAR.GZ 文件。此外,您可以通过为您的商业软件开发一个 ZIP 到 TAR.GZ 转换器 来自动化整个过程。
将涵盖以下要点:
TAR 存档的 Java API - 安装
我们将 安装 这个库来实现该功能。因此,您有两个选择,首先,您可以 下载 JAR 文件,或者您可以按照以下 Maven 配置进行操作:
<repositories>
<repository>
<id>snapshots</id>
<name>repo</name>
<url>http://repository.aspose.com/repo/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-zip</artifactId>
<version>24.5</version>
</dependency>
</dependencies>

在 Java 中以编程方式将 ZIP 转换为 TAR.GZ
这里需要注意的一点是,您需要源 ZIP 文件来实现 ZIP 到 TAR.GZ 的转换。
以下步骤演示如何使用 Aspose.ZIP for Java 生成 TAR.GZ 文件:
- 创建 Archive 类的实例并加载源 ZIP 文件。
- 实例化 TarArchive 类的对象。
- 通过调用 getEntries 方法获取构成 RAR 存档的 RarArchiveEntry 类型条目。
- 调用 createEntry 方法在存档中创建一个条目。
- 通过调用 saveGzipped 方法保存为 TAR.GZ 存档。
将以下代码示例复制并粘贴到您的服务器文件中,并运行服务器以 在 Java 中将 ZIP 转换为 TAR.GZ:
package com.example; | |
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import com.aspose.zip.Archive; | |
import com.aspose.zip.ArchiveEntry; | |
import com.aspose.zip.TarArchive; | |
import com.aspose.zip.exceptions.IOException; | |
public class Main { | |
// Convert ZIP to TAR.GZ in Java | |
public static void main(String[] args) throws java.io.IOException { | |
String dir = "/files/"; | |
// Create an instance of the Archive class and load the source ZIP file. | |
try (Archive source = new Archive(dir+"sample.zip")) { | |
// Instantiate an object of the TarArchive class. | |
try (TarArchive tar = new TarArchive()) { | |
// Get the entries of RarArchiveEntry type constituting the rar archive by calling the getEntries method. | |
for (ArchiveEntry entry : source.getEntries()) { | |
if (!entry.isDirectory()) { | |
ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
try (java.io.InputStream mem = entry.open()) { | |
byte[] b = new byte[8192]; | |
int bytesRead; | |
while (0 < (bytesRead = mem.read(b, 0, b.length))) { | |
out.write(b, 0, bytesRead); | |
} | |
} | |
// Invoke the createEntry method to create a single entry within the archive. | |
tar.createEntry(entry.getName(), new ByteArrayInputStream(out.toByteArray())); | |
} | |
} | |
// Save as the TAR.GZ archive by calling the saveGzipped method. | |
tar.saveGzipped(dir+"result.tar.gz"); | |
} | |
} catch (IOException ex) { | |
System.out.println(ex); | |
} | |
} | |
} |
您可以在下图中查看输出:

ZIP 到 TAR.GZ 转换器 - 在线工具
现在,本节介绍一个由 Aspose.ZIP for Java 提供支持的 在线 工具。实际上,这个 ZIP 到 TAR.GZ 转换器 使您能够在线将 ZIP 文件转换为其他流行的文件格式。此外,它是基于 Web 的,并提供强大的转换功能。最重要的是,使用它没有费用,您可以轻松进行转换。

创建 TAR.GZ 文件 - 获取免费许可证
您可以获得一个免费的 临时许可证 来尝试这个 Java API,而没有评估限制。
总结
最后,Aspose.ZIP for Java 不仅提供转换功能,还提供存档格式的程序化创建和操作。因此,本指南演示了如何 在 Java 中将 ZIP 转换为 TAR.GZ,以及步骤和代码示例。因此,您可以访问 文档 和 API 参考 来了解更多信息。
您可以查看这个 入门指南 来开始开发。最后,aspose.com 将发布其他有用的博客文章,所以请保持关注。
提问
您可以在我们的 论坛 上告诉我们您的问题或疑问。
常见问题解答 – FAQs
如何将 ZIP 转换为 TAR.GZ?
Aspose.ZIP 提供多种编程语言的强大解决方案来实现 ZIP 到 TAR.GZ 的转换。因此,对于 Java,请访问此 链接。