Unity5.X AssetBundle创建MD5文件

using UnityEngine;
using UnityEditor;
using System.IO;
using System.Xml;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;


public class CreatMD5 : MonoBehaviour {




    public static void  CreatMD5XML()
    {
        Dictionary> DicFileMD5 = new Dictionary>();
        MD5CryptoServiceProvider md5Generator = new MD5CryptoServiceProvider();


        //需要创建MD5文件的文件夹路径
        string dir = System.IO.Path.Combine(Application.dataPath, "StreamingAssets");
        //MD5保存路径
        string savePath = System.IO.Path.Combine(Application.dataPath, "StreamingAssets");






        foreach (string filePath in Directory.GetFiles(dir))
        {
            if (filePath.Contains(".meta") || filePath.Contains("VersionMD5") || filePath.Contains(".xml"))
                continue;


            FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
            byte[] hash = md5Generator.ComputeHash(file);
            List tempList = new List();
            string strMD5 = System.BitConverter.ToString(hash);
            string size = file.Length.ToString();
            tempList.Add(strMD5);
            tempList.Add(size);


            file.Close();


            string key = filePath.Substring(dir.Length + 1, filePath.Length - dir.Length - 1);


            if (DicFileMD5.ContainsKey(key) == false)
                DicFileMD5.Add(key, tempList);
            else
                Debug.LogWarning(" name = " + filePath);
        }




        if (Directory.Exists(savePath) == false)
            Directory.CreateDirectory(savePath);




        XmlDocument XmlDoc = new XmlDocument();
        XmlElement XmlRoot = XmlDoc.CreateElement("Files");
        XmlDoc.AppendChild(XmlRoot);
        foreach (KeyValuePair> pair in DicFileMD5)
        {
            XmlElement xmlElem = XmlDoc.CreateElement("File");
            XmlRoot.AppendChild(xmlElem);


            xmlElem.SetAttribute("FileName", pair.Key);
            xmlElem.SetAttribute("MD5", pair.Value[0]);
            xmlElem.SetAttribute("Size", pair.Value[1]);
        }


        XmlDoc.Save(savePath + "/VersionMD5.xml");
        XmlDoc = null;




        AssetDatabase.Refresh();
    }


    static Dictionary> ReadMD5File(string fileName)
    {
        Dictionary> DicMD5 = new Dictionary>();


        // 如果文件不存在,则直接返回
        if (System.IO.File.Exists(fileName) == false)
            return DicMD5;


        XmlDocument XmlDoc = new XmlDocument();
        XmlDoc.Load(fileName);
        XmlElement XmlRoot = XmlDoc.DocumentElement;


        foreach (XmlNode node in XmlRoot.ChildNodes)
        {
            if ((node is XmlElement) == false)
                continue;


            List tempList = new List();
            string file = (node as XmlElement).GetAttribute("FileName");
            string md5 = (node as XmlElement).GetAttribute("MD5");
            string size = (node as XmlElement).GetAttribute("Size");
            tempList.Add(md5);
            tempList.Add(size);


            if (DicMD5.ContainsKey(file) == false)
            {
                DicMD5.Add(file, tempList);
            }
        }


        XmlRoot = null;
        XmlDoc = null;


        return DicMD5;
    }
}

你可能感兴趣的:(Unity5.X AssetBundle创建MD5文件)