C#对文件操作(基本的读写以及压缩和解压)

主要是针对单个文件进行读写操作和压缩操作:用到的主要C#类有FileStream、FileInfo、StreamWrite、StreamRead、GZipStream。

字符数组和字节数组的转换:

 1  byte[] bytedata = new byte[200];
 2             char[] chardata = new char[200];
 3             try
 4             {
 5                 FileStream fs = new FileStream("App.config", FileMode.Open);
 6                 fs.Seek(0, SeekOrigin.Begin);
 7                 fs.Read(bytedata, 0, 200);
 8             }
 9             catch (IOException io)
10             {
11                 Console.WriteLine(io.ToString());
12                 Console.ReadKey();
13                 return;
14             }
15             Decoder dc = Encoding.UTF8.GetDecoder();//创建一个解码器用来对二进制数组解码成字符数组
16             dc.GetChars(bytedata, 0, bytedata.Length, chardata, 0);
17             Console.WriteLine(chardata);
18             Console.ReadKey();
19             byte[] byteData;
20             char[] charData;
21             try
22             {
23                 FileStream fs = new FileStream("Log.txt", FileMode.Create);
24                 charData = "this is the user first log the software".ToCharArray();
25                 Encoder e = Encoding.UTF8.GetEncoder();
26                 byteData = new byte[charData.Length];
27                 e.GetBytes(charData, 0, charData.Length, byteData, 0, true);
28                 fs.Seek(0, SeekOrigin.End);
29                 fs.Write(byteData, 0, byteData.Length);
30             }
31             catch (IOException io)
32             {
33                 Console.WriteLine(io.ToString());
34                 Console.ReadKey();
35                 return;
36             }
37             Console.ReadKey();

对文件进行压缩和解压(一单个文件为例):

 1 string fileName = "CompressedFile.txt";
 2             Console.WriteLine("Please input a word and it will repeate 100 times");
 3             string inputString = Console.ReadLine();
 4             StringBuilder sourceString = new StringBuilder(inputString.Length * 100);
 5             for (int i = 0; i < 100; i++) 
 6             {
 7                 sourceString.AppendLine(inputString);
 8             }
 9             string sourceCompresses = sourceString.ToString();
10             Console.WriteLine("source data's length is {0}", sourceCompresses.Length);
11             try
12             {
13                 CompressedFile(fileName, sourceCompresses);
14                 Console.WriteLine("Compressed successfully");
15                 FileInfo fileInfo = new FileInfo(fileName);
16                 Console.WriteLine("compressed file's length is{0}", fileInfo.Length);
17                 string loadCompressed = LoadCompressedFile(fileName);
18                 Console.WriteLine(loadCompressed);
19             }
20             catch (IOException io)
21             {
22                 Console.WriteLine(io.ToString());
23                 Console.ReadKey();
24                 return;
25             }
26             Console.ReadKey();
27         }
28 
29         private static void CompressedFile(string fileName, string sourceCompress) 
30         {
31             FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
32             GZipStream gzCompressedFile = new GZipStream(fs, CompressionMode.Compress);
33             StreamWriter sw = new StreamWriter(gzCompressedFile,Encoding.UTF8);
34             sw.Write(sourceCompress);
35             sw.Close();
36         }
37 
38         private static string LoadCompressedFile(string fileName) 
39         {
40             FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
41             GZipStream gzLoadCompressed = new GZipStream(fs, CompressionMode.Decompress);
42             StreamReader sr = new StreamReader(gzLoadCompressed,Encoding.UTF8);
43             StringBuilder strBuild = new StringBuilder();
44             string strReadLine = sr.ReadLine();
45             while (!string.IsNullOrEmpty(strReadLine)) 
46             {
47                 strBuild.Append(strReadLine);
48                 strReadLine = sr.ReadLine();
49             }
50             return strBuild.ToString();
51         }
原文地址:https://www.cnblogs.com/simen-tan/p/5374373.html