ICSharpCode.SharpZipLib.Zip 解析时报错System.NotSupportedException: No data is available for encoding 936

分析原因

利用ICSharpCode.SharpZipLib.Zip进行APK解析时,因为APK内编译的名称为中文,查询微软开发文档936为gb2312中文编码

 微软开发文档地址icon-default.png?t=L9C2https://docs.microsoft.com/zh-cn/windows/win32/intl/code-page-identifiers

错误代码

using (ZipInputStream zip = new ZipInputStream(File.OpenRead(path))) 
{
	using (var filestream = new FileStream(path, FileMode.Open, FileAccess.Read)) 
	{
		 
        // 出现错误部分
		ZipFile zipfile = new ZipFile(filestream);
		foreach (ZipEntry entry in zipfile) 
		{
			if (entry != null) 
			{
				if (entry.Name.ToLower() == "androidmanifest.xml") 
				{
					manifestData = new byte[50 * 1024];
					Stream strm = zipfile.GetInputStream(entry);
					strm.Read(manifestData, 0, manifestData.Length);
				}
				if (entry.Name.ToLower() == "resources.arsc") 
				{
					Stream strm = zipfile.GetInputStream(entry);
					using (BinaryReader s = new BinaryReader(strm)) 
					{
						resourcesData = s.ReadBytes((int)entry.Size);
					}
				}
			}
		}
	}
}

解决方法

using (ZipInputStream zip = new ZipInputStream(File.OpenRead(path))) 
{
	using (var filestream = new FileStream(path, FileMode.Open, FileAccess.Read)) 
	{
        // 在.Net Core中默认System.Text中不支持CodePagesEncodingProvider.Instance
        // 添加下方这行代码允许访问.Net Framework平台上不支持的编码提供程序
     System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
		ZipFile zipfile = new ZipFile(filestream);
		foreach (ZipEntry entry in zipfile) 
		{
			if (entry != null) 
			{
				if (entry.Name.ToLower() == "androidmanifest.xml") 
				{
					manifestData = new byte[50 * 1024];
					Stream strm = zipfile.GetInputStream(entry);
					strm.Read(manifestData, 0, manifestData.Length);
				}
				if (entry.Name.ToLower() == "resources.arsc") 
				{
					Stream strm = zipfile.GetInputStream(entry);
					using (BinaryReader s = new BinaryReader(strm)) 
					{
						resourcesData = s.ReadBytes((int)entry.Size);
					}
				}
			}
		}
	}
}

posted @ 2021-10-28 09:06  GoodTimeGGB  阅读(42)  评论(0编辑  收藏  举报