java解压tar.gz文件

  public static void decompressTarGzs(File file, String outputDir) throws IOException {
        try (TarArchiveInputStream tarIn = new TarArchiveInputStream(
                new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(file))), "GBK")) {
            // Create output directory
            createDirectory(outputDir, null);
            TarArchiveEntry entry;
            while ((entry = tarIn.getNextTarEntry()) != null) {
                // Is directory
                if (entry.isDirectory()) {
                    // Create empty directory
                    createDirectory(outputDir, entry.getName());
                } else {
                    // Is file
                    File vFile = new File(outputDir + File.separator + entry.getName());
                    try (OutputStream out = new FileOutputStream(vFile)) {
                        writeFile(tarIn, out);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("UnGZipUtil", "decompressTarGz: Exception", e);
        }
    }

    public static void createDirectory(String outputDir, String subDir) {
        File file = new File(outputDir);
        // Subdirectory not empty
        if (!(subDir == null || subDir.trim().equals(""))) {
            file = new File(outputDir + File.separator + subDir);
        }
        if (!file.exists()) {
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            file.mkdirs();
        }
    }

    public static void writeFile(InputStream in, OutputStream out) throws IOException {
        int length;
        byte[] b = new byte[1024 * 1024];
        while ((length = in.read(b)) != -1) {
            out.write(b, 0, length);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java可以使用Apache Commons Compress来解压tar.gz文件。以下是一个示例代码: ```java import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; import java.io.*; public class TarGzipExample { public static void main(String[] args) throws IOException { File file = new File("example.tar.gz"); try (FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis); GzipCompressorInputStream gzis = new GzipCompressorInputStream(bis); TarArchiveInputStream tais = new TarArchiveInputStream(gzis)) { TarArchiveEntry entry; while ((entry = tais.getNextTarEntry()) != null) { String name = entry.getName(); File outputFile = new File(name); if (entry.isDirectory()) { outputFile.mkdirs(); } else { outputFile.getParentFile().mkdirs(); try (OutputStream out = new FileOutputStream(outputFile); BufferedOutputStream bos = new BufferedOutputStream(out)) { byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = tais.read(buffer)) != -1) { bos.write(buffer, 0, bytesRead); } } } } } } } ``` 在读取tar.gz文件时,文件路径可能会乱码。这是因为tar格式不支持Unicode字符集,因此在保存文件名时会使用一种转换方法。如果文件名中包含非ASCII字符,那么这种转换方法可能会导致文件名乱码。要解决这个问题,可以使用`org.apache.commons.compress.archivers.tar.TarUtils.parseName()`方法来解析文件名。以下是修改后的代码: ```java import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import org.apache.commons.compress.archivers.tar.TarUtils; import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; import java.io.*; public class TarGzipExample { public static void main(String[] args) throws IOException { File file = new File("example.tar.gz"); try (FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis); GzipCompressorInputStream gzis = new GzipCompressorInputStream(bis); TarArchiveInputStream tais = new TarArchiveInputStream(gzis)) { TarArchiveEntry entry; while ((entry = tais.getNextTarEntry()) != null) { String name = TarUtils.parseName(entry.getName(), entry.getHeader().nameBytes).toString(); File outputFile = new File(name); if (entry.isDirectory()) { outputFile.mkdirs(); } else { outputFile.getParentFile().mkdirs(); try (OutputStream out = new FileOutputStream(outputFile); BufferedOutputStream bos = new BufferedOutputStream(out)) { byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = tais.read(buffer)) != -1) { bos.write(buffer, 0, bytesRead); } } } } } } } ``` 这个示例代码会在解压文件时解析文件名,并在保存文件时使用解析后的文件名。这样可以避免文件名乱码问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

九狼JIULANG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值