【问题标题】:Extract .tgz file programmatically android以编程方式提取.tgz文件android
【发布时间】:2018-08-01 05:42:35
【问题描述】:

我想在我的 android 应用程序中以编程方式提取 .tgz 文件。我搜索了很多,但没有得到任何答案。下面是我用来提取的代码。

public static void extractArchive2(File archive, File destination) {
    Archive arch = null;
    try {
        arch = new Archive(archive);
    } catch (RarException e) {
        Log.d("MainActivity::","RarException::" + e.toString());
    } catch (IOException e1) {
        Log.d("MainActivity::","IOException::" + e1.toString());
    }
    if (arch != null) {
        if (arch.isEncrypted()) {
            return;
        }
        FileHeader fh = null;
        while (true) {
            fh = arch.nextFileHeader();
            if (fh == null) {
                Log.d("MainActivity::","fh null::");
                break;
            }
            if (fh.isEncrypted()) {
                Log.d("MainActivity::","file is encrypted cannot extract::" + fh.getFileNameString());

                continue;
            }
            Log.d("MainActivity::", "extracting: " + fh.getFileNameString() );
            try {
                if (fh.isDirectory()) {
                    createDirectory(fh, destination);
                } else {
                    File f = createFile(fh, destination);
                    OutputStream stream = new FileOutputStream(f);
                    arch.extractFile(fh, stream);
                    stream.close();
                }
            } catch (IOException e) {
                Log.d("MainActivity::", "IOException 2: " + e.toString());
            } catch (RarException e) {
                Log.d("MainActivity::", "RarException 2: " + e.toString());
            }
        }
    }
}

但是这里的头总是空的,它会导致空指针。有什么方法可以提取 .tgz 文件。有没有图书馆可以做到这一点?

【问题讨论】:

标签: android file


【解决方案1】:

因为 junrar 库提取 .rar 文件。 .tgz 是一种完全不同的压缩格式。图书馆不支持它。这就是为什么名称是“unrar”而不是“untgz”或gunzip的原因(用于创建.tgz文件的命令通常是gzip创建和gunzip打开)。

【讨论】: