我正在尝试使用Deflate算法解压缩Android adb文件。我尝试了dotnetzip Ionic Zlib和微软内置的System.IO.Compression,但它们都会导致存档损坏。它们都有完全相同的文件大小,但是在坏的和好的档案之间的哈希值不匹配。
我使用下面的代码来解压。
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);
我可以通过CygWin和OpenSSL解压缩它,它是正确的解压缩,所以我知道我的文件没有损坏或任何东西。
cat app_stripped.ab | openssl zlib -d > app.tar
使用Ionic library
尝试使用此方法解压:
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);