本文實例講述了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#程序設計有所幫助。
新聞熱點
疑難解答