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

你可能感兴趣的:(安卓笔记,java,开发语言,linux)