我想要解压由adb备份的.ab文件,并将其写入一个tar包中。

huangapple go评论111阅读模式
英文:

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

问题

我是你的中文翻译助手,以下是翻译好的内容:

我是这样处理的:

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()

发生了错误:io.Copy backup.ab -> backup.ab.tar 失败:意外的EOF

这是因为.ab文件损坏了还是处理方式不正确?

英文:

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?

答案1

得分: 1

很可能是.ab文件损坏了。

但是你的代码也有问题。在读取.ab文件时,你应该跳过前24个字节。否则,你可能会看到这个错误:zlib: invalid header。由于你看到了其他内容,我会认为你的.ab文件是损坏的。

另外,rd := bufio.NewReader(bf) 是不需要的。

以下是对我有效的演示代码:

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)
	}
}

更新

使用backup.ab测试了演示代码,没有报错。但生成的tar文件无效:

$ tar tvf backup.tar
<...文件列表已截断...>
tar: Unexpected EOF in archive
tar: Error is not recoverable: exiting now

尝试使用zlib-flate,得到了相同的结果:

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

尝试使用https://github.com/nelenkov/android-backup-extractor,也得到了相同的结果。所以backup.ab文件应该是损坏的。

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

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 (
	&quot;compress/zlib&quot;
	&quot;io&quot;
	&quot;os&quot;
)

func main() {
	bf, err := os.Open(&quot;temp.ab&quot;)
	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(&quot;temp.tar&quot;)
	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
&lt;...list of files truncated...&gt;
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 &gt; 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

huangapple
  • 本文由 发表于 2023年4月24日 18:11:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76090789.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定