首页 > 开发 > 综合 > 正文

c#2.0中新增的两个压缩类

2024-07-21 02:26:30
字体:
来源:转载
供稿:网友

.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
    ...{
        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 );

                // 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 );

                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 ( );
            }

        }
    }

,欢迎访问网页设计爱好者web开发。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表