C#对文件操作(基本的读写以及压缩和解压)
2017-04-15 19:32
639 查看
主要是针对单个文件进行读写操作和压缩操作:用到的主要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 }
相关文章推荐
- C#对文件操作(基本的读写以及压缩和解压)
- C# 压缩 解压 复制文件夹,文件的操作
- C# 文件的一些基本操作(转)//用C#读写ini配置文件
- C#文件的压缩和解压(ZIP)使用DotNetZip封装类操作zip文件(创建/读取/更新)实例
- c# 动态操作 zip\rar 文件,压缩解压组件使用
- C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志
- Java对文件的随机读写以及压缩处理操作
- c#程序操作文件进行压缩和解压
- C#txt文件读写基本操作
- C# ini文件读写基本操作
- C#压缩与解压文件源码
- C#文件读写IO操作 实例
- 通过C#代码 压缩/解压文件
- (转载)C#利用ICSharpCode.SharpZipLib.dll压缩文件和解压文件
- C#操作压缩文件(rar,zip,gzip,winform/asp.net)
- C#压缩解压zip 文件
- C# 对sharepoint 列表的一些基本操作,包括添加/删除/查询/上传文件给sharepoint list添加数据
- 使用C#2.0进行文件压缩和解压
- c#文件的一些基本操作(转)
- 使用C#2.0进行文件压缩和解压