前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ICSharpCode.SharpZipLib.Zip 解析时报错System.NotSupportedException: No data is availa

ICSharpCode.SharpZipLib.Zip 解析时报错System.NotSupportedException: No data is availa

原创
作者头像
GoodTime
发布2023-10-31 09:56:16
2360
发布2023-10-31 09:56:16
举报
文章被收录于专栏:GoodTime的全栈开发

分析原因

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

微软开发文档地址

代码语言:c#
复制
// 错误代码
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);
					}
				}
			}
		}
	}
}

以上就是ICSharpCode.SharpZipLib.Zip 解析时报错System.NotSupportedException: No data is availa的介绍,做此记录,如有帮助,欢迎点赞关注收藏!

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 以上就是ICSharpCode.SharpZipLib.Zip 解析时报错System.NotSupportedException: No data is availa的介绍,做此记录,如有帮助,欢迎点赞关注收藏!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档