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原作 )
classclsZip
{
publicvoidCompressFile(stringsourceFilestringdestinationFile)
{
//makesurethesourcefileisthere
if(FileExists(sourceFile)==false)
thrownewFileNotFoundException();
//Createthestreamsandbytearraysneeded
byte[]buffer=null;
FileStreamsourceStream=null;
FileStreamdestinationStream=null;
GZipStreamcompressedStream=null;
try
{
//Readthebytesfromthesourcefileintoabytearray
sourceStream=newFileStream(sourceFileFileModeOpenFileAccessReadFileShareRead);
//Readthesourcestreamvaluesintothebuffer
buffer=newbyte[sourceStreamLength];
intcheckCounter=sourceStreamRead(bufferbufferLength);
if(checkCounter!=bufferLength)
{
thrownewApplicationException();
}
//OpentheFileStreamtowriteto
destinationStream=newFileStream(destinationFileFileModeOpenOrCreateFileAccessWrite);
//Createacompressionstreampointingtothedestiantionstream
compressedStream=newGZipStream(destinationStreamCompressionModeCompresstrue);
//Nowwritethecompresseddatatothedestinationfile
compressedStreamWrite(bufferbufferLength);
}
catch(ApplicationExceptionex)
{
MessageBoxShow(exMessage压缩文件时发生错误MessageBoxButtonsOKMessageBoxIconError);
}
finally
{
//Makesureweallwayscloseallstreams
if(sourceStream!=null)
sourceStreamClose();
if(compressedStream!=null)
compressedStreamClose();
if(destinationStream!=null)
destinationStreamClose();
}
}
publicvoidDecompressFile(stringsourceFilestringdestinationFile)
{
//makesurethesourcefileisthere
if(FileExists(sourceFile)==false)
thrownewFileNotFoundException();
//Createthestreamsandbytearraysneeded
FileStreamsourceStream=null;
FileStreamdestinationStream=null;
GZipStreamdecompressedStream=null;
byte[]quartetBuffer=null;
try
{
//Readinthecompressedsourcestream
sourceStream=newFileStream(sourceFileFileModeOpen);
//Createacompressionstreampointingtothedestiantionstream
decompressedStream=newGZipStream(sourceStreamCompressionModeDecompresstrue);
//Readthefootertodeterminethelengthofthedestiantionfile
quartetBuffer=newbyte[];
intposition=(int)sourceStreamLength;
sourceStreamPosition=position;
sourceStreamRead(quartetBuffer);
sourceStreamPosition=;
intcheckLength=BitConverterToInt(quartetBuffer);
byte[]buffer=newbyte[checkLength+];
intoffset=;
inttotal=;
//Readthecompresseddataintothebuffer
while(true)
{
intbytesRead=decompressedStreamRead(bufferoffset);
if(bytesRead==)
break;
offset+=bytesRead;
total+=bytesRead;
}
//Nowwriteeverythingtothedestinationfile
destinationStream=newFileStream(destinationFileFileModeCreate);
destinationStreamWrite(buffertotal);
//andflusheveryhtingtocleanoutthebuffer
destinationStreamFlush();
}
catch(ApplicationExceptionex)
{
MessageBoxShow(exMessage解压文件时发生错误MessageBoxButtonsOKMessageBoxIconError);
}
finally
{
//Makesureweallwayscloseallstreams
if(sourceStream!=null)
sourceStreamClose();
if(decompressedStream!=null)
decompressedStreamClose();
if(destinationStream!=null)
destinationStreamClose();
}
}
}