首页 > 编程技术 > csharp

C#使用DeflateStream解压缩数据文件的方法

发布时间:2020-6-25 11:31

本文实例讲述了C#使用DeflateStream解压缩数据文件的方法。分享给大家供大家参考。具体分析如下:

DeflateStream方法用于从一个流中读取数据,并写入到另一个流。DeflateStream不写入数据到其它类型的资源,比如文件或者内存。 DeflateStream在写入另一个流的时候,它会对数据进行压缩和解压缩。

使用DEFLATE压缩数据文件的一般过程:

打开一个现有的文件 
打开/创建输出文件 
创建减缩对象 
逐字节读取源文件,并把它传递给DEFLATE对象 
使用deflate对象写入输出文件流

String sourcefilename = FILETOBEUNCOMPRESSED;
Filestream sourcefile = File.OpenRead(sourcefilename);
Filestream destinationfile = File.Create(outputfilename);
DeflateStream compressionstream = new DeflateStream(sourcefile,CompressionMode.Decompress);
int sourcebyte = compressionstream.ReadByte();
while(sourcebyte != -1)
{
  destinationfile.WriteByte((byte)sourcebyte);
  sourcebyte = compressionstream.ReadByte();
}

希望本文所述对大家的C#程序设计有所帮助。

标签:[!--infotagslink--]

您可能感兴趣的文章: