C# System.IO.Compression命名空间怎么实现

发布时间:2021-12-02 11:29:09 作者:iii
阅读:170
热销空间服务器,安全稳定,日均0.96元/天 查看>>

这篇文章主要介绍“C# System.IO.Compression命名空间怎么实现”,在日常操作中,相信很多人在C# System.IO.Compression命名空间怎么实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C# System.IO.Compression命名空间怎么实现”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

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. //Createthestreamsandbytearraysneeded  

  9. byte[]buffer=null;  

  10. FileStreamsourceStream=null;  

  11. FileStreamdestinationStream=null;  

  12. GZipStreamcompressedStream=null;  

  13. try  

  14. ...{  

  15. //Readthebytesfromthesourcefileintoabytearray  

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

  17. //Readthesourcestreamvaluesintothebuffer  

  18. buffer=newbyte[sourceStream.Length];  

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

  20. if(checkCounter!=buffer.Length)  

  21. ...{  

  22. thrownewApplicationException();  

  23. }  

  24. //OpentheFileStreamtowriteto  

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

  26. //Createacompressionstreampointingtothedestiantionstream  

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

  28. //Nowwritethecompresseddatatothedestinationfile  

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

  30. }  

  31. catch(ApplicationExceptionex)  

  32. ...{  

  33. MessageBox.Show(ex.Message,"压缩文件时发生错误:",MessageBoxButtons.OK,MessageBoxIcon.Error);  

  34. }  

  35. finally  

  36. ...{  

  37. //Makesureweallwayscloseallstreams  

  38. if(sourceStream!=null)  

  39. sourceStream.Close();  

  40. if(compressedStream!=null)  

  41. compressedStream.Close();  

  42. if(destinationStream!=null)  

  43. destinationStream.Close();  

  44. }  

  45. }  

  46. publicvoidDecompressFile(stringsourceFile,stringdestinationFile)  

  47. ...{  

  48. //makesurethesourcefileisthere  

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

  50. thrownewFileNotFoundException();  

  51. //Createthestreamsandbytearraysneeded  

  52. FileStreamsourceStream=null;  

  53. FileStreamdestinationStream=null;  

  54. GZipStreamdecompressedStream=null;  

  55. byte[]quartetBuffer=null;  

  56. try  

  57. ...{  

  58. //Readinthecompressedsourcestream  

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

  60. //Createacompressionstreampointingtothedestiantionstream  

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

  62. //Readthefootertodeterminethelengthofthedestiantionfile  

  63. quartetBuffer=newbyte[4];  

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

  65. sourceStream.Position=position;  

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

  67. sourceStream.Position=0;  

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

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

  70. intoffset=0;  

  71. inttotal=0;  

  72. //Readthecompresseddataintothebuffer  

  73. while(true)  

  74. ...{  

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

  76. if(bytesRead==0)  

  77. break;  

  78. offset+=bytesRead;  

  79. total+=bytesRead;  

  80. }  

  81. //Nowwriteeverythingtothedestinationfile  

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

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

  84. //andflusheveryhtingtocleanoutthebuffer  

  85. destinationStream.Flush();  

  86. }  

  87. catch(ApplicationExceptionex)  

  88. ...{  

  89. MessageBox.Show(ex.Message,"解压文件时发生错误:",
    MessageBoxButtons.OK,MessageBoxIcon.Error);  

  90. }  

  91. finally  

  92. ...{  

  93. //Makesureweallwayscloseallstreams  

  94. if(sourceStream!=null)  

  95. sourceStream.Close();  

  96. if(decompressedStream!=null)  

  97. decompressedStream.Close();  

  98. if(destinationStream!=null)  

  99. destinationStream.Close();  

  100. }  

  101. }  

到此,关于“C# System.IO.Compression命名空间怎么实现”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:
  1. C#命名空间
  2. C#使用GZipStream实现文件的压缩与解压

开发者交流群:

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

上一篇:如何浅析MySQL数据库磁盘I/O调整优化

下一篇:tk.Mybatis插入数据获取Id怎么实现

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》
开发者交流群×