溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C#使用SharpZipLib的方法

發(fā)布時間:2021-06-16 14:15:30 來源:億速云 閱讀:218 作者:chen 欄目:編程語言

本篇內(nèi)容介紹了“C#使用SharpZipLib的方法”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

學(xué)習(xí)C#語言時,經(jīng)常會遇到文件的壓縮和解壓縮問題,這里將介紹C#使用SharpZipLib進(jìn)行文件的壓縮和解壓縮。

C#使用SharpZipLib進(jìn)行文件的壓縮和解壓縮

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

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

  1. /// <summary> 

  2. /// 壓縮文件  

  3. /// </summary> 

  4.  

  5. using System;  

  6. using System.IO;  

  7.  

  8. using ICSharpCode.SharpZipLib.Checksums;  

  9. using ICSharpCode.SharpZipLib.Zip;  

  10. using ICSharpCode.SharpZipLib.GZip;  

  11.  

  12. namespace Compression  

  13. {  

  14. public class ZipClass  

  15. {  

  16.  

  17. public void ZipFile(string FileToZip, string ZipedFile ,
    int CompressionLevel, int BlockSize)  

  18. {  

  19. //如果文件沒有找到,則報錯  

  20. if (! System.IO.File.Exists(FileToZip))  

  21. {  

  22. throw new System.IO.FileNotFoundException
    ("The specified file " + FileToZip + " could not be found. Zipping aborderd");  

  23. }  

  24.  

  25. system.io.filestream StreamToZip = new System.IO.FileStream
    (FileToZip,System.IO.FileMode.Open , System.IO.FileAccess.Read);  

  26. System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile);  

  27. ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);  

  28. ZipEntry ZipEntry = new ZipEntry("ZippedFile");  

  29. ZipStream.PutNextEntry(ZipEntry);  

  30. ZipStream.SetLevel(CompressionLevel);  

  31. byte[] buffer = new byte[BlockSize];  

  32. System.Int32 size =StreamToZip.Read(buffer,0,buffer.Length);  

  33. ZipStream.Write(buffer,0,size);  

  34. try  

  35. {  

  36. while (size < StreamToZip.Length)  

  37. {  

  38. int sizeRead =StreamToZip.Read(buffer,0,buffer.Length);  

  39. ZipStream.Write(buffer,0,sizeRead);  

  40. size += sizeRead;  

  41. }  

  42. }  

  43. catch(System.Exception ex)  

  44. {  

  45. throw ex;  

  46. }  

  47. ZipStream.Finish();  

  48. ZipStream.Close();  

  49. StreamToZip.Close();  

  50. }  

  51.  

  52. public void ZipFileMain(string[] args)  

  53. {  

  54. string[] filenames = Directory.GetFiles(args[0]);  

  55.  

  56. crc32 crc = new Crc32();  

  57. ZipOutputStream s = new ZipOutputStream(File.Create(args[1]));  

  58.  

  59. s.setlevel(6); // 0 - store only to 9 - means best compression  

  60.  

  61. foreach (string file in filenames)  

  62. {  

  63. //打開壓縮文件  

  64. FileStream fs = File.OpenRead(file);  

  65.  

  66. byte[] buffer = new byte[fs.Length];  

  67. fs.Read(buffer, 0, buffer.Length);  

  68. ZipEntry entry = new ZipEntry(file);  

  69.  

  70. entry.datetime = DateTime.Now;  

  71.  

  72. // set Size and the crc, because the information  

  73. // about the size and crc should be stored in the header  

  74. // if it is not set it is automatically written in the footer.  

  75. // (in this case size == crc == -1 in the header)  

  76. // Some ZIP programs have problems with zip files that don't store  

  77. // the size and crc in the header.  

  78. entry.Size = fs.Length;  

  79. fs.Close();  

  80.  

  81. crc.reset();  

  82. crc.Update(buffer);  

  83.  

  84. entry.crc = crc.Value;  

  85.  

  86. s.putnextentry(entry);  

  87.  

  88. s.write(buffer, 0, buffer.Length);  

  89.  

  90. }  

  91.  

  92. s.finish();  

  93. s.Close();  

  94. }  

  95. }  

  96. }  

  97.  

  98. 現(xiàn)在再來看看解壓文件類的源碼  

  99.  

  100. /// <summary> 

  101. /// 解壓文件  

  102. /// </summary> 

  103.  

  104. using System;  

  105. using System.Text;  

  106. using System.Collections;  

  107. using System.IO;  

  108. using System.Diagnostics;  

  109. using System.Runtime.Serialization.Formatters.Binary;  

  110. using System.Data;  

  111.  

  112. using ICSharpCode.SharpZipLib.BZip2;  

  113. using ICSharpCode.SharpZipLib.Zip;  

  114. using ICSharpCode.SharpZipLib.Zip.Compression;  

  115. using ICSharpCode.SharpZipLib.Zip.Compression.Streams;  

  116. using ICSharpCode.SharpZipLib.GZip;  

  117.  

  118. namespace DeCompression  

  119. {  

  120. public class UnZipClass  

  121. {  

  122. public void UnZip(string[] args)  

  123. {  

  124. ZipInputStream s = new ZipInputStream(File.OpenRead(args[0]));  

  125.  

  126. zipentry theEntry;  

  127. while ((theEntry = s.GetNextEntry()) != null)  

  128. {  

  129.  

  130. string directoryName = Path.GetDirectoryName(args[1]);  

  131. string fileName = Path.GetFileName(theEntry.Name);  

  132.  

  133. //生成解壓目錄  

  134. Directory.CreateDirectory(directoryName);  

  135.  

  136. if (fileName != String.Empty)  

  137. {  

  138. //解壓文件到指定的目錄  

  139. FileStream streamWriter = File.Create(args[1]+theEntry.Name);  

  140.  

  141. int size = 2048;  

  142. byte[] data = new byte[2048];  

  143. while (true)  

  144. {  

  145. ssize = s.Read(data, 0, data.Length);  

  146. if (size > 0)  

  147. {  

  148. streamWriter.Write(data, 0, size);  

  149. }  

  150. else  

  151. {  

  152. break;  

  153. }  

  154. }  

  155.  

  156. streamwriter.close();  

  157. }  

  158. }  

  159. s.Close();  

  160. }  

  161. }  

  162. }  

“C#使用SharpZipLib的方法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI