自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

閑談.NET Framework Compression功能應(yīng)用技巧

開發(fā) 后端
.NET Framework Compression功能是微軟公司新增的一個(gè)功能??梢詭椭覀儗?shí)現(xiàn)文件數(shù)據(jù)的壓縮等等。下面就一起來看看具體的應(yīng)用技巧。

.NET Framework能為開發(fā)人員提供一個(gè)合適的WEB應(yīng)用程序部署平臺(tái),幫助他們輕松的完成各種程序的開發(fā)創(chuàng)建。以前做項(xiàng)目的時(shí)候,需要提供文件壓縮功能。當(dāng)時(shí)是使用了一個(gè)開源的類庫(kù),名為ZipLib,使用起來還是很方便的。在.Net 2.0中,微軟在System.IO中新增了System.IO.Compression命名空間,.NET Framework Compression功能提供了壓縮功能的相關(guān)類GZipStream。 #t#

這個(gè)類的使用與一般的文件流使用差不多。我沒有分析其內(nèi)部實(shí)現(xiàn),但猜測(cè)應(yīng)該還是采用Decorator模式對(duì)Stream進(jìn)行了裝飾,從中應(yīng)用了.NET Framework Compression功能的算法。它通過Write()方法,將buffer里面的內(nèi)容寫到另一個(gè)文件流中,例如源文件為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時(shí),需要添加引用:

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

怎么樣,通過對(duì).NET Framework Compression功能的介紹,大家應(yīng)該基本掌握了其中的應(yīng)用技巧了吧。

責(zé)任編輯:曹凱 來源: IT168
相關(guān)推薦

2010-01-06 18:54:41

脫離.Net Fram

2010-01-05 10:17:35

.NET Framew

2010-01-06 10:23:47

.NET Framew

2010-01-05 15:00:30

.NET Framew

2010-01-06 17:02:28

.Net Framew

2010-01-05 17:30:23

.NET Framew

2010-01-05 16:20:46

.NET Framew

2010-01-06 18:33:56

.Net Framew

2010-01-05 17:59:54

.NET Framew

2010-01-06 19:18:22

.NET Framew

2010-01-05 10:55:50

.NET Framew

2009-12-10 14:04:09

.Net Framew

2010-01-05 15:35:21

.NET Framew

2010-01-06 09:54:30

.NET Framew

2010-01-05 15:27:04

.NET Framew

2009-12-31 15:13:47

Silverlight

2010-01-07 17:18:19

VB.NET cstr

2010-01-05 16:31:45

.NET Framew

2009-12-30 10:25:03

Silverlight

2009-08-05 10:17:55

ASP.NET TheASP.NET開發(fā)技巧
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)