C#使用DeflateStream解压缩数据文件的方法
本文实例讲述了C#使用DeflateStream解压缩数据文件的方法。分享给大家供大家参考。具体分析如下:
DeflateStream方法用于从一个流中读取数据,并写入到另一个流。DeflateStream不写入数据到其它类型的资源,比如文件或者内存。DeflateStream在写入另一个流的时候,它会对数据进行压缩和解压缩。
使用DEFLATE压缩数据文件的一般过程:
打开一个现有的文件
打开/创建输出文件
创建减缩对象
逐字节读取源文件,并把它传递给DEFLATE对象
使用deflate对象写入输出文件流
Stringsourcefilename=FILETOBEUNCOMPRESSED;
Filestreamsourcefile=File.OpenRead(sourcefilename);
Filestreamdestinationfile=File.Create(outputfilename);
DeflateStreamcompressionstream=newDeflateStream(sourcefile,CompressionMode.Decompress);
intsourcebyte=compressionstream.ReadByte();
while(sourcebyte!=-1)
{
destinationfile.WriteByte((byte)sourcebyte);
sourcebyte=compressionstream.ReadByte();
}
希望本文所述对大家的C#程序设计有所帮助。