资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

.NETFrameworkCompression功能应用技巧是什么

.NET Framework Compression功能应用技巧是什么,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

公司主营业务:成都网站设计、成都网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联推出荆州免费做网站回馈大家。

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

这个类的使用与一般的文件流使用差不多。我没有分析其内部实现,但猜测应该还是采用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时,需要添加引用:

using System.IO; 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功能应用技巧是什么问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


本文名称:.NETFrameworkCompression功能应用技巧是什么
本文URL:http://cdkjz.cn/article/igepcj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220