Search code examples
c#androidadbarchivezlib

C# Decompress an Android Backup (adb) file


I'm trying to decompress an Android adb file using the Deflate algorithm. I've tried both DotNetZips Ionic Zlib as well as Microsofts built-in System.IO.Compression introduced in Net 4.5 but both of them result in a corrupted archive. They both have the exact same file size, but the hashes don't match up between the corrupt and good archives.

I'm using the following code to decompress.

byte[] app = File.ReadAllBytes(tb_keyOutDir.Text + "\\app_stripped.ab");
MemoryStream ms = new MemoryStream(app);

//skip first two bytes to avoid invalid block length error
ms.Seek(2, SeekOrigin.Begin);

DeflateStream deflate = new DeflateStream(ms, CompressionMode.Decompress);
string dec = new StreamReader(deflate, Encoding.ASCII).ReadToEnd();

File.WriteAllText(tb_keyOutDir.Text + "\\app.tar", dec);

I can decompress it via CygWin with OpenSSL and it's decompressing it properly so I know my files aren't corrupted or anything.

cat app_stripped.ab | openssl zlib -d > app.tar

Solution

  • use Ionic library

    try use this method to decompress :

        public static byte[] Decompress(byte[] gzip) {
            using (var stream = new Ionic.Zlib.ZlibStream(new MemoryStream(gzip), Ionic.Zlib.CompressionMode.Decompress)) {
                const int size = 1024;
                byte[] buffer = new byte[size];
                using (MemoryStream memory = new MemoryStream()) {
                    int count = 0;
                    do {
                        count = stream.Read(buffer, 0, size);
                        if (count > 0) {
                            memory.Write(buffer, 0, count);
                        }
                    }
                    while (count > 0);
                    return memory.ToArray();
                }
            }
        }
    

    and when you want call :

        byte[] app = Decompress(File.ReadAllBytes(tb_keyOutDir.Text + "\\app_stripped.ab"));
        File.WriteAllBytes(tb_keyOutDir.Text + "\\app.tar", app);