framework 2.0 中新增的兩個壓縮類
system.io.compression 命名空間
注意:此命名空間在 .Net framework 2.0 版中是新增的。
system.io.compression 命名空間包含提供基本的流壓縮和解壓縮服務的類。
(downmoon原作)
類 說明
deflatestream 提供用於使用 deflate 算法壓縮和解壓縮流的方法和屬性。
gzipstream 提供用於壓縮和解壓縮流的方法和屬性。
枚舉 說明
compressionmode 指定是否壓縮或解壓縮基礎流。
下面以 gzipstream 為例說明
注意:此類在 .Net framework 2.0 版中是新增的。
提供用於壓縮和解壓縮流的方法和屬性。
命名空間:system.io.compression
程序集:system(在 system.dll 中)
語法
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 1952“gzip file format specification 4.3(gzip 文件格式規范 4.3)gzip file format specification 4.3(gzip 文件格式規范 4.3)”中獲得。此類不能用於壓縮大於 4 gb 的文件。
給繼承者的說明 當從 gzipstream 繼承時,必須重寫下列成員:canseek、canwrite 和 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, 0, 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, 0, 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 );


&nbsthe footer to determine the length of the destiantion file

quartetbuffer = new byte[4];

int position = (int)sourcestream.length - 4;

sourcestream.position = position;

sourcestream.read ( quartetbuffer, 0, 4 );

sourcestream.position = 0;

int checklength = bitconverter.toint32 ( quartetbuffer, 0 );


byte[] buffer = new byte[checklength + 100];


int offset = 0;

int total = 0;


// read the compressed data into the buffer

while ( true )


...{

int bytesread = decompressedstream.read ( buffer, offset, 100 );


if ( bytesread == 0 )

break;


offset += bytesread;

total += bytesread;

}

// now write everything to the destination file

destinationstream = new filestream ( destinationfile, filemode.create );

destinationstream.write ( buffer, 0, 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 ( );

}


}

}

文章整理:站長天空 網址:http://www.z6688.com/
以上信息與文章正文是不可分割的一部分,如果您要轉載本文章,請保留以上信息,謝謝!


/**//// <summary>

/// FileZipLib 壓縮,解壓縮的類

/// </summary>

public class FileZipLib


...{


public FileZipLib() ...{}


/**//// <summary>

/// 創建一個壓縮文件

/// </summary>

/// <param name="zipFilename">壓縮後的文件名</param>

/// <param name="sourceDirectory">待壓縮文件的所在目錄</param>

public static voidPackFiles(string zipFilename,string sourceDirectory)


...{

FastZip fz = new FastZip() ;

fz.CreateEmptyDirectorIEs = true ;

fz.CreateZip(zipFilename,sourceDirectory,true,"") ;

fz = null ;

}



/**//// <summary>

/// 解壓縮文件

/// </summary>

/// <param name="zipFile">待解壓縮的文件</param>

/// <param name="directory">解壓縮後文件存放的目錄</param>

public static bool UnpackFiles(string zipFile,string directory)


...{

if( !Directory.Exists(directory) )

Directory.CreateDirectory(directory) ;


ZipInputStream zis = new ZipInputStream( File.OpenRead(zipFile) ) ;

ZipEntry theEntry = null ;

while( (theEntry = zis.GetNextEntry()) != null )


...{

string directoryName = Path.GetDirectoryName(theEntry.Name) ;

string fileName = Path.GetFileName(theEntry.Name) ;

if( directoryName != string.Empty )

Directory.CreateDirectory(directory + directoryName) ;


if( fileName != string.Empty )


...{

FileStream streamWriter = File.Create( Path.Combine( directory,theEntry.Name) ) ;

int size = 2048 ;

byte[] data = new byte[size] ;

while ( true )


...{

size = zis.Read(data,0,data.Length) ;

if( size > 0 )

streamWriter.Write( data,0,size ) ;

else

break ;

}


streamWriter.Close() ;

}

}


zis.Close() ;

return true ;

}

}

//最後別忘了using ICSharpCode.SharpZipLib.Zip ;
