我有一个.DAT文件,如果我在记事本中打开文本给我的值为#¨¤³¦+eäÌIóÆ‹,j°
。我从一台机器上获得了这个文件,我有一个文档可以手动打开这个文件。按照那个< / p>
1. Rename each file extensions of TAXA files from .DAT to .GZ
2. Uncompress (or unzip) all TAXAxxxx.GZ (you can use ZIP)
3. After the file extractions, rename each file with file extensions as TAXAxxxx.TXT
这是我试过的代码..
string str = "";
BinaryReader br = new BinaryReader(File.OpenRead(@"F:\TAXAZ.DAT"));
foreach (char ch in br.ReadChars(5000))
str += ch;
br.Dispose();
这是按照上面提到的说明手动发生,但是当我试图以编程方式进行时它没有发生。我尝试了压缩和解压缩概念,但它徒劳无功。后来我试图用二进制读取,然后转换成字符串,但也没用。 请帮我把这个文件作为文字阅读。 感谢..
答案 0 :(得分:2)
您的说明说您必须将.dat重命名为.gz,解压缩,然后读取该文件。 您的代码正在跳过前两个步骤,只是尝试读取该文件。这就是它无法正常工作的原因。
您可以在C#中执行此操作,但需要对其进行解压缩。您可以使用How to specify KeyValueTextInputFormat Separator执行此操作。
来自该链接的示例C#代码:
using System;
using System.IO;
using System.IO.Compression;
namespace zip
{
public class Program
{
private static string directoryPath = @"c:\temp";
public static void Main()
{
DirectoryInfo directorySelected = new DirectoryInfo(directoryPath);
Compress(directorySelected);
foreach (FileInfo fileToDecompress in directorySelected.GetFiles("*.gz"))
{
Decompress(fileToDecompress);
}
}
public static void Compress(DirectoryInfo directorySelected)
{
foreach (FileInfo fileToCompress in directorySelected.GetFiles())
{
using (FileStream originalFileStream = fileToCompress.OpenRead())
{
if ((File.GetAttributes(fileToCompress.FullName) &
FileAttributes.Hidden) != FileAttributes.Hidden & fileToCompress.Extension != ".gz")
{
using (FileStream compressedFileStream = File.Create(fileToCompress.FullName + ".gz"))
{
using (GZipStream compressionStream = new GZipStream(compressedFileStream,
CompressionMode.Compress))
{
originalFileStream.CopyTo(compressionStream);
}
}
FileInfo info = new FileInfo(directoryPath + "\\" + fileToCompress.Name + ".gz");
Console.WriteLine("Compressed {0} from {1} to {2} bytes.",
fileToCompress.Name, fileToCompress.Length.ToString(), info.Length.ToString());
}
}
}
}
public static void Decompress(FileInfo fileToDecompress)
{
using (FileStream originalFileStream = fileToDecompress.OpenRead())
{
string currentFileName = fileToDecompress.FullName;
string newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length);
using (FileStream decompressedFileStream = File.Create(newFileName))
{
using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
{
decompressionStream.CopyTo(decompressedFileStream);
Console.WriteLine("Decompressed: {0}", fileToDecompress.Name);
}
}
}
}
}
}