-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTextFile.cs
111 lines (105 loc) · 3.75 KB
/
TextFile.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using Li.Zlib;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace Li.Text
{
public class TextFile
{
public readonly static string textHead = "#layer_type name left top width height type opacity visible layer_id group_layer_id base images ";
public string FileHander
{
get;
set;
}
public PictureInfo Fglarge
{
get;
set;
}
public List<PictureInfo> TextData
{
get;
set;
}
public TextFile()
{
FileHander = string.Empty;
Fglarge = new PictureInfo();
TextData = new List<PictureInfo>();
}
public TextFile(string ruleFile)
{
if (Path.GetExtension(ruleFile).ToLower() == ".json")
{
JsonFile(ruleFile);
return;
}
List<string> list = new List<string>();
FileStream fileStream = new FileStream(ruleFile, FileMode.Open, FileAccess.Read, FileShare.None);
BinaryReader binaryReader = new BinaryReader(fileStream);
byte[] array = binaryReader.ReadBytes(5);
if (array[0] == 254 && array[1] == 254 && array[2] == 2 && array[3] == byte.MaxValue && array[4] == 254)
{
int num = binaryReader.ReadInt32();
fileStream.Position = fileStream.Length - num;
byte[] bytes = LiZlib.deCompressBytes(binaryReader.ReadBytes(num));
using (StringReader stringReader = new StringReader(Encoding.Unicode.GetString(bytes)))
{
string text;
while ((text = stringReader.ReadLine()) != null)
{
if (!string.IsNullOrEmpty(text))
{
list.Add(text);
}
}
}
}
else
{
Encoding encoding = (array[1] == 0) ? Encoding.Unicode : Encoding.UTF8;
fileStream.Position = 0L;
using (StreamReader streamReader = new StreamReader(fileStream, encoding))
{
while (streamReader.Peek() >= 0)
{
string text2 = streamReader.ReadLine();
if (!string.IsNullOrEmpty(text2))
{
list.Add(text2);
}
}
}
}
binaryReader.Close();
fileStream.Close();
FileHander = list[0];
Fglarge = new PictureInfo(list[1]);
TextData = new List<PictureInfo>();
for (int i = 2; i < list.Count; i++)
{
TextData.Add(new PictureInfo(list[i]));
}
}
private void JsonFile(string ruleFile)
{
string text = "";
FileStream fileStream = new FileStream(ruleFile, FileMode.Open, FileAccess.Read, FileShare.None);
using (StreamReader streamReader = new StreamReader(fileStream))
{
text = streamReader.ReadToEnd();
}
fileStream.Close();
PictureInfo[] array = JsonConvert.DeserializeObject<PictureInfo[]>(text.Replace("\t", ""));
FileHander = "";
Fglarge = array[0];
TextData = new List<PictureInfo>();
for (int i = 1; i < array.Length; i++)
{
TextData.Add(array[i]);
}
}
}
}