DeflateStream方法用于从一个流中读取数据,并写入到另一个流。DeflateStream不写入数据到其它类型的资源,比如文件或者内存。 DeflateStream在写入另一个流的时候,它会对数据进行压缩和解压缩。
使用DEFLATE压缩数据文件的一般过程
打开一个现有的文件
打开/创建输出文件
创建减缩对象
逐字节读取源文件,并把它传递给DEFLATE对象
使用deflate对象写入输出文件流
String sourcefilename = FILETOBECOMPRESSED;
Filestream sourcefile = File.OpenRead(sourcefilename);
Filestream destinationfile = File.Create(outputfilename);
DeflateStream compressionstream = new DeflateStream(destinationfile, CompressionMode.Compress);
int sourcebyte = sourcefile.ReadByte();
while(sourcebyte != -1)
{
compressionstream.WriteByte((byte)sourcebyte);
sourcebyte = sourcefile.ReadByte();
}
