这篇文章主要介绍“C# System.io.Compression命名空间怎么实现”,在日常操作中,相信很多人在C# System.IO.Compression命名空间怎么实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希
这篇文章主要介绍“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
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命名空间怎么实现”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!
--结束END--
本文标题: C# System.IO.Compression命名空间怎么实现
本文链接: https://lsjlt.com/news/294390.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0