日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

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

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#利用sharpziplib的方法淺析

C#語言有很多值得學(xué)習(xí)的地方,這里我們主要介紹C#利用sharpziplib,包括介紹壓縮和解壓縮的兩個類,分別為 ZipClass和UnZipClass等方面。

創(chuàng)新互聯(lián)公司服務(wù)電話:18982081108,為您提供成都網(wǎng)站建設(shè)網(wǎng)頁設(shè)計及定制高端網(wǎng)站建設(shè)服務(wù),創(chuàng)新互聯(lián)公司網(wǎng)頁制作領(lǐng)域十年,包括成都餐廳設(shè)計等多個方面擁有多年的網(wǎng)站推廣經(jīng)驗,選擇創(chuàng)新互聯(lián)公司,為網(wǎng)站錦上添花!

在做項目的時候需要將文件進(jìn)行壓縮和解壓縮,于是就從http://www.icsharpcode.net下載了關(guān)于壓縮和解壓縮的源碼,但是下載下來后,面對這么多的代碼,一時不知如何下手。只好耐下心來,慢慢的研究,總算找到了門路。針對自己的需要改寫了文件壓縮和解壓縮的兩個類,分別為 ZipClass和UnZipClass。其中碰到了不少困難,就決定寫出來壓縮和解壓的程序后,一定把源碼貼出來共享,讓***接觸壓縮和解壓縮的朋友可以少走些彎路。下面就來解釋如何C#利用sharpziplib進(jìn)行文件的壓縮和解壓縮。

首先需要在項目里C#利用sharpziplib.dll。然后修改其中的關(guān)于壓縮和解壓縮的類。實現(xiàn)源碼如下:

 
 
 
  1. /// 
  2. /// 壓縮文件
  3. /// 
  4. using System;
  5. using System.IO;
  6. using ICSharpCode.SharpZipLib.Checksums;
  7. using ICSharpCode.SharpZipLib.Zip;
  8. using ICSharpCode.SharpZipLib.GZip;
  9. namespace Compression
  10. {
  11. public class ZipClass
  12. {
  13. public void ZipFile(string FileToZip, string ZipedFile ,
    int CompressionLevel, int BlockSize)
  14. {
  15. //如果文件沒有找到,則報錯
  16. if (! System.IO.File.Exists(FileToZip))
  17. {
  18. throw new System.IO.FileNotFoundException
    ("The specified file " + FileToZip + " could not be found. Zipping aborderd");
  19. }
  20. system.io.filestream StreamToZip = new System.IO.FileStream
    (FileToZip,System.IO.FileMode.Open , System.IO.FileAccess.Read);
  21. System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile);
  22. ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
  23. ZipEntry ZipEntry = new ZipEntry("ZippedFile");
  24. ZipStream.PutNextEntry(ZipEntry);
  25. ZipStream.SetLevel(CompressionLevel);
  26. byte[] buffer = new byte[BlockSize];
  27. System.Int32 size =StreamToZip.Read(buffer,0,buffer.Length);
  28. ZipStream.Write(buffer,0,size);
  29. try
  30. {
  31. while (size < StreamToZip.Length)
  32. {
  33. int sizeRead =StreamToZip.Read(buffer,0,buffer.Length);
  34. ZipStream.Write(buffer,0,sizeRead);
  35. size += sizeRead;
  36. }
  37. }
  38. catch(System.Exception ex)
  39. {
  40. throw ex;
  41. }
  42. ZipStream.Finish();
  43. ZipStream.Close();
  44. StreamToZip.Close();
  45. }
  46. public void ZipFileMain(string[] args)
  47. {
  48. string[] filenames = Directory.GetFiles(args[0]);
  49. crc32 crc = new Crc32();
  50. ZipOutputStream s = new ZipOutputStream(File.Create(args[1]));
  51. s.setlevel(6); // 0 - store only to 9 - means best compression
  52. foreach (string file in filenames)
  53. {
  54. //打開壓縮文件
  55. FileStream fs = File.OpenRead(file);
  56. byte[] buffer = new byte[fs.Length];
  57. fs.Read(buffer, 0, buffer.Length);
  58. ZipEntry entry = new ZipEntry(file);
  59. entry.datetime = DateTime.Now;
  60. // set Size and the crc, because the information
  61. // about the size and crc should be stored in the header
  62. // if it is not set it is automatically written in the footer.
  63. // (in this case size == crc == -1 in the header)
  64. // Some ZIP programs have problems with zip files that don't store
  65. // the size and crc in the header.
  66. entry.Size = fs.Length;
  67. fs.Close();
  68. crc.reset();
  69. crc.Update(buffer);
  70. entry.crc = crc.Value;
  71. s.putnextentry(entry);
  72. s.write(buffer, 0, buffer.Length);
  73. }
  74. s.finish();
  75. s.Close();
  76. }
  77. }

分享文章:C#利用sharpziplib的方法淺析
轉(zhuǎn)載來于:http://www.dlmjj.cn/article/dhjpdpo.html