日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
閑談.NETFrameworkCompression功能應(yīng)用技巧

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

這個(gè)類的使用與一般的文件流使用差不多。我沒(méi)有分析其內(nèi)部實(shí)現(xiàn),但猜測(cè)應(yīng)該還是采用Decorator模式對(duì)Stream進(jìn)行了裝飾,從中應(yīng)用了.NET Framework Compression功能的算法。它通過(guò)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. int bytesRead = decompressedStream.Read 
    ( buffer, offset, 100 ); 
  18. if ( bytesRead == 0 ) break; 
  19. offset += bytesRead; total += bytesRead; 
  20. // Now write everything to the destination file
  21. destinationStream = new FileStream 
    ( destinationFile, FileMode.Create ); 
  22. destinationStream.Write ( buffer, 0, total ); 
  23. // and flush everyhting to clean out the buffer
  24. destinationStream.Flush ( );

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


網(wǎng)頁(yè)標(biāo)題:閑談.NETFrameworkCompression功能應(yīng)用技巧
標(biāo)題路徑:http://www.5511xx.com/article/coeschh.html