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
- 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,"壓縮文件時發(fā)生錯誤:",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,"解壓文件時發(fā)生錯誤:",
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命名空間
【編輯推薦】