让代码更简单

当前位置:代码狗 > 开发教程 > C#开发教程 > 正文

使用C#解包微信小程序代码

重要:本文最后更新于2020-09-22 08:28:51,某些文章具有时效性,若有错误或已失效,请在下方留言或联系代码狗

看到别人的微信小程序眼馋了怎么办?拿过来改改就是我们自己的了,下面教大家怎么将别人的微信小程序变成自己的。要实现这样的功能,必须先拿到微信小程序的安装包。苹果的程序拿不到,我们只能拿到安卓的微信小程序安装包。

安卓微信小程序在安卓微信的安装目录中,下面给个参考:/data/data/com.tencent.mm/MicroMsg/账号标识/appbrand/pkg/

使用C#解包微信小程序代码

使用C#解包微信小程序代码

C#微信小程序解包类

/// <summary>
/// 微信小程序解包
/// </summary>
public class UnWxapkg
{
FileStream fileStream = null;
BinaryReader binaryReader = null;
Action<string, byte[]> action = null;
string message; 
public UnWxapkg(string path)
{
fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
binaryReader = new BinaryReader(fileStream);

}
public string Go(Action<string, byte[]> action_ = null)
{
action = action_;
if (!ReadFirstMark())
{
return message;
}
ReadInfo();
int indexInfoLength = ReadindexInfoLength();
int bodyInfoLength = ReadBodyInfoLength();
if (!ReadLastMark())
{
return message;
}
int fileCount = ReadFileCount();
for (int i = 0; i < fileCount; i++)
{
ReadFile();
}
binaryReader.Dispose();
fileStream.Dispose();
binaryReader.Close();
fileStream.Close();

return "完成";
}
/// <summary>
/// 读取标志位 用于判断是否为小程序包
/// </summary>
/// <param name="binaryReader"></param>
public bool ReadFirstMark()
{
if (ReadInt(1) != 190)
{
message = "当前文件不是微信小程序包!";
return false;
}
return true;
}
/// <summary>
/// 读取小程序附加信息
/// </summary>
/// <returns></returns>
public int ReadInfo() {
return (int)binaryReader.ReadUInt32();
}
/// <summary>
/// 读取索引长度
/// </summary>
/// <returns></returns>
public int ReadindexInfoLength()
{
return ReadInt(4);
}
/// <summary>
/// 读取数据长度
/// </summary>
/// <returns></returns>
public int ReadBodyInfoLength()
{
return ReadInt(4);
}
/// <summary>
/// 读取结尾标记
/// </summary>
/// <returns></returns>
public bool ReadLastMark()
{
if (ReadInt(1) != 237)
{
message = "当前文件不是微信小程序包!";
return false;
}
return true;
}
/// <summary>
/// 读取文件数量
/// </summary>
/// <returns></returns>
public int ReadFileCount()
{
return ReadInt(4);
}
/// <summary>
/// 读文件信息
/// </summary>
public void ReadFile()
{
int fileNameLength = ReadInt(4);
byte[] fileNameb = binaryReader.ReadBytes(fileNameLength);
string fileName = System.Text.Encoding.Default.GetString(fileNameb);
int file_start = ReadInt(4);
int file_length = ReadInt(4);
//先缓存当前索引
long currindex = binaryReader.BaseStream.Position;
binaryReader.BaseStream.Position = file_start;
byte[] file_content = binaryReader.ReadBytes(file_length);
//还原索引
binaryReader.BaseStream.Position = currindex;
if (action != null)
{
action.Invoke(fileName, file_content);
}

}
/// <summary>
/// 读整数
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
int ReadInt(int length)
{
int num = 0;
for (int i = 0; i < length; i++)
{
num = (num << 8) + ((int)binaryReader.ReadByte());
}
return num;
}
}

简单使用

 string path = "小程序包路径";
UnWxapkg unWxapkg = new UnWxapkg(path);
Console.WriteLine("开始解包!");
string mess = unWxapkg.Go((x,x1)=> {
//解包后的存储地址
string filePath = @"E:\123\1" + x;
string dirPath = Path.GetDirectoryName(filePath);
//开始写出文件
if (!Directory.Exists(dirPath)) {
Directory.CreateDirectory(dirPath);
}
FileStream stream = new FileStream(filePath, FileMode.OpenOrCreate);
stream.Write(x1, 0, x1.Length);
stream.Dispose();
stream.Close();
Console.WriteLine(x1.Length.ToString()+" "+ @"E:\123\1" + x);
});

解包后效果如下图

使用C#解包微信小程序代码

使用C#解包微信小程序代码

感觉很棒!可以赞赏支持我哟~

赞(1) 打赏

评论 1

  1. #1

    不错,谢谢分享

    fafa2周前 (10-12)回复
×

请作者吃根烤肠!

支付宝