zoukankan      html  css  js  c++  java
  • unix、dos、Mac文件格式转换 C#

    最近有个需求,xml文件修改时dos格式要变成unix格式,查找了一下三个文件的区别在于换行符
    unix格式:-LF( )
    dos格式:-LF/CR( )
    mac格式:-CR( )
    C#后台代码

      public class ConvertFileHelper
            {
                public enum FileType
                {
                    DOS,
                    UNIX,
                    MAC
                }
    
                private const byte CR = 0x0D;//
                private const byte LF = 0x0A;//
    
                private static readonly byte[] DOS_LINE_ENDING = new byte[] { CR, LF };
    
                public static FileType DetermineFileFormat(string fileName)
                {
                    byte[] data = File.ReadAllBytes(fileName);
                    int position = 0;
                    if (Array.IndexOf(data, LF, position) >= 0)
                    {
                        if (Array.IndexOf(data, CR, position) >= 0)
                        {
                            return FileType.DOS;
                        }
                        return FileType.UNIX;
    
                    }
                    if (Array.IndexOf(data, CR, position) >= 0)
                    {
                        return FileType.MAC;
                    }
                    return FileType.DOS;
                }
    
                public static void AutoFileTypeConvert(string fileName, FileType fileType)
                {
                    FileType oldFileType = DetermineFileFormat(fileName);
    
                    if (fileType == FileType.DOS)
                    {
                        if (oldFileType == FileType.DOS)
                            return;
                        else if (oldFileType == FileType.UNIX)
                            Unix2Dos(fileName);
                        else if (oldFileType == FileType.MAC)
                            Mac2Dos(fileName);
                    }
                    else if (fileType == FileType.UNIX)
                    {
                        if (oldFileType == FileType.UNIX)
                            return;
                        else if (oldFileType == FileType.DOS)
                            Dos2Unix(fileName);
                        else if (oldFileType == FileType.MAC)
                            Mac2Unix(fileName);
                    }
                    else if (fileType == FileType.MAC)
                    {
                        if (oldFileType == FileType.MAC)
                            return;
                        else if (oldFileType == FileType.DOS)
                            Dos2Mac(fileName);
                        else if (oldFileType == FileType.UNIX)
                            Unix2Mac(fileName);
                    }
                }
    
                public static void Unix2Dos(string fileName)
                {
                    byte[] data = File.ReadAllBytes(fileName);
                    using (FileStream fileStream = File.OpenWrite(fileName))
                    {
                        BinaryWriter bw = new BinaryWriter(fileStream);
                        int position = 0;
                        int index = 0;
                        do
                        {
                            index = Array.IndexOf(data, LF, position);
                            if (index >= 0)
                            {
                                if (index > 0 && data[index - 1] == CR)
                                {
                                    bw.Write(data, position, index - position + 1);
                                }
                                else
                                {
                                    bw.Write(data, position, index - position);
                                    bw.Write(DOS_LINE_ENDING);
                                }
                                position = index + 1;
                            }
                        }
                        while (index >= 0);
                        bw.Write(data, position, data.Length - position);
                        fileStream.SetLength(fileStream.Position);
                    }
                }
    
                public static void Mac2Dos(string fileName)
                {
                    byte[] data = File.ReadAllBytes(fileName);
                    int len = data.Length - 1;
                    using (FileStream fileStream = File.OpenWrite(fileName))
                    {
                        BinaryWriter bw = new BinaryWriter(fileStream);
                        int position = 0;
                        int index = 0;
                        do
                        {
                            index = Array.IndexOf(data, CR, position);
                            if (index >= 0)
                            {
                                if (index >= 0 && index < len && data[index + 1] == LF)
                                {
                                    bw.Write(data, position, index - position + 1);
                                }
                                else
                                {
                                    bw.Write(data, position, index - position);
                                    bw.Write(DOS_LINE_ENDING);
                                }
                                position = index + 1;
                            }
                        } while (index >= 0);
                        bw.Write(data, position, data.Length - position);
                        fileStream.SetLength(fileStream.Position);
                    }
                }
    
                public static void Dos2Unix(string fileName)
                {
                    byte[] data = File.ReadAllBytes(fileName);
                    using (FileStream fileStream = File.OpenWrite(fileName))
                    {
                        BinaryWriter bw = new BinaryWriter(fileStream);
                        int position = 0;
                        int index = 0;
                        do
                        {
                            index = Array.IndexOf(data, CR, position);
                            if (index >= 0)
                            {
                                if (index > 0 && data[index + 1] == LF)
                                {
                                    bw.Write(data, position, index - position);
                                }
                                else
                                {
                                    bw.Write(data, position, index - position + 1);
                                }
                                position = index + 1;
                            }
                        }
                        while (index >= 0);
                        bw.Write(data, position, data.Length - position);
                        fileStream.SetLength(fileStream.Position);
                    }
                }
    
                public static void Dos2Mac(string fileName)
                {
                    byte[] data = File.ReadAllBytes(fileName);
                    using (FileStream fileStream = File.OpenWrite(fileName))
                    {
                        BinaryWriter bw = new BinaryWriter(fileStream);
                        int position = 0;
                        int index = 0;
                        do
                        {
                            index = Array.IndexOf(data, LF, position);
                            if (index >= 0)
                            {
                                if (index > 0 && data[index - 1] == CR)
                                {
                                    bw.Write(data, position, index - position);
                                }
                                else
                                {
                                    bw.Write(data, position, index - position + 1);
                                }
                                position = index + 1;
                            }
                        }
                        while (index >= 0);
                        bw.Write(data, position, data.Length - position);
                        fileStream.SetLength(fileStream.Position);
                    }
                }
    
                public static void Mac2Unix(string fileName)
                {
                    byte[] data = File.ReadAllBytes(fileName);
                    int len = data.Length - 1;
                    using (FileStream fileStream = File.OpenWrite(fileName))
                    {
                        BinaryWriter bw = new BinaryWriter(fileStream);
                        int position = 0;
                        int index = 0;
                        do
                        {
                            index = Array.IndexOf(data, CR, position);
                            if (index >= 0)
                            {
                                if (index >= 0 && index < len && data[index + 1] == LF)
                                {
                                    bw.Write(data, position, index - position + 1);
                                }
                                else
                                {
                                    bw.Write(data, position, index - position);
                                    bw.Write(LF);
                                }
                                position = index + 1;
                            }
                        } while (index >= 0);
                        bw.Write(data, position, data.Length - position);
                        fileStream.SetLength(fileStream.Position);
                    }
                }
    
                public static void Unix2Mac(string fileName)
                {
                    byte[] data = File.ReadAllBytes(fileName);
                    using (FileStream fileStream = File.OpenWrite(fileName))
                    {
                        BinaryWriter bw = new BinaryWriter(fileStream);
                        int position = 0;
                        int index = 0;
                        do
                        {
                            index = Array.IndexOf(data, LF, position);
                            if (index >= 0)
                            {
                                if (index > 0 && data[index - 1] == CR)
                                {
                                    bw.Write(data, position, index - position + 1);
                                }
                                else
                                {
                                    bw.Write(data, position, index - position);
                                    bw.Write(CR);
                                }
                                position = index + 1;
                            }
                        }
                        while (index >= 0);
                        bw.Write(data, position, data.Length - position);
                        fileStream.SetLength(fileStream.Position);
                    }
                }
            }
    
  • 相关阅读:
    不规范的json文档 转化成 java 对象的处理
    财经接口
    Back-off pulling image "registry.access.redhat.com/rhel7/pod-infrastructure:latest
    VMware Workstation 14 Pro永久激活密钥
    Angular2入门:TypeScript的装饰器
    Angular2入门:TypeScript的模块
    Angular2入门:TypeScript的类
    51nod“省选”模测第二场 B 异或约数和(数论分块)
    51nod1238 最小公倍数之和 V3(莫比乌斯反演)
    cf1139D. Steps to One(dp)
  • 原文地址:https://www.cnblogs.com/zl-green/p/12893803.html
Copyright © 2011-2022 走看看