首页 > 代码库 > C#对文件操作(基本的读写以及压缩和解压)

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

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

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

 1  byte[] bytedata = http://www.mamicode.com/new byte[200];"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 = "http://www.mamicode.com/this is the user first log the software".ToCharArray();
25                 Encoder e = Encoding.UTF8.GetEncoder();
26                 byteData = http://www.mamicode.com/new byte[charData.Length];>

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

 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         }

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