国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

c#2.0中新增的兩個壓縮類(downmoon原創)

2024-07-21 02:27:02
字體:
來源:轉載
供稿:網友

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

        }
    }

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 旬阳县| 宁蒗| 涿鹿县| 富顺县| 阳谷县| 安达市| 南召县| 体育| 安阳县| 高陵县| 甘孜县| 鄂托克前旗| 司法| 和平区| 边坝县| 建始县| 乃东县| 沂南县| 阿勒泰市| 自贡市| 曲麻莱县| 鹤壁市| 辰溪县| 长子县| 准格尔旗| 绍兴县| 尉氏县| 云安县| 罗平县| 白朗县| 潼关县| 澜沧| 建始县| 闵行区| 河北省| 遵义县| 大同市| 耿马| 易门县| 罗定市| 庐江县|