压缩数据流

using Hyperion; using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.Linq; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Threading.Tasks; namespace Common.BaseLibrary.IO { public class IO { public static byte[] SaveBin(object obj) { return SaveBin(obj, false); } public static object LoadBin(byte[] buff) { return LoadBin(buff, false); } public static byte[] SaveBin(object obj, bool compress) { byte[] buff = null; using (MemoryStream stream = new MemoryStream()) { if (compress) { using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Compress)) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(gzipStream, obj); } } else { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, obj); } buff = stream.ToArray(); } return buff; } public static object LoadBin(byte[] buff, bool compress) { object obj = null; if (buff != null) { using (MemoryStream stream = new MemoryStream()) { stream.Write(buff, 0, buff.Length); stream.Seek(0, SeekOrigin.Begin); if (compress) { using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress)) { BinaryFormatter formatter = new BinaryFormatter(); try { obj = formatter.Deserialize(gzipStream); } catch //(SerializationException e)
 { //throw;
 } finally { gzipStream.Close(); } } } else { BinaryFormatter formatter = new BinaryFormatter(); try { obj = formatter.Deserialize(stream); } catch //(SerializationException e)
 { //throw;
 } finally { stream.Close(); } } } } return obj; } public static byte[] SerializeByHyperion<T>(T obj) { var hser = new Serializer(new SerializerOptions(false)); var hstream = new MemoryStream(); hser.Serialize(obj, hstream); byte[] buff = hstream.ToArray(); hstream.Close(); hstream.Dispose(); return buff; } public static T DeserializeByHyperion<T>(byte[] buff) { var hdeser = new Serializer(new SerializerOptions(false)); var hstream = new MemoryStream(buff); T obj = hdeser.Deserialize<T>(hstream); hstream.Close(); hstream.Dispose(); return obj; } } }
public void InsertWorkingDataDBRec(WorkDataModel model) { IExtensionRepository<WorkingData> workDataService = DIFactory.ObjectContainer.Resolve<IExtensionRepository<WorkingData>>(); if (model != null) { WorkingData data = new WorkingData() { TaskID = model.TaskID, NJStartTime = model.NJStartTime, NJStopTime = model.NJStopTime, NJWorkingDt = IO.SerializeByHyperion<List<RealTimeDataModel>>(JsonConvert.DeserializeObject<List<RealTimeDataModel>>(model.NJWorkingDt)),//压缩数据流
                    OptFieldName = model.OptFieldName, OptLength = model.OptLength, GoodOptLength = model.GoodOptLength, OptArea = model.OptArea, OptPrice = model.OptPrice, OptGoodPercent = model.OptGoodPercent, OptAllowance = model.OptAllowance, OptAvgDepth = model.OptAvgDepth, IsNJOpt = model.IsNJOpt, OptAvgFuel = model.OptAvgFuel, }; workDataService.Insert(data); } }
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AIMS.Model { public class WorkingData { [Key] public int Id { get; set; } /// <summary>
        /// 任务id /// </summary>
 [Required] public int TaskID { get; set; } /// <summary>
        /// 开始时间 /// </summary>
        public DateTime? NJStartTime { get; set; } /// <summary>
        /// 结束时间 /// </summary>
        public DateTime? NJStopTime { get; set; } /// <summary>
        /// 农机数据序列化为二进制 /// </summary>
        public byte[] NJWorkingDt { get; set; }//重点

        /// <summary>
        /// 做业地块的名称 /// </summary>
        [StringLength(255)] public string OptFieldName { get; set; } /// <summary>
        /// 做业长度 /// </summary>
        public double? OptLength { get; set; } /// <summary>
        /// 做业达标长度 /// </summary>

        public double? GoodOptLength { get; set; } /// <summary>
        /// 做业面积 /// </summary>
        public double? OptArea { get; set; } /// <summary>
        /// 做业价格 /// </summary>
        public decimal? OptPrice { get; set; } /// <summary>
        /// 做业达标率 /// </summary>
        public double? OptGoodPercent { get; set; } /// <summary>
        /// 补贴价格 /// </summary>
        public decimal? OptAllowance { get; set; } /// <summary>
        /// 做业平均耕深 /// </summary>
        public double? OptAvgDepth { get; set; } /// <summary>
        /// 是否做业 /// </summary>
        public bool IsNJOpt { get; set; } /// <summary>
        /// 做业平均油耗 /// </summary>
        public double? OptAvgFuel { get; set; } } }
相关文章
相关标签/搜索