unity5.x assetbundle Create MD5 file

Source: Internet
Author: User
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<string, list<string>> DicFileMD5 = new dictionary<string, list<string>> ();
MD5CryptoServiceProvider md5generator = new MD5CryptoServiceProvider ();


The folder path where you need to create the MD5 file
String dir = System.IO.Path.Combine (Application.datapath, "streamingassets");
MD5 Save Path
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<string> templist = new list<string> ();
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 ("<two File has the same name> name =" + FilePath);
}




if (directory.exists (savepath) = = False)
Directory.CreateDirectory (Savepath);




XmlDocument xmldoc = new XmlDocument ();
XmlElement xmlroot = xmldoc.createelement ("Files");
Xmldoc.appendchild (XmlRoot);
foreach (keyvaluepair<string, list<string>> 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<string, list<string>> readmd5file (string fileName)
{
dictionary<string, list<string>> DicMD5 = new dictionary<string, list<string>> ();


If the file does not exist, it returns directly
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<string> templist = new list<string> ();
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;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.