Search code examples
goadbtar

I want to decompress the .ab file backed up by adb and write it into a tar package


I handle it in the following way

bf, err := os.Open(backupFilePath)
if err != nil {
    fmt.Println("os.Open: ", err)
    return
}
defer bf.Close()
rd := bufio.NewReader(bf)

tarFile, err := os.Create(tarFilePath)
if err != nil {
    fmt.Println("os.Create: ", err)
    return
}
defer tarFile.Close()

zf, zerr := zlib.NewReader(rd)
if zerr != nil {
    return "", zerr
}
_, err = io.Copy(tarFile, zf)
if err != nil {
    fmt.Println("io.Copy backup.ab -> backup.ab.tar failed:  ", err)
}
zf.Close()

An error occurred: io.Copy backup.ab -> backup.ab.tar failed: Unexpected EOF

Is this the case because the .ab file is corrupted or is this the wrong way to handle it?


Solution

  • It's most likely that the .ab file is corrupted.

    But there is an issue in your code too. You should skip the first 24 bytes when reading from the .ab file. Otherwise, you should see this error: zlib: invalid header. Since you see something else, I would assume that your .ab file is corrupted.

    BTW, rd := bufio.NewReader(bf) is not needed.

    Here is the demo that works for me:

    package main
    
    import (
        "compress/zlib"
        "io"
        "os"
    )
    
    func main() {
        bf, err := os.Open("temp.ab")
        if err != nil {
            panic(err)
        }
        defer bf.Close()
        if _, err := bf.Seek(24, 0); err != nil {
            panic(err)
        }
    
        zf, err := zlib.NewReader(bf)
        if err != nil {
            panic(err)
        }
        defer zf.Close()
    
        tarFile, err := os.Create("temp.tar")
        if err != nil {
            panic(err)
        }
        defer tarFile.Close()
    
        _, err = io.Copy(tarFile, zf)
        if err != nil {
            panic(err)
        }
    }
    

    Update:

    Tested the demo with backup.ab, no error was reported. But the generated tar file is invalid:

    $ tar tvf backup.tar
    <...list of files truncated...>
    tar: Unexpected EOF in archive
    tar: Error is not recoverable: exiting now
    

    Tried with zlib-flate, got the same result:

    $ dd if=backup.ab ibs=24 skip=1 | zlib-flate -uncompress > backup2.tar
    $ md5sum backup*.tar
    3eff01578dec035367688e03b6ec7a72  backup2.tar
    3eff01578dec035367688e03b6ec7a72  backup.tar
    

    Tried with https://github.com/nelenkov/android-backup-extractor, got the same result too. So the backup.ab file should be corrupted.

    $ java -jar ~/Downloads/abe.jar unpack backup.ab backup3.tar
    $ md5sum backup*.tar
    3eff01578dec035367688e03b6ec7a72  backup2.tar
    3eff01578dec035367688e03b6ec7a72  backup3.tar
    3eff01578dec035367688e03b6ec7a72  backup.tar