forked from plasticbox/ConvertEncoding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertProcess.cs
More file actions
167 lines (145 loc) · 5.49 KB
/
ConvertProcess.cs
File metadata and controls
167 lines (145 loc) · 5.49 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace ConvertEncoding
{
class ConvertProcess
{
public ConvertEncodingForm.delSetMaxProgressBar SetMaxProgressBar;
public ConvertEncodingForm.delPerformStep PerformStep;
// 코드페이지 번호 http://msdn.microsoft.com/ko-kr/library/system.text.encoding.aspx
int euckrCodepage = 51949;
// 인코딩을 편리하게 해주기 위해서 인코딩클래스 변수 만듬
System.Text.Encoding utf8;
System.Text.Encoding euckr;
// 변환할 리스트
List<string> convertList;
public ConvertProcess()
{
this.utf8 = System.Text.Encoding.UTF8;
this.euckr = System.Text.Encoding.GetEncoding(euckrCodepage);
this.convertList = new List<string>();
}
public void FileSearch(string sDir, string[] fileExtensions)
{
this.convertList.Clear();
try
{
foreach (string sExtension in fileExtensions)
{
foreach (string searchFile in Directory.GetFiles(sDir, "*." + sExtension, SearchOption.AllDirectories))
{
this.convertList.Add(searchFile);
}
}
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
finally
{
SetMaxProgressBar(this.convertList.Count());
}
}
public void ConvertingProcess(int nIndex)
{
switch (nIndex)
{
case (int)ConvertEncodingForm.Type.EUCKR:
{
foreach (string filePath in this.convertList)
{
this.convertEuckrToUtf8(filePath);
PerformStep();
}
}
break;
case (int)ConvertEncodingForm.Type.UTF8:
{
foreach (string filePath in this.convertList)
{
this.convetUtf8ToEuckr(filePath);
PerformStep();
}
}
break;
}
}
public bool convertEuckrToUtf8(string filePath)
{
System.IO.FileInfo file = new System.IO.FileInfo(filePath);
if (false == file.Exists)
{
return false;
}
if (Encoding.ASCII != ConvertProcess.GetEncoding(filePath))
{
return false;
}
string strContents = System.IO.File.ReadAllText(filePath, System.Text.Encoding.Default);
byte[] arrUTFContents = utf8.GetBytes(strContents);
byte[] arrResult = Encoding.UTF8.GetPreamble().Concat(arrUTFContents).ToArray();
try
{
System.IO.File.WriteAllBytes(filePath, arrResult);
System.IO.FileInfo newFile = new System.IO.FileInfo(filePath);
newFile.Attributes = file.Attributes;
newFile.CreationTime = file.CreationTime;
newFile.LastAccessTime = file.LastAccessTime;
newFile.LastWriteTime = file.LastWriteTime;
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
return false;
}
return true;
}
public bool convetUtf8ToEuckr(string filePath)
{
System.IO.FileInfo file = new System.IO.FileInfo(filePath);
if (false == file.Exists)
{
return false;
}
if (Encoding.UTF8 != ConvertProcess.GetEncoding(filePath))
{
return false;
}
string strContents = System.IO.File.ReadAllText(filePath, utf8);
byte[] arrEUCKRContents = euckr.GetBytes(strContents);
try
{
System.IO.File.WriteAllBytes(filePath, arrEUCKRContents);
System.IO.FileInfo newFile = new System.IO.FileInfo(filePath);
newFile.Attributes = file.Attributes;
newFile.CreationTime = file.CreationTime;
newFile.LastAccessTime = file.LastAccessTime;
newFile.LastWriteTime = file.LastWriteTime;
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
return false;
}
return true;
}
public static Encoding GetEncoding(string filename)
{
// Read the BOM
var bom = new byte[4];
using (var file = new FileStream(filename, FileMode.Open)) file.Read(bom, 0, 4);
// Analyze the BOM
if (bom[0] == 0x2b && bom[1] == 0x2f && bom[2] == 0x76) return Encoding.UTF7;
if (bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf) return Encoding.UTF8;
if (bom[0] == 0xff && bom[1] == 0xfe) return Encoding.Unicode; //UTF-16LE
if (bom[0] == 0xfe && bom[1] == 0xff) return Encoding.BigEndianUnicode; //UTF-16BE
if (bom[0] == 0 && bom[1] == 0 && bom[2] == 0xfe && bom[3] == 0xff) return Encoding.UTF32;
return Encoding.ASCII;
}
}
}