NET Framework 中新增的兩個壓縮類
SystemIOCompression 命名空間
注意此命名空間在 NET Framework 版中是新增的
SystemIOCompression 命名空間包含提供基本的流壓縮和解壓縮服務的類
(downmoon原作)
類 說明
DeflateStream 提供用於使用 Deflate 算法壓縮和解壓縮流的方法和屬性
GZipStream 提供用於壓縮和解壓縮流的方法和屬性
枚舉 說明
CompressionMode 指定是否壓縮或解壓縮基礎流
下面以 GZipStream 為例說明
注意此類在 NET Framework 版中是新增的
提供用於壓縮和解壓縮流的方法和屬性
命名空間:SystemIOCompression
程序集:System(在 systemdll 中)
語法
Visual Basic(聲明)
Public Class GZipStream
Inherits Stream
Visual Basic(用法)
Dim instance As GZipStream
C#
public class GZipStream : Stream
C++
public ref class GZipStream : public Stream
J#
public class GZipStream extends Stream
JScript
public class GZipStream extends Stream
備注
此類表示 GZip 數據格式它使用無損壓縮和解壓縮文件的行業標准算法這種格式包括一個檢測數據損壞的循環冗余校驗值GZip 數據格式使用的算法與 DeflateStream 類的算法相同但它可以擴展以使用其他壓縮格式這種格式可以通過不涉及專利使用權的方式輕松實現gzip 的格式可以從 RFC GZIP file format specification (GZIP 文件格式規范 )GZIP file format specification (GZIP 文件格式規范 )中獲得此類不能用於壓縮大於 GB 的文件
給繼承者的說明 當從 GZipStream 繼承時必須重寫下列成員CanSeekCanWrite 和 CanRead
下面提供 一個完整的壓縮與解壓類(downmoon原作 )

class clsZip

{

public void CompressFile ( string sourceFile
string destinationFile )

{

// make sure the source file is there

if ( File
Exists ( sourceFile ) == false )

throw new FileNotFoundException ( );


// Create the streams and byte arrays needed

byte[] buffer = null;

FileStream sourceStream = null;

FileStream destinationStream = null;

GZipStream compressedStream = null;


try

{

// Read the bytes from the source file into a byte array

sourceStream = new FileStream ( sourceFile
FileMode
Open
FileAccess
Read
FileShare
Read );


// Read the source stream values into the buffer

buffer = new byte[sourceStream
Length];

int checkCounter = sourceStream
Read ( buffer
buffer
Length );


if ( checkCounter != buffer
Length )

{

throw new ApplicationException ( );

}


// Open the FileStream to write to

destinationStream = new FileStream ( destinationFile
FileMode
OpenOrCreate
FileAccess
Write );


// Create a compression stream pointing to the destiantion stream

compressedStream = new GZipStream ( destinationStream
CompressionMode
Compress
true );


// Now write the compressed data to the destination file

compressedStream
Write ( buffer
buffer
Length );

}

catch ( ApplicationException ex )

{

MessageBox
Show ( ex
Message
壓縮文件時發生錯誤
MessageBoxButtons
OK
MessageBoxIcon
Error );

}

finally

{

// Make sure we allways close all streams

if ( sourceStream != null )

sourceStream
Close ( );


if ( compressedStream != null )

compressedStream
Close ( );


if ( destinationStream != null )

destinationStream
Close ( );

}

}


public void DecompressFile ( string sourceFile
string destinationFile )

{

// make sure the source file is there

if ( File
Exists ( sourceFile ) == false )

throw new FileNotFoundException ( );


// Create the streams and byte arrays needed

FileStream sourceStream = null;

FileStream destinationStream = null;

GZipStream decompressedStream = null;

byte[] quartetBuffer = null;


try

{

// Read in the compressed source stream

sourceStream = new FileStream ( sourceFile
FileMode
Open );


// Create a compression stream pointing to the destiantion stream

decompressedStream = new GZipStream ( sourceStream
CompressionMode
Decompress
true );


// Read the footer to determine the length of the destiantion file

quartetBuffer = new byte[
];

int position = (int)sourceStream
Length
;

sourceStream
Position = position;

sourceStream
Read ( quartetBuffer
);

sourceStream
Position =
;

int checkLength = BitConverter
ToInt
( quartetBuffer
);


byte[] buffer = new byte[checkLength +
];


int offset =
;

int total =
;


// Read the compressed data into the buffer

while ( true )

{

int bytesRead = decompressedStream
Read ( buffer
offset
);


if ( bytesRead ==
)

break;


offset += bytesRead;

total += bytesRead;

}


// Now write everything to the destination file

destinationStream = new FileStream ( destinationFile
FileMode
Create );

destinationStream
Write ( buffer
total );


// and flush everyhting to clean out the buffer

destinationStream
Flush ( );

}

catch ( ApplicationException ex )

{

MessageBox
Show(ex
Message
解壓文件時發生錯誤
MessageBoxButtons
OK
MessageBoxIcon
Error);

}

finally

{

// Make sure we allways close all streams

if ( sourceStream != null )

sourceStream
Close ( );


if ( decompressedStream != null )

decompressedStream
Close ( );


if ( destinationStream != null )

destinationStream
Close ( );

}


}

}
From:http://tw.wingwit.com/Article/program/net/201311/12971.html