.net 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
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
public void compressfile ( string sourcefile, string destinationfile )
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
// make sure the source file is there
if ( file.exists ( sourcefile ) == false )
throw new filenotfoundexception ( );
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// create the streams and byte arrays needed
byte[] buffer = null;
filestream sourcestream = null;
filestream destinationstream = null;
gzipstream compressedstream = null;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
// read the bytes from the source file into a byte array
sourcestream = new filestream ( sourcefile, filemode.open, fileaccess.read, fileshare.read );
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// read the source stream values into the buffer
buffer = new byte[sourcestream.length];
int checkcounter = sourcestream.read ( buffer, 0, buffer.length );
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
if ( checkcounter != buffer.length )
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
throw new applicationexception ( );
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// open the filestream to write to
destinationstream = new filestream ( destinationfile, filemode.openorcreate, fileaccess.write );
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// create a compression stream pointing to the destiantion stream
compressedstream = new gzipstream ( destinationstream, compressionmode.compress, true );
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// now write the compressed data to the destination file
compressedstream.write ( buffer, 0, buffer.length );
}
catch ( applicationexception ex )
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
messagebox.show ( ex.message, "压缩文件时发生错误:", messageboxbuttons.ok, messageboxicon.error );
}
finally
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
// make sure we allways close all streams
if ( sourcestream != null )
sourcestream.close ( );
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
if ( compressedstream != null )
compressedstream.close ( );
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
if ( destinationstream != null )
destinationstream.close ( );
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public void decompressfile ( string sourcefile, string destinationfile )
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
// make sure the source file is there
if ( file.exists ( sourcefile ) == false )
throw new filenotfoundexception ( );
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// create the streams and byte arrays needed
filestream sourcestream = null;
filestream destinationstream = null;
gzipstream decompressedstream = null;
byte[] quartetbuffer = null;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
// read in the compressed source stream
sourcestream = new filestream ( sourcefile, filemode.open );
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// create a compression stream pointing to the destiantion stream
decompressedstream = new gzipstream ( sourcestream, compressionmode.decompress, true );
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// read the 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 );
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
byte[] buffer = new byte[checklength + 100];
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
int offset = 0;
int total = 0;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// read the compressed data into the buffer
while ( true )
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
int bytesread = decompressedstream.read ( buffer, offset, 100 );
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
if ( bytesread == 0 )
break;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
offset += bytesread;
total += bytesread;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// now write everything to the destination file
destinationstream = new filestream ( destinationfile, filemode.create );
destinationstream.write ( buffer, 0, total );
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// and flush everyhting to clean out the buffer
destinationstream.flush ( );
}
catch ( applicationexception ex )
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
messagebox.show(ex.message, "解压文件时发生错误:", messageboxbuttons.ok, messageboxicon.error);
}
finally
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
// make sure we allways close all streams
if ( sourcestream != null )
sourcestream.close ( );
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
if ( decompressedstream != null )
decompressedstream.close ( );
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
if ( destinationstream != null )
destinationstream.close ( );
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
}
文章整理:
站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!