溫馨提示×

溫馨提示×

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

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

C# System.IO.Compression命名空間怎么實現(xiàn)

發(fā)布時間:2021-12-02 11:29:09 來源:億速云 閱讀:144 作者:iii 欄目:編程語言

這篇文章主要介紹“C# System.IO.Compression命名空間怎么實現(xiàn)”,在日常操作中,相信很多人在C# System.IO.Compression命名空間怎么實現(xiàn)問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”C# System.IO.Compression命名空間怎么實現(xiàn)”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

C# System.IO.Compression命名空間,注意:此命名空間在 .NET Framework 2.0 版中是新增的。C# System.IO.Compression命名空間包含提供基本的流壓縮和解壓縮服務的類。GZipStream提供用于壓縮和解壓縮流的方法和屬性。下面以 GZipStream  為例說明

注意:此類在 .NET Framework 2.0 版中是新增的。

提供用于壓縮和解壓縮流的方法和屬性。
命名空間:System.IO.Compression
程序集:System(在 system.dll 中)
語法
Visual Basic(聲明)
Public Class GZipStream
Inherits Stream
Visual Basic(用法)
Dim instance As GZipStream

C#
public class GZipStream : Stream

C++
public ref class GZipStream : public Stream

J#
public class GZipStream extends Stream

JScript
public class GZipStream extends Stream

  1. classclsZip  

  2. ...{  

  3. publicvoidCompressFile(stringsourceFile,stringdestinationFile)  

  4. ...{  

  5. //makesurethesourcefileisthere  

  6. if(File.Exists(sourceFile)==false)  

  7. thrownewFileNotFoundException();  

  8.  

  9. //Createthestreamsandbytearraysneeded  

  10. byte[]buffer=null;  

  11. FileStreamsourceStream=null;  

  12. FileStreamdestinationStream=null;  

  13. GZipStreamcompressedStream=null;  

  14.  

  15. try  

  16. ...{  

  17. //Readthebytesfromthesourcefileintoabytearray  

  18. sourceStream=newFileStream(sourceFile,FileMode.Open,FileAccess.Read,FileShare.Read);  

  19.  

  20. //Readthesourcestreamvaluesintothebuffer  

  21. buffer=newbyte[sourceStream.Length];  

  22. intcheckCounter=sourceStream.Read(buffer,0,buffer.Length);  

  23.  

  24. if(checkCounter!=buffer.Length)  

  25. ...{  

  26. thrownewApplicationException();  

  27. }  

  28.  

  29. //OpentheFileStreamtowriteto  

  30. destinationStream=newFileStream(destinationFile,FileMode.OpenOrCreate,FileAccess.Write);  

  31.  

  32. //Createacompressionstreampointingtothedestiantionstream  

  33. compressedStream=newGZipStream(destinationStream,CompressionMode.Compress,true);  

  34.  

  35. //Nowwritethecompresseddatatothedestinationfile  

  36. compressedStream.Write(buffer,0,buffer.Length);  

  37. }  

  38. catch(ApplicationExceptionex)  

  39. ...{  

  40. MessageBox.Show(ex.Message,"壓縮文件時發(fā)生錯誤:",MessageBoxButtons.OK,MessageBoxIcon.Error);  

  41. }  

  42. finally  

  43. ...{  

  44. //Makesureweallwayscloseallstreams  

  45. if(sourceStream!=null)  

  46. sourceStream.Close();  

  47.  

  48. if(compressedStream!=null)  

  49. compressedStream.Close();  

  50.  

  51. if(destinationStream!=null)  

  52. destinationStream.Close();  

  53. }  

  54. }  

  55.  

  56. publicvoidDecompressFile(stringsourceFile,stringdestinationFile)  

  57. ...{  

  58. //makesurethesourcefileisthere  

  59. if(File.Exists(sourceFile)==false)  

  60. thrownewFileNotFoundException();  

  61.  

  62. //Createthestreamsandbytearraysneeded  

  63. FileStreamsourceStream=null;  

  64. FileStreamdestinationStream=null;  

  65. GZipStreamdecompressedStream=null;  

  66. byte[]quartetBuffer=null;  

  67.  

  68. try  

  69. ...{  

  70. //Readinthecompressedsourcestream  

  71. sourceStream=newFileStream(sourceFile,FileMode.Open);  

  72.  

  73. //Createacompressionstreampointingtothedestiantionstream  

  74. decompressedStream=newGZipStream(sourceStream,CompressionMode.Decompress,true);  

  75.  

  76. //Readthefootertodeterminethelengthofthedestiantionfile  

  77. quartetBuffer=newbyte[4];  

  78. intposition=(int)sourceStream.Length-4;  

  79. sourceStream.Position=position;  

  80. sourceStream.Read(quartetBuffer,0,4);  

  81. sourceStream.Position=0;  

  82. intcheckLength=BitConverter.ToInt32(quartetBuffer,0);  

  83.  

  84. byte[]buffer=newbyte[checkLength+100];  

  85.  

  86. intoffset=0;  

  87. inttotal=0;  

  88.  

  89. //Readthecompresseddataintothebuffer  

  90. while(true)  

  91. ...{  

  92. intbytesRead=decompressedStream.Read(buffer,offset,100);  

  93.  

  94. if(bytesRead==0)  

  95. break;  

  96.  

  97. offset+=bytesRead;  

  98. total+=bytesRead;  

  99. }  

  100.  

  101. //Nowwriteeverythingtothedestinationfile  

  102. destinationStream=newFileStream(destinationFile,FileMode.Create);  

  103. destinationStream.Write(buffer,0,total);  

  104.  

  105. //andflusheveryhtingtocleanoutthebuffer  

  106. destinationStream.Flush();  

  107. }  

  108. catch(ApplicationExceptionex)  

  109. ...{  

  110. MessageBox.Show(ex.Message,"解壓文件時發(fā)生錯誤:",
    MessageBoxButtons.OK,MessageBoxIcon.Error);  

  111. }  

  112. finally  

  113. ...{  

  114. //Makesureweallwayscloseallstreams  

  115. if(sourceStream!=null)  

  116. sourceStream.Close();  

  117.  

  118. if(decompressedStream!=null)  

  119. decompressedStream.Close();  

  120.  

  121. if(destinationStream!=null)  

  122. destinationStream.Close();  

  123. }  

  124.  

  125. }  

到此,關(guān)于“C# System.IO.Compression命名空間怎么實現(xiàn)”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

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

AI