壓縮 解壓縮流GZipStream

2021-09-06 07:56:51 字數 4363 閱讀 1660

如果要在壓縮過程中檢查錯誤或要與其他作業系統所用程式共享壓縮資料,則要是用gzipstream類。gzipstream類包含是用gzip資料格式進行壓縮和解壓縮檔案的方法,該類不能用於解壓縮大於4gb的檔案。

一、屬性

basestream       獲取對基礎流的引用。

canread        獲取乙個值,該值指示流是否支援在解壓縮檔案的過程中讀取檔案。 (重寫 stream..::.canread。)

canseek        獲取乙個值,該值指示流是否支援查詢。 (重寫 stream..::.canseek。)

cantimeout       獲取乙個值,該值確定當前流是否可以超時。 (繼承自 stream。)

canwrite         獲取乙個值,該值指示流是否支援寫入。 (重寫 stream..::.canwrite。)

length          不支援,並且總是引發 notsupportedexception。 (重寫 stream..::.length。)

position         不支援,並且總是引發 notsupportedexception。 (重寫 stream..::.position。)

readtimeout       獲取或設定乙個值(以毫秒為單位),該值確定流在超時前嘗試讀取多長時間。 (繼承自 stream。)

writetimeout       獲取或設定乙個值(以毫秒為單位),該值確定流在超時前嘗試寫入多長時間。 (繼承自 stream。)

二、方法

beginread         開始非同步讀操作。 (重寫 stream..::.beginread(array(), int32, int32, asynccallback, object)。)

beginwrite        開始非同步寫操作。 (重寫 stream..::.beginwrite(array(), int32, int32, asynccallback, object)。)

close           關閉當前流並釋放與之關聯的所有資源(如套接字和檔案控制代碼)。 (繼承自 stream。)

createobjref         建立乙個物件,該物件包含生成用於與遠端物件進行通訊的**所需的全部相關資訊。 (繼承自 marshalbyrefobject。)

dispose           已過載。

endread           等待掛起的非同步讀取完成。 (重寫 stream..::.endread(iasyncresult)。)

endwrite          處理非同步寫入的結束。 (重寫 stream..::.endwrite(iasyncresult)。)

flush            將當前 gzipstream 物件的內部緩衝區的內容重新整理到基礎流。 (重寫 stream..::.flush()()()。)

gethashcode        用作特定型別的雜湊函式。 (繼承自 object。)

getlifetimeservice     檢索控制此例項的生存期策略的當前生存期服務物件。 (繼承自 marshalbyrefobject。)

initializelifetimeservice  獲取控制此例項的生存期策略的生存期服務物件。 (繼承自 marshalbyrefobject。)

memberwiseclone     已過載。

read            將若干解壓縮的位元組讀入指定的位元組陣列。 (重寫 stream..::.read(array(), int32, int32)。)

readbyte         從流中讀取乙個位元組,並將流內的位置向前推進乙個位元組,或者如果已到達流的末尾,則返回 -1。 (繼承自 stream。)

seek            此屬性不受支援,並且總是引發 notsupportedexception。 (重寫 stream..::.seek(int64, seekorigin)。)

setlength        此屬性不受支援,並且總是引發 notsupportedexception。 (重寫 stream..::.setlength(int64)。)

write           從指定的位元組陣列中將壓縮的位元組寫入基礎流。 (重寫 stream..::.write(array(), int32, int32)。)

writebyte        將乙個位元組寫入流內的當前位置,並將流內的位置向前推進乙個位元組。 (繼承自 stream。)

練了下手,附上**:(注意釋放資源。) 

static

void main(string

args)

"e:\file.txt", str, encoding.utf8);

"新增完成");

//以上**輸出乙個468k的txt檔案

filestream fs = new filestream(@"

e:\file.txt

", filemode.open, fileaccess.readwrite);

byte bytes = new

byte

[fs.length];

fs.read(bytes,

0,(int

)fs.length);

//壓縮入這個檔案流

filestream fs1 = new filestream(@"

e:\file1.txt

", filemode.createnew, fileaccess.write);

gzipstream gs = new gzipstream(fs1, compressionmode.compress, true

);

//把資料寫入壓縮流

gs.write(bytes, 0, bytes.length); //

寫入之後,會自動呼叫filestream.write()方法 這時候e盤會生成乙個e:\file1.txt檔案,裡面是壓縮之後的內容

console.writeline(fs.length); //

輸出 480024

console.writeline(fs1.length); //

輸出 5142

//壓縮流寫入記憶體

memorystream target = new

memorystream();

gzipstream gzs = new gzipstream(target, compressionmode.compress, true

); gzs.write(bytes,

0, bytes.length); //

相當於呼叫了memorystream的write方法,已將壓縮的資料寫入記憶體

byte bytecompress =target.toarray();

console.writeline(target.length);

//輸出5142

//解壓縮後輸出

filestream fs2 = new filestream(@"

e:\file1.txt

",filemode.open,fileaccess.read);

byte byte1 = new

byte

[fs2.length];

gzipstream gzs1 = new gzipstream(fs2, compressionmode.decompress, true

); streamreader streamr = new

streamreader(gzs1, encoding.default);

string str =streamr.readtoend();

console.writeline(str);

//輸出解壓之後的內容了 好長的

//解壓縮之後用read()是在不知道長度該如何確定

filestream fs3 = new filestream(@"

e:\file1.txt

", filemode.open, fileaccess.read);

byte byte2 = new

byte

[fs2.length];

gzipstream gzs2 = new gzipstream(fs2, compressionmode.decompress, true

); gzs1.read(byte2,

0,byte2.length);

console.writeline(encoding.utf8.getstring(byte2));

//輸出解壓之後的內容了 好長的

console.readkey();

}

壓縮解壓縮

壓縮 壓縮後的檔名 包含物理路徑 待壓縮的資料夾 包含物理路徑 public static void packfiles string filename,string directory catch exception 解壓縮 待解壓檔名 包含物理路徑 解壓到哪個目錄中 包含物理路徑 public ...

壓縮 解壓縮

linux使用最廣泛的壓縮格式位gz,使用gzip命令進行壓縮和解壓縮 1 gzip,gunzip,壓縮 解壓縮檔案,compress or expand files gzip acdfhkllnnrtvv19 s suffix name gunzip acfhkllnnrtvv s suffix ...

Linux 壓縮 解壓縮

1.以.a為副檔名的檔案 tar xv file.a 2.以.z為副檔名的檔案 uncompress file.z 3.以.gz為副檔名的檔案 gunzip file.gz 4.以.bz2為副檔名的檔案 bunzip2 file.bz2 5.以.tar.z為副檔名的檔案 tar xvzf file....