【发布时间】: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 文件。有没有图书馆可以做到这一点?
【问题讨论】:
-
this 问题是否相关?
-
什么是存档?这不是内置的 Android 类。如果它是一个图书馆,你需要告诉我们是哪一个。
-
@GabeSechan
implementation group: 'com.github.junrar', name: 'junrar', version: '0.7'应该是这个。