using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using Common.TextUtil; namespace SSC { public class Z005 : NSC.IService { /// /// 获取最新文件 /// /// /// /// public NSC.NetServiceStruct ServiceFunction(Common.Data.UserAuthority userAuth, NSC.NetServiceStruct input) { string version = ""; StringBuilder query = new StringBuilder(); query.Append("SELECT VERSIONNO FROM T_UPDATE WHERE DEVICENAME='{DEVICENAME}' AND UPDATETIME<={UPDATETIME} ORDER BY CREATETIME DESC LIMIT 1"); query.Replace("{DEVICENAME}", input.FirmwareModel); query.Replace("{UPDATETIME}", Common.Data.SqlLite.SqlLiteHelper.LOCAL_TIME_CMD); Common.Data.SqlLite.SqlLiteHelper sqlite = new Common.Data.SqlLite.SqlLiteHelper(); using (var conn = sqlite.OpenConnection(Common.Config.ConfigSetting.GetConnectionString())) using (var table = sqlite.QueryReturnDataTable(conn, query.ToString())) { if (table.Rows.Count > 0) { version = table.Rows[0]["VERSIONNO"].ToString(); } } if (version == "") { throw new Exception("找不到适用的更新文件!"); } //打开文件 string filename = Common.Config.ConfigSetting.GetSetting("0001") + input.FirmwareModel + @"\" + version + @"\update.bin"; byte[] bytes = null; using (System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)) { bytes = new byte[fs.Length]; fs.Read(bytes, 0, bytes.Length); } input.ExceptionType = Common.Data.ExceptionType.NoneException; input.Returns.Add("Version", new NSC.ParameterStruct(version)); input.Returns.Add("Files", new NSC.ParameterStruct(bytes)); return input; } /// /// 解码更新文件 /// /// 要解码的更新文件流 /// 版本号 /// 临时文件夹路径 /// 解码后的临时文件信息 public List DecodeUpdateBinFile(byte[] binFile, string version, string tmpPath) { byte[] buff = Common.IO.Compression.DeflateCompression.Decompress(binFile); int length = BitConverter.ToInt32(buff, 0); System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); doc.LoadXml(Encoding.GetEncoding("gb2312").GetString(buff, 4, length)); var nodes = doc.SelectNodes("FileUpdateData/File"); if (System.IO.Directory.Exists(tmpPath)) { System.IO.Directory.Delete(tmpPath, true); } System.IO.Directory.CreateDirectory(tmpPath); List fileList = new List(); int position = length + 3; foreach (System.Xml.XmlNode n in nodes) { FileInformation file = new FileInformation(); file.FileName = n.Attributes["Name"].Value; file.UpdatePath = n.Attributes["Path"].Value; file.FileLength = int.Parse(n.Attributes["Length"].Value); if (file.UpdatePath.IsEmptyOrNull()) { file.FilePath = tmpPath + @"\" + file.FileName; } else { System.IO.Directory.CreateDirectory(tmpPath + @"\" + file.UpdatePath); file.FilePath = tmpPath + @"\" + file.UpdatePath + @"\" + file.FileName; } using (System.IO.FileStream fs = new System.IO.FileStream(file.FilePath, System.IO.FileMode.CreateNew, System.IO.FileAccess.Write, System.IO.FileShare.None)) { fs.Write(buff, position, file.FileLength); } position += file.FileLength; fileList.Add(file); } return fileList; } /// /// 编码更新文件 /// /// 文件列表 /// 更新模块定义 /// 版本号 /// 编码后的字节流 public static byte[] EncodeUpdateBinFile(List fileList, string module, string version) { using (MemoryStream tmpStream = new MemoryStream()) { //写版本及文件信息 System.Xml.XmlTextWriter xtw = new System.Xml.XmlTextWriter(tmpStream, EncodingHelper.GB2312); xtw.WriteStartDocument(); xtw.WriteStartElement("FileUpdateData"); xtw.WriteAttributeString("ModuleCode", module); xtw.WriteAttributeString("VersionNumber", version); foreach (var f in fileList) { xtw.WriteStartElement("File"); xtw.WriteAttributeString("Name", f.FileName); xtw.WriteAttributeString("Path", f.UpdatePath); xtw.WriteAttributeString("Length", f.FileLength.ToString()); xtw.WriteEndElement(); } xtw.WriteEndElement(); xtw.WriteEndDocument(); xtw.Flush(); int length = (int)tmpStream.Length; tmpStream.Position = 0; tmpStream.Write(BitConverter.GetBytes(length), 0, 4); tmpStream.Position = tmpStream.Length - 1; //写入文件流 foreach (var f in fileList) { using (System.IO.FileStream fs = new FileStream(f.FilePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { byte[] buff = new byte[fs.Length]; fs.Read(buff, 0, (int)fs.Length); tmpStream.Write(buff, 0, buff.Length); } } return Common.IO.Compression.DeflateCompression.Compress(tmpStream.ToArray()); } } } /// /// 文件信息 /// public struct FileInformation { /// /// 文件名 /// public string FileName; /// /// 文件存放路径 /// public string FilePath; /// /// 文件更新目标相对路径 /// public string UpdatePath; /// /// 文件长度 /// public int FileLength; } }