闲谈.NET Framework Compression功能应用技巧
创始人
2024-06-19 09:00:40
0

.NET Framework能为开发人员提供一个合适的WEB应用程序部署平台,帮助他们轻松的完成各种程序的开发创建。以前做项目的时候,需要提供文件压缩功能。当时是使用了一个开源的类库,名为ZipLib,使用起来还是很方便的。在.Net 2.0中,微软在System.IO中新增了System.IO.Compression命名空间,.NET Framework Compression功能提供了压缩功能的相关类GZipStream。 #t#

这个类的使用与一般的文件流使用差不多。我没有分析其内部实现,但猜测应该还是采用Decorator模式对Stream进行了装饰,从中应用了.NET Framework Compression功能的算法。它通过Write()方法,将buffer里面的内容写到另一个文件流中,例如源文件为sourceFile,压缩后的文件为targetFile,则方法为:

  1. byte[] buffer = null;   
  2. FileStream sourceStream = null;   
  3. FileStream targetStream = null;   
  4. GZipStream compressedStream = null;   
  5. sourceStream = new FileStream
    (sourceFile,FileMode.Open,FileAccess.
    Read,FileShare.Read);   
  6. buffer = new byte[sourceStream.Length];   
  7. sourceStream.Read(buffer,0,buffer.Length);   
  8. targetStream = new FileStream
    (targetFile,FileMode.OpenOrCreate,
    FileAccess.Write);   
  9. //将CompressedStream指向targetStream;   
  10. compressedStream = new GZipStream
    (targetStream,CompressionMode.
    Compress,true);  
  11. compressStream.Write(buffer,0,
    buffer.Length); 

在使用GZipStream时,需要添加引用:

  1. using System.IO; 
  2. using System.IO.Compression; 

.NET Framework Compression功能的解压缩与前面的方法差不多,仍然使用GZipStream文件流:

 

  1. // Read in the compressed source stream   
  2. sourceStream = new FileStream 
    ( sourceFile, FileMode.Open );   
  3. // Create a compression stream pointing 
    to the destiantion stream   
  4. decompressedStream = new GZipStream 
    ( sourceStream, CompressionMode.
    Decompress, true );   
  5. // Read the footer to determine the 
    length of the destiantion file   
  6. quartetBuffer = new byte[4];   
  7. int position = (int)sourceStream.Length - 4;  
  8. sourceStream.Position = position;   
  9. sourceStream.Read ( quartetBuffer, 0, 4 );  
  10. sourceStream.Position = 0;   
  11. int checkLength = BitConverter.ToInt32 
    ( quartetBuffer, 0 );   
  12. byte[] buffer = new byte[checkLength + 100];   
  13. int offset = 0;   
  14. int total = 0;   
  15. // Read the compressed data into the buffer   
  16. while ( true )   
  17. {   
  18. int bytesRead = decompressedStream.Read 
    ( buffer, offset, 100 );   
  19. if ( bytesRead == 0 ) break;   
  20. offset += bytesRead; total += bytesRead;   
  21. }   
  22. // Now write everything to the destination file  
  23. destinationStream = new FileStream 
    ( destinationFile, FileMode.Create );   
  24. destinationStream.Write ( buffer, 0, total );   
  25. // and flush everyhting to clean out the buffer  
  26. destinationStream.Flush ( ); 

怎么样,通过对.NET Framework Compression功能的介绍,大家应该基本掌握了其中的应用技巧了吧。

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...