C# 解压 Android 备份 (adb) 文件 [英] C# Decompress an Android Backup (adb) file

查看:30
本文介绍了C# 解压 Android 备份 (adb) 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Deflate 算法解压缩 Android adb 文件.我已经尝试过 DotNetZips Ionic Zlib 以及微软内置的 System.IO.Compression 在 Net 4.5 中引入,但它们都导致存档损坏.它们都具有完全相同的文件大小,但损坏的存档和良好的存档之间的哈希值不匹配.

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

我可以通过带有 OpenSSL 的 CygWin 解压缩它并且它正在正确解压缩它,所以我知道我的文件没有损坏或任何东西.

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

推荐答案

使用 Ionic 库

试试用这个方法解压:

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

当你想打电话时:

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

这篇关于C# 解压 Android 备份 (adb) 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆