I want to extract .tgz file programmatically in my android app. I searched a lot but dint get any answer. Below is the code I am using to extract.
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());
}
}
}
}
But here the header is always null and it leads to nullpointer. Is there any way I can extract .tgz files. Is there any library to achive this ?