本文實例講述了C#使用GZipStream解壓縮數據文件的方法。分享給大家供大家參考。具體分析如下:
GZipStream用于從一個流讀取數據寫入到另一個流,GZipStream不能寫入到其它的資源,比如文件或者內存,只能從流到流。
GZipStream使用的一般流程如下:
打開一個現有的文件
打開/創建輸出文件
創建GZipStream對象
逐字節讀源文件,并把它傳遞到GZipStream
使用GZipStream寫入到輸出文件流
String sourcefilename = FILETOBEUNCOMPRESSED;Filestream sourcefile = File.OpenRead(sourcefilename);Filestream destinationfile = File.Create(outputfilename);GZipStream compressionstream = new GZipStream(sourcefile, CompressionMode.Decompress);int sourcebyte = compressionstream.ReadByte();while(sourcebyte != -1){ destinationfile.WriteByte((byte)sourcebyte); sourcebyte = compressionstream.ReadByte();}
希望本文所述對大家的C#程序設計有所幫助。
新聞熱點
疑難解答