C# System.IO.Compression命名空间

开发 后端
这里介绍C# System.IO.Compression命名空间包含提供基本的流压缩和解压缩服务的类。GZipStream提供用于压缩和解压缩流的方法和属性。

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,"压缩文件时发生错误:",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,"解压文件时发生错误:",
    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. }  

以上介绍C# System.IO.Compression命名空间

【编辑推荐】

  1. 如何用C#和ADO.NET访问
  2. 浅析C# Switch语句
  3. C#验证输入方法详解
  4. 简单介绍C# 匿名方法
  5. C# FileSystemWatcher对象
责任编辑:佚名 来源: IT168
相关推荐

2024-06-20 08:09:24

2009-08-27 15:34:38

C#命名空间

2009-08-28 13:56:25

C#反射命名空间

2009-08-12 10:46:54

C#命名空间

2009-09-11 09:22:14

System.Linq

2009-09-01 17:17:48

C#命名空间和程序集

2009-09-01 16:07:04

C#命名规约

2009-07-31 18:18:33

Camel命名法C#命名规范

2009-08-27 16:30:08

C#编程命名规范

2009-08-28 09:30:48

C#命名属性

2009-09-01 09:12:37

C# System.X

2009-08-21 08:52:40

C#语言命名

2009-09-01 16:12:41

C#命名指南

2009-08-13 13:38:30

C#命名规范

2014-04-28 09:56:56

Objective-CiOS命名空间

2009-05-22 11:01:53

C++JavaC#

2009-09-01 17:29:51

C#命名规约

2009-09-01 15:08:07

C#命名规范

2024-05-06 09:12:13

C#SerialPort监听数据

2009-05-26 16:12:43

Visual StudC#命名
点赞
收藏

51CTO技术栈公众号