0

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 ?

madhuri H R
  • 699
  • 1
  • 10
  • 26
  • https://stackoverflow.com/questions/7128171/how-to-compress-decompress-tar-gz-files-in-java – AskNilesh Aug 01 '18 at 05:46
  • Possible duplicate of [How to Compress/Decompress tar.gz files in java](https://stackoverflow.com/questions/7128171/how-to-compress-decompress-tar-gz-files-in-java) – Sachin Rajput Aug 01 '18 at 05:48
  • Is [this](https://stackoverflow.com/questions/51610015/how-to-unrar-tgz-file-programatically) question related? – K Neeraj Lal Aug 01 '18 at 05:57
  • What is Archive? That isn't a built in Android class. If its a library, you need to tell us what one. – Gabe Sechan Aug 01 '18 at 05:59
  • @GabeSechan `implementation group: 'com.github.junrar', name: 'junrar', version: '0.7'` Should be this one. – K Neeraj Lal Aug 01 '18 at 06:02

1 Answers1

1

Because the junrar library extracts .rar files. .tgz is a completely different compression format. The library doesn't support it. That's why the name is "unrar" instead of "untgz" or gunzip (the command used to create .tgz files normally is gzip to create and gunzip to open).

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127