我有一个 HTTPHandler,它正在读取一组 CSS 文件并将它们组合起来,然后对它们进行 GZipping。但是,一些 CSS 文件包含字节顺序标记(由于 TFS 2005 自动合并中的错误),并且在 FireFox 中,BOM 被作为实际内容的一部分读取,因此它搞砸了我的类名等。我该如何剥离出 BOM 字符?有没有一种简单的方法可以做到这一点,而无需手动通过字节数组寻找“”?
JC Grubbs
问问题
15956 次
5 回答
8
用示例扩展Jon 的评论。
var name = GetFileName();
var bytes = System.IO.File.ReadAllBytes(name);
System.IO.File.WriteAllBytes(name, bytes.Skip(3).ToArray());
于 2008-11-14T02:54:00.940 回答
6
扩展 JaredPar 示例以递归子目录:
using System.Linq;
using System.IO;
namespace BomRemover
{
/// <summary>
/// Remove UTF-8 BOM (EF BB BF) of all *.php files in current & sub-directories.
/// </summary>
class Program
{
private static void removeBoms(string filePattern, string directory)
{
foreach (string filename in Directory.GetFiles(directory, file Pattern))
{
var bytes = System.IO.File.ReadAllBytes(filename);
if(bytes.Length > 2 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF)
{
System.IO.File.WriteAllBytes(filename, bytes.Skip(3).ToArray());
}
}
foreach (string subDirectory in Directory.GetDirectories(directory))
{
removeBoms(filePattern, subDirectory);
}
}
static void Main(string[] args)
{
string filePattern = "*.php";
string startDirectory = Directory.GetCurrentDirectory();
removeBoms(filePattern, startDirectory);
}
}
}
当您尝试执行基本的 PHP 下载文件时,我发现 UTF-8 BOM 损坏文件后,我需要那段 C# 代码。
于 2010-05-19T08:23:39.300 回答
3
var text = File.ReadAllText(args.SourceFileName);
var streamWriter = new StreamWriter(args.DestFileName, args.Append, new UTF8Encoding(false));
streamWriter.Write(text);
streamWriter.Close();
于 2009-07-16T09:50:02.080 回答
1
另一种方式,假设 UTF-8 为 ASCII。
File.WriteAllText(filename, File.ReadAllText(filename, Encoding.UTF8), Encoding.ASCII);
于 2008-11-14T08:32:32.830 回答
0
For larger file, use the following code; memory efficient!
StreamReader sr = new StreamReader(path: @"<Input_file_full_path_with_byte_order_mark>",
detectEncodingFromByteOrderMarks: true);
StreamWriter sw = new StreamWriter(path: @"<Output_file_without_byte_order_mark>",
append: false,
encoding: new UnicodeEncoding(bigEndian: false, byteOrderMark: false));
var lineNumber = 0;
while (!sr.EndOfStream)
{
sw.WriteLine(sr.ReadLine());
lineNumber += 1;
if (lineNumber % 100000 == 0)
Console.Write("\rLine# " + lineNumber.ToString("000000000000"));
}
sw.Flush();
sw.Close();
于 2018-03-14T13:37:59.517 回答