c#2.0中新增的兩個壓縮類

來源:互聯網
上載者:User
壓縮

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

        }
    }



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.