/// <summary> /// 解析普通文本文档 /// </summary> /// <param name="fs"></param> /// <param name="encoding"></param> /// <returns></returns> private static RuleDataModel CreatFromTextFile(Stream fs, Encoding encoding) { List <string> lines = new List <string>(); fs.Position = 0L; using (StreamReader streamReader = new StreamReader(fs, encoding)) { while (streamReader.Peek() >= 0) { string str = streamReader.ReadLine(); if (!string.IsNullOrEmpty(str)) { lines.Add(str); } } } RuleDataModel data = new RuleDataModel() { OriginalFilePath = "", FileHander = lines[0], FgLarge = LineDataModel.CreatFromLineString(lines[1]) }; for (int i = 2; i < lines.Count; i++) { data.TextData.Add(LineDataModel.CreatFromLineString(lines[i])); } fs.Close(); return(data); }
/// <summary> /// 解析被ZLIB压缩的文本内容 /// </summary> /// <param name="fs"></param> /// <param name="br"></param> /// <returns></returns> private static RuleDataModel CreatFromeZlibFile(FileStream fs, BinaryReader br) { List <string> lines = new List <string>(); var dataLength = br.ReadInt32(); fs.Position = fs.Length - dataLength; int b1 = br.ReadByte(); int b2 = br.ReadByte(); if ((0x78 != b1 && 0x58 != b1) || 0 != (b1 << 8 | b2) % 31) { throw new InvalidDataException("Data not recoginzed as zlib-compressed stream"); } using (var deStream = new DeflateStream(new MemoryStream(br.ReadBytes(dataLength - 2)), CompressionMode.Decompress, true)) { using (var sr = new StreamReader(deStream, Encoding.Unicode)) { while (sr.Peek() >= 0) { string str = sr.ReadLine(); if (!string.IsNullOrEmpty(str)) { lines.Add(str); } } } } RuleDataModel data = new RuleDataModel() { OriginalFilePath = fs.Name, FileHander = lines[0], FgLarge = LineDataModel.CreatFromLineString(lines[1]) }; for (int i = 2; i < lines.Count; i++) { data.TextData.Add(LineDataModel.CreatFromLineString(lines[i])); } br.Close(); fs.Close(); return(data); }