C# Dos、Unix、Mac文件格式之间的相互转换

一、Dos、Unix、Mac文件格式(换行符)区别

  • Dos格式文件:-CR/LF(\r\n)
  • Unix格式文件:-LF (\n)
  • Mac格式文件:-CR (\r)

二、转换方法

1.C#

  1 using System;
  2 using System.IO;
  3 
  4 namespace SmartREIN.Common
  5 {
  6     public class TextDocumentFileTypeConvert
  7     {
  8         public enum FileType
  9         {
 10             DOS,
 11             UNIX,
 12             MAC
 13         }
 14 
 15         private const byte CR = 0x0D;//\r
 16         private const byte LF = 0x0A;//\n
 17         private static readonly byte[] DOS_LINE_ENDING = new byte[] { CR, LF };
 18 
 19         public static FileType DetermineFileFormat(string fileName)
 20         {
 21             byte[] data = File.ReadAllBytes(fileName);
 22             int position = 0;
 23             if (Array.IndexOf(data, LF, position) >= 0)
 24             {
 25                 if (Array.IndexOf(data, CR, position) >= 0)
 26                 {
 27                     return FileType.DOS;
 28                 }
 29                 return FileType.UNIX;
 30 
 31             }
 32             if (Array.IndexOf(data, CR, position) >= 0)
 33             {
 34                 return FileType.MAC;
 35             }
 36             return FileType.DOS;
 37         }
 38 
 39         public static void AutoFileTypeConvert(string fileName, FileType fileType)
 40         {
 41             FileType oldFileType = DetermineFileFormat(fileName);
 42 
 43             if (fileType == FileType.DOS)
 44             {
 45                 if (oldFileType == FileType.DOS)
 46                     return;
 47                 else if (oldFileType == FileType.UNIX)
 48                     Unix2Dos(fileName);
 49                 else if (oldFileType == FileType.MAC)
 50                     Mac2Dos(fileName);
 51             }
 52             else if (fileType == FileType.UNIX)
 53             {
 54                 if (oldFileType == FileType.UNIX)
 55                     return;
 56                 else if (oldFileType == FileType.DOS)
 57                     Dos2Unix(fileName);
 58                 else if (oldFileType == FileType.MAC)
 59                     Mac2Unix(fileName);
 60             }
 61             else if (fileType == FileType.MAC)
 62             {
 63                 if (oldFileType == FileType.MAC)
 64                     return;
 65                 else if (oldFileType == FileType.DOS)
 66                     Dos2Mac(fileName);
 67                 else if (oldFileType == FileType.UNIX)
 68                     Unix2Mac(fileName);
 69             }
 70         }
 71 
 72         public static void Unix2Dos(string fileName)
 73         {
 74             byte[] data = File.ReadAllBytes(fileName);
 75             using (FileStream fileStream = File.OpenWrite(fileName))
 76             {
 77                 BinaryWriter bw = new BinaryWriter(fileStream);
 78                 int position = 0;
 79                 int index = 0;
 80                 do
 81                 {
 82                     index = Array.IndexOf(data, LF, position);
 83                     if (index >= 0)
 84                     {
 85                         if (index > 0 && data[index - 1] == CR)
 86                         {
 87                             bw.Write(data, position, index - position + 1);
 88                         }
 89                         else
 90                         {
 91                             bw.Write(data, position, index - position);
 92                             bw.Write(DOS_LINE_ENDING);
 93                         }
 94                         position = index + 1;
 95                     }
 96                 }
 97                 while (index >= 0);
 98                 bw.Write(data, position, data.Length - position);
 99                 fileStream.SetLength(fileStream.Position);
100             }
101         }
102 
103         public static void Mac2Dos(string fileName)
104         {
105             byte[] data = File.ReadAllBytes(fileName);
106             int len = data.Length - 1;
107             using (FileStream fileStream = File.OpenWrite(fileName))
108             {
109                 BinaryWriter bw = new BinaryWriter(fileStream);
110                 int position = 0;
111                 int index = 0;
112                 do
113                 {
114                     index = Array.IndexOf(data, CR, position);
115                     if (index >= 0)
116                     {
117                         if (index >= 0 && index < len && data[index + 1] == LF)
118                         {
119                             bw.Write(data, position, index - position + 1);
120                         }
121                         else
122                         {
123                             bw.Write(data, position, index - position);
124                             bw.Write(DOS_LINE_ENDING);
125                         }
126                         position = index + 1;
127                     }
128                 } while (index >= 0);
129                 bw.Write(data, position, data.Length - position);
130                 fileStream.SetLength(fileStream.Position);
131             }
132         }
133 
134         public static void Dos2Unix(string fileName)
135         {
136             byte[] data = File.ReadAllBytes(fileName);
137             using (FileStream fileStream = File.OpenWrite(fileName))
138             {
139                 BinaryWriter bw = new BinaryWriter(fileStream);
140                 int position = 0;
141                 int index = 0;
142                 do
143                 {
144                     index = Array.IndexOf(data, CR, position);
145                     if (index >= 0)
146                     {
147                         if (index > 0 && data[index + 1] == LF)
148                         {
149                             bw.Write(data, position, index - position);
150                         }
151                         else
152                         {
153                             bw.Write(data, position, index - position + 1);
154                         }
155                         position = index + 1;
156                     }
157                 }
158                 while (index >= 0);
159                 bw.Write(data, position, data.Length - position);
160                 fileStream.SetLength(fileStream.Position);
161             }
162         }
163 
164         public static void Dos2Mac(string fileName)
165         {
166             byte[] data = File.ReadAllBytes(fileName);
167             using (FileStream fileStream = File.OpenWrite(fileName))
168             {
169                 BinaryWriter bw = new BinaryWriter(fileStream);
170                 int position = 0;
171                 int index = 0;
172                 do
173                 {
174                     index = Array.IndexOf(data, LF, position);
175                     if (index >= 0)
176                     {
177                         if (index > 0 && data[index - 1] == CR)
178                         {
179                             bw.Write(data, position, index - position);
180                         }
181                         else
182                         {
183                             bw.Write(data, position, index - position + 1);
184                         }
185                         position = index + 1;
186                     }
187                 }
188                 while (index >= 0);
189                 bw.Write(data, position, data.Length - position);
190                 fileStream.SetLength(fileStream.Position);
191             }
192         }
193 
194         public static void Mac2Unix(string fileName)
195         {
196             byte[] data = File.ReadAllBytes(fileName);
197             int len = data.Length - 1;
198             using (FileStream fileStream = File.OpenWrite(fileName))
199             {
200                 BinaryWriter bw = new BinaryWriter(fileStream);
201                 int position = 0;
202                 int index = 0;
203                 do
204                 {
205                     index = Array.IndexOf(data, CR, position);
206                     if (index >= 0)
207                     {
208                         if (index >= 0 && index < len && data[index + 1] == LF)
209                         {
210                             bw.Write(data, position, index - position + 1);
211                         }
212                         else
213                         {
214                             bw.Write(data, position, index - position);
215                             bw.Write(LF);
216                         }
217                         position = index + 1;
218                     }
219                 } while (index >= 0);
220                 bw.Write(data, position, data.Length - position);
221                 fileStream.SetLength(fileStream.Position);
222             }
223         }
224 
225         public static void Unix2Mac(string fileName)
226         {
227             byte[] data = File.ReadAllBytes(fileName);
228             using (FileStream fileStream = File.OpenWrite(fileName))
229             {
230                 BinaryWriter bw = new BinaryWriter(fileStream);
231                 int position = 0;
232                 int index = 0;
233                 do
234                 {
235                     index = Array.IndexOf(data, LF, position);
236                     if (index >= 0)
237                     {
238                         if (index > 0 && data[index - 1] == CR)
239                         {
240                             bw.Write(data, position, index - position + 1);
241                         }
242                         else
243                         {
244                             bw.Write(data, position, index - position);
245                             bw.Write(CR);
246                         }
247                         position = index + 1;
248                     }
249                 }
250                 while (index >= 0);
251                 bw.Write(data, position, data.Length - position);
252                 fileStream.SetLength(fileStream.Position);
253             }
254         }
255     }
256 }

2.UltraEdit

File -> Conversions -> UNIX/MAC to DOS

         -> DOS to UNIX

         -> DOS to MAC

其它还有很多方式去转,对应的环境有对应的方法。

 

C# Dos、Unix、Mac文件格式之间的相互转换

上一篇:Wpf DataGrid动态添加列,行数据(一)


下一篇:vscode废掉了,跳转不到函数定义,无法自动补全,重装也没用的解决办法