C# System.IO.Compression命名空间,注意:此命名空间在 .NET Framework 2.0 版中是新增的。C# System.IO.Compression命名空间包含提供基本的流压缩和解压缩服务的类。GZipStream提供用于压缩和解压缩流的方法和属性。下面以 GZipStream 为例说明

创新互联是一家专业提供乌拉特中企业网站建设,专注与成都网站设计、成都做网站、H5场景定制、小程序制作等业务。10年已为乌拉特中众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。
注意:此类在 .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
- classclsZip
 - ...{
 - publicvoidCompressFile(stringsourceFile,stringdestinationFile)
 - ...{
 - //makesurethesourcefileisthere
 - if(File.Exists(sourceFile)==false)
 - thrownewFileNotFoundException();
 - //Createthestreamsandbytearraysneeded
 - byte[]buffer=null;
 - FileStreamsourceStream=null;
 - FileStreamdestinationStream=null;
 - GZipStreamcompressedStream=null;
 - try
 - ...{
 - //Readthebytesfromthesourcefileintoabytearray
 - sourceStream=newFileStream(sourceFile,FileMode.Open,FileAccess.Read,FileShare.Read);
 - //Readthesourcestreamvaluesintothebuffer
 - buffer=newbyte[sourceStream.Length];
 - intcheckCounter=sourceStream.Read(buffer,0,buffer.Length);
 - if(checkCounter!=buffer.Length)
 - ...{
 - thrownewApplicationException();
 - }
 - //OpentheFileStreamtowriteto
 - destinationStream=newFileStream(destinationFile,FileMode.OpenOrCreate,FileAccess.Write);
 - //Createacompressionstreampointingtothedestiantionstream
 - compressedStream=newGZipStream(destinationStream,CompressionMode.Compress,true);
 - //Nowwritethecompresseddatatothedestinationfile
 - compressedStream.Write(buffer,0,buffer.Length);
 - }
 - catch(ApplicationExceptionex)
 - ...{
 - MessageBox.Show(ex.Message,"压缩文件时发生错误:",MessageBoxButtons.OK,MessageBoxIcon.Error);
 - }
 - finally
 - ...{
 - //Makesureweallwayscloseallstreams
 - if(sourceStream!=null)
 - sourceStream.Close();
 - if(compressedStream!=null)
 - compressedStream.Close();
 - if(destinationStream!=null)
 - destinationStream.Close();
 - }
 - }
 - publicvoidDecompressFile(stringsourceFile,stringdestinationFile)
 - ...{
 - //makesurethesourcefileisthere
 - if(File.Exists(sourceFile)==false)
 - thrownewFileNotFoundException();
 - //Createthestreamsandbytearraysneeded
 - FileStreamsourceStream=null;
 - FileStreamdestinationStream=null;
 - GZipStreamdecompressedStream=null;
 - byte[]quartetBuffer=null;
 - try
 - ...{
 - //Readinthecompressedsourcestream
 - sourceStream=newFileStream(sourceFile,FileMode.Open);
 - //Createacompressionstreampointingtothedestiantionstream
 - decompressedStream=newGZipStream(sourceStream,CompressionMode.Decompress,true);
 - //Readthefootertodeterminethelengthofthedestiantionfile
 - quartetBuffer=newbyte[4];
 - intposition=(int)sourceStream.Length-4;
 - sourceStream.Position=position;
 - sourceStream.Read(quartetBuffer,0,4);
 - sourceStream.Position=0;
 - intcheckLength=BitConverter.ToInt32(quartetBuffer,0);
 - byte[]buffer=newbyte[checkLength+100];
 - intoffset=0;
 - inttotal=0;
 - //Readthecompresseddataintothebuffer
 - while(true)
 - ...{
 - intbytesRead=decompressedStream.Read(buffer,offset,100);
 - if(bytesRead==0)
 - break;
 - offset+=bytesRead;
 - total+=bytesRead;
 - }
 - //Nowwriteeverythingtothedestinationfile
 - destinationStream=newFileStream(destinationFile,FileMode.Create);
 - destinationStream.Write(buffer,0,total);
 - //andflusheveryhtingtocleanoutthebuffer
 - destinationStream.Flush();
 - }
 - catch(ApplicationExceptionex)
 - ...{
 - MessageBox.Show(ex.Message,"解压文件时发生错误:",
 
MessageBoxButtons.OK,MessageBoxIcon.Error);- }
 - finally
 - ...{
 - //Makesureweallwayscloseallstreams
 - if(sourceStream!=null)
 - sourceStream.Close();
 - if(decompressedStream!=null)
 - decompressedStream.Close();
 - if(destinationStream!=null)
 - destinationStream.Close();
 - }
 - }
 - }
 
以上介绍C# System.IO.Compression命名空间
                当前标题:C#System.IO.Compression命名空间
                
                网站地址:http://www.csdahua.cn/qtweb/news21/547421.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网