c#2.0中新增的两个压缩类(转贴)

.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。


下面提供 一个完整的压缩与解压类代码:
None.gif class  clsZip
ExpandedBlockStart.gifContractedBlock.gif    dot.gif
dot.gif {
InBlock.gif        
public void CompressFile ( string sourceFile, string destinationFile )
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
dot.gif{
InBlock.gif            
// make sure the source file is there
InBlock.gif
            if ( File.Exists ( sourceFile ) == false )
InBlock.gif                
throw new FileNotFoundException ( );
InBlock.gif
InBlock.gif            
// Create the streams and byte arrays needed
InBlock.gif
            byte[] buffer = null;
InBlock.gif            FileStream sourceStream 
= null;
InBlock.gif            FileStream destinationStream 
= null;
InBlock.gif            GZipStream compressedStream 
= null;
InBlock.gif
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
dot.gif{
InBlock.gif                
// Read the bytes from the source file into a byte array
InBlock.gif
                sourceStream = new FileStream ( sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read );
InBlock.gif
InBlock.gif                
// Read the source stream values into the buffer
InBlock.gif
                buffer = new byte[sourceStream.Length];
InBlock.gif                
int checkCounter = sourceStream.Read ( buffer, 0, buffer.Length );
InBlock.gif
InBlock.gif                
if ( checkCounter != buffer.Length )
ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif
dot.gif{
InBlock.gif                    
throw new ApplicationException ( );
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
// Open the FileStream to write to
InBlock.gif
                destinationStream = new FileStream ( destinationFile, FileMode.OpenOrCreate, FileAccess.Write );
InBlock.gif
InBlock.gif                
// Create a compression stream pointing to the destiantion stream
InBlock.gif
                compressedStream = new GZipStream ( destinationStream, CompressionMode.Compress, true );
InBlock.gif
InBlock.gif                
// Now write the compressed data to the destination file
InBlock.gif
                compressedStream.Write ( buffer, 0, buffer.Length );
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch ( ApplicationException ex )
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
dot.gif{
InBlock.gif                MessageBox.Show ( ex.Message, 
"压缩文件时发生错误:", MessageBoxButtons.OK, MessageBoxIcon.Error );
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
dot.gif{
InBlock.gif                
// Make sure we allways close all streams
InBlock.gif
                if ( sourceStream != null )
InBlock.gif                    sourceStream.Close ( );
InBlock.gif
InBlock.gif                
if ( compressedStream != null )
InBlock.gif                    compressedStream.Close ( );
InBlock.gif
InBlock.gif                
if ( destinationStream != null )
InBlock.gif                    destinationStream.Close ( );
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void DecompressFile ( string sourceFile, string destinationFile )
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
dot.gif{
InBlock.gif            
// make sure the source file is there
InBlock.gif
            if ( File.Exists ( sourceFile ) == false )
InBlock.gif                
throw new FileNotFoundException ( );
InBlock.gif
InBlock.gif            
// Create the streams and byte arrays needed
InBlock.gif
            FileStream sourceStream = null;
InBlock.gif            FileStream destinationStream 
= null;
InBlock.gif            GZipStream decompressedStream 
= null;
InBlock.gif            
byte[] quartetBuffer = null;
InBlock.gif
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
dot.gif{
InBlock.gif                
// Read in the compressed source stream
InBlock.gif
                sourceStream = new FileStream ( sourceFile, FileMode.Open );
InBlock.gif
InBlock.gif                
// Create a compression stream pointing to the destiantion stream
InBlock.gif
                decompressedStream = new GZipStream ( sourceStream, CompressionMode.Decompress, true );
InBlock.gif
InBlock.gif                
// Read the footer to determine the length of the destiantion file
InBlock.gif
                quartetBuffer = new byte[4];
InBlock.gif                
int position = (int)sourceStream.Length - 4;
InBlock.gif                sourceStream.Position 
= position;
InBlock.gif                sourceStream.Read ( quartetBuffer, 
04 );
InBlock.gif                sourceStream.Position 
= 0;
InBlock.gif                
int checkLength = BitConverter.ToInt32 ( quartetBuffer, 0 );
InBlock.gif
InBlock.gif                
byte[] buffer = new byte[checkLength + 100];
InBlock.gif
InBlock.gif                
int offset = 0;
InBlock.gif                
int total = 0;
InBlock.gif
InBlock.gif                
// Read the compressed data into the buffer
InBlock.gif
                while ( true )
ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif
dot.gif{
InBlock.gif                    
int bytesRead = decompressedStream.Read ( buffer, offset, 100 );
InBlock.gif
InBlock.gif                    
if ( bytesRead == 0 )
InBlock.gif                        
break;
InBlock.gif
InBlock.gif                    offset 
+= bytesRead;
InBlock.gif                    total 
+= bytesRead;
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
// Now write everything to the destination file
InBlock.gif
                destinationStream = new FileStream ( destinationFile, FileMode.Create );
InBlock.gif                destinationStream.Write ( buffer, 
0, total );
InBlock.gif
InBlock.gif                
// and flush everyhting to clean out the buffer
InBlock.gif
                destinationStream.Flush ( );
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch ( ApplicationException ex )
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
dot.gif{
InBlock.gif                MessageBox.Show(ex.Message, 
"解压文件时发生错误:", MessageBoxButtons.OK, MessageBoxIcon.Error);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
dot.gif{
InBlock.gif                
// Make sure we allways close all streams
InBlock.gif
                if ( sourceStream != null )
InBlock.gif                    sourceStream.Close ( );
InBlock.gif
InBlock.gif                
if ( decompressedStream != null )
InBlock.gif                    decompressedStream.Close ( );
InBlock.gif
InBlock.gif                
if ( destinationStream != null )
InBlock.gif                    destinationStream.Close ( );
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

转载于:https://www.cnblogs.com/raymond19840709/archive/2007/04/06/702307.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值