.net framework 2.0 中新增的两个压缩类及SharpZipLib类
来源:互联网 发布:哪里能找到辐射站数据 编辑:程序博客网 时间:2024/05/21 15:50
.net framework 2.0 中新增的两个压缩类
system.io.compression 命名空间
注意:此命名空间在 .net framework 2.0 版中是新增的。
system.io.compression 命名空间包含提供基本的流压缩和解压缩服务的类。
(downmoon原作)
类 说明
deflatestream 提供用于使用 deflate 算法压缩和解压缩流的方法和属性。
gzipstream 提供用于压缩和解压缩流的方法和属性。
枚举 说明
compressionmode 指定是否压缩或解压缩基础流。
下面以 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
备注
此类表示 gzip 数据格式,它使用无损压缩和解压缩文件的行业标准算法。这种格式包括一个检测数据损坏的循环冗余校验值。gzip 数据格式使用的算法与 deflatestream 类的算法相同,但它可以扩展以使用其他压缩格式。这种格式可以通过不涉及专利使用权的方式轻松实现。gzip 的格式可以从 rfc 1952“gzip file format specification 4.3(gzip 文件格式规范 4.3)gzip file format specification 4.3(gzip 文件格式规范 4.3)”中获得。此类不能用于压缩大于 4 gb 的文件。
给继承者的说明 当从 gzipstream 继承时,必须重写下列成员:canseek、canwrite 和 canread。
下面提供 一个完整的压缩与解压类(downmoon原作 ):
文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!
system.io.compression 命名空间
注意:此命名空间在 .net framework 2.0 版中是新增的。
system.io.compression 命名空间包含提供基本的流压缩和解压缩服务的类。
(downmoon原作)
类 说明
deflatestream 提供用于使用 deflate 算法压缩和解压缩流的方法和属性。
gzipstream 提供用于压缩和解压缩流的方法和属性。
枚举 说明
compressionmode 指定是否压缩或解压缩基础流。
下面以 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
备注
此类表示 gzip 数据格式,它使用无损压缩和解压缩文件的行业标准算法。这种格式包括一个检测数据损坏的循环冗余校验值。gzip 数据格式使用的算法与 deflatestream 类的算法相同,但它可以扩展以使用其他压缩格式。这种格式可以通过不涉及专利使用权的方式轻松实现。gzip 的格式可以从 rfc 1952“gzip file format specification 4.3(gzip 文件格式规范 4.3)gzip file format specification 4.3(gzip 文件格式规范 4.3)”中获得。此类不能用于压缩大于 4 gb 的文件。
给继承者的说明 当从 gzipstream 继承时,必须重写下列成员:canseek、canwrite 和 canread。
下面提供 一个完整的压缩与解压类(downmoon原作 ):
class clszip
...{
public void compressfile ( string sourcefile, string destinationfile )
...{
// make sure the source file is there
if ( file.exists ( sourcefile ) == false )
throw new filenotfoundexception ( );
// create the streams and byte arrays needed
byte[] buffer = null;
filestream sourcestream = null;
filestream destinationstream = null;
gzipstream compressedstream = null;
try
...{
// read the bytes from the source file into a byte array
sourcestream = new filestream ( sourcefile, filemode.open, fileaccess.read, fileshare.read );
// read the source stream values into the buffer
buffer = new byte[sourcestream.length];
int checkcounter = sourcestream.read ( buffer, 0, buffer.length );
if ( checkcounter != buffer.length )
...{
throw new applicationexception ( );
}
// open the filestream to write to
destinationstream = new filestream ( destinationfile, filemode.openorcreate, fileaccess.write );
// create a compression stream pointing to the destiantion stream
compressedstream = new gzipstream ( destinationstream, compressionmode.compress, true );
// now write the compressed data to the destination file
compressedstream.write ( buffer, 0, buffer.length );
}
catch ( applicationexception ex )
...{
messagebox.show ( ex.message, "压缩文件时发生错误:", messageboxbuttons.ok, messageboxicon.error );
}
finally
...{
// make sure we allways close all streams
if ( sourcestream != null )
sourcestream.close ( );
if ( compressedstream != null )
compressedstream.close ( );
if ( destinationstream != null )
destinationstream.close ( );
}
}
public void decompressfile ( string sourcefile, string destinationfile )
...{
// make sure the source file is there
if ( file.exists ( sourcefile ) == false )
throw new filenotfoundexception ( );
// create the streams and byte arrays needed
filestream sourcestream = null;
filestream destinationstream = null;
gzipstream decompressedstream = null;
byte[] quartetbuffer = null;
try
...{
// read in the compressed source stream
sourcestream = new filestream ( sourcefile, filemode.open );
// create a compression stream pointing to the destiantion stream
decompressedstream = new gzipstream ( sourcestream, compressionmode.decompress, true );
// read the footer to determine the length of the destiantion file
quartetbuffer = new byte[4];
int position = (int)sourcestream.length - 4;
sourcestream.position = position;
sourcestream.read ( quartetbuffer, 0, 4 );
sourcestream.position = 0;
int checklength = bitconverter.toint32 ( quartetbuffer, 0 );
byte[] buffer = new byte[checklength + 100];
int offset = 0;
int total = 0;
// read the compressed data into the buffer
while ( true )
...{
int bytesread = decompressedstream.read ( buffer, offset, 100 );
if ( bytesread == 0 )
break;
offset += bytesread;
total += bytesread;
}
// now write everything to the destination file
destinationstream = new filestream ( destinationfile, filemode.create );
destinationstream.write ( buffer, 0, total );
// and flush everyhting to clean out the buffer
destinationstream.flush ( );
}
catch ( applicationexception ex )
...{
messagebox.show(ex.message, "解压文件时发生错误:", messageboxbuttons.ok, messageboxicon.error);
}
finally
...{
// make sure we allways close all streams
if ( sourcestream != null )
sourcestream.close ( );
if ( decompressedstream != null )
decompressedstream.close ( );
if ( destinationstream != null )
destinationstream.close ( );
}
}
}
...{
public void compressfile ( string sourcefile, string destinationfile )
...{
// make sure the source file is there
if ( file.exists ( sourcefile ) == false )
throw new filenotfoundexception ( );
// create the streams and byte arrays needed
byte[] buffer = null;
filestream sourcestream = null;
filestream destinationstream = null;
gzipstream compressedstream = null;
try
...{
// read the bytes from the source file into a byte array
sourcestream = new filestream ( sourcefile, filemode.open, fileaccess.read, fileshare.read );
// read the source stream values into the buffer
buffer = new byte[sourcestream.length];
int checkcounter = sourcestream.read ( buffer, 0, buffer.length );
if ( checkcounter != buffer.length )
...{
throw new applicationexception ( );
}
// open the filestream to write to
destinationstream = new filestream ( destinationfile, filemode.openorcreate, fileaccess.write );
// create a compression stream pointing to the destiantion stream
compressedstream = new gzipstream ( destinationstream, compressionmode.compress, true );
// now write the compressed data to the destination file
compressedstream.write ( buffer, 0, buffer.length );
}
catch ( applicationexception ex )
...{
messagebox.show ( ex.message, "压缩文件时发生错误:", messageboxbuttons.ok, messageboxicon.error );
}
finally
...{
// make sure we allways close all streams
if ( sourcestream != null )
sourcestream.close ( );
if ( compressedstream != null )
compressedstream.close ( );
if ( destinationstream != null )
destinationstream.close ( );
}
}
public void decompressfile ( string sourcefile, string destinationfile )
...{
// make sure the source file is there
if ( file.exists ( sourcefile ) == false )
throw new filenotfoundexception ( );
// create the streams and byte arrays needed
filestream sourcestream = null;
filestream destinationstream = null;
gzipstream decompressedstream = null;
byte[] quartetbuffer = null;
try
...{
// read in the compressed source stream
sourcestream = new filestream ( sourcefile, filemode.open );
// create a compression stream pointing to the destiantion stream
decompressedstream = new gzipstream ( sourcestream, compressionmode.decompress, true );
// read the footer to determine the length of the destiantion file
quartetbuffer = new byte[4];
int position = (int)sourcestream.length - 4;
sourcestream.position = position;
sourcestream.read ( quartetbuffer, 0, 4 );
sourcestream.position = 0;
int checklength = bitconverter.toint32 ( quartetbuffer, 0 );
byte[] buffer = new byte[checklength + 100];
int offset = 0;
int total = 0;
// read the compressed data into the buffer
while ( true )
...{
int bytesread = decompressedstream.read ( buffer, offset, 100 );
if ( bytesread == 0 )
break;
offset += bytesread;
total += bytesread;
}
// now write everything to the destination file
destinationstream = new filestream ( destinationfile, filemode.create );
destinationstream.write ( buffer, 0, total );
// and flush everyhting to clean out the buffer
destinationstream.flush ( );
}
catch ( applicationexception ex )
...{
messagebox.show(ex.message, "解压文件时发生错误:", messageboxbuttons.ok, messageboxicon.error);
}
finally
...{
// make sure we allways close all streams
if ( sourcestream != null )
sourcestream.close ( );
if ( decompressedstream != null )
decompressedstream.close ( );
if ( destinationstream != null )
destinationstream.close ( );
}
}
}
文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!
/**//// <summary>
/// FileZipLib 压缩,解压缩的类
/// </summary>
public class FileZipLib
...{
public FileZipLib() ...{}
/**//// <summary>
/// 创建一个压缩文件
/// </summary>
/// <param name="zipFilename">压缩后的文件名</param>
/// <param name="sourceDirectory">待压缩文件的所在目录</param>
public static void PackFiles(string zipFilename,string sourceDirectory)
...{
FastZip fz = new FastZip() ;
fz.CreateEmptyDirectories = true ;
fz.CreateZip(zipFilename,sourceDirectory,true,"") ;
fz = null ;
}
/**//// <summary>
/// 解压缩文件
/// </summary>
/// <param name="zipFile">待解压缩的文件</param>
/// <param name="directory">解压缩后文件存放的目录</param>
public static bool UnpackFiles(string zipFile,string directory)
...{
if( !Directory.Exists(directory) )
Directory.CreateDirectory(directory) ;
ZipInputStream zis = new ZipInputStream( File.OpenRead(zipFile) ) ;
ZipEntry theEntry = null ;
while( (theEntry = zis.GetNextEntry()) != null )
...{
string directoryName = Path.GetDirectoryName(theEntry.Name) ;
string fileName = Path.GetFileName(theEntry.Name) ;
if( directoryName != string.Empty )
Directory.CreateDirectory(directory + directoryName) ;
if( fileName != string.Empty )
...{
FileStream streamWriter = File.Create( Path.Combine( directory,theEntry.Name) ) ;
int size = 2048 ;
byte[] data = new byte[size] ;
while ( true )
...{
size = zis.Read(data,0,data.Length) ;
if( size > 0 )
streamWriter.Write( data,0,size ) ;
else
break ;
}
streamWriter.Close() ;
}
}
zis.Close() ;
return true ;
}
}
//最后别忘了using ICSharpCode.SharpZipLib.Zip ;
/// FileZipLib 压缩,解压缩的类
/// </summary>
public class FileZipLib
...{
public FileZipLib() ...{}
/**//// <summary>
/// 创建一个压缩文件
/// </summary>
/// <param name="zipFilename">压缩后的文件名</param>
/// <param name="sourceDirectory">待压缩文件的所在目录</param>
public static void PackFiles(string zipFilename,string sourceDirectory)
...{
FastZip fz = new FastZip() ;
fz.CreateEmptyDirectories = true ;
fz.CreateZip(zipFilename,sourceDirectory,true,"") ;
fz = null ;
}
/**//// <summary>
/// 解压缩文件
/// </summary>
/// <param name="zipFile">待解压缩的文件</param>
/// <param name="directory">解压缩后文件存放的目录</param>
public static bool UnpackFiles(string zipFile,string directory)
...{
if( !Directory.Exists(directory) )
Directory.CreateDirectory(directory) ;
ZipInputStream zis = new ZipInputStream( File.OpenRead(zipFile) ) ;
ZipEntry theEntry = null ;
while( (theEntry = zis.GetNextEntry()) != null )
...{
string directoryName = Path.GetDirectoryName(theEntry.Name) ;
string fileName = Path.GetFileName(theEntry.Name) ;
if( directoryName != string.Empty )
Directory.CreateDirectory(directory + directoryName) ;
if( fileName != string.Empty )
...{
FileStream streamWriter = File.Create( Path.Combine( directory,theEntry.Name) ) ;
int size = 2048 ;
byte[] data = new byte[size] ;
while ( true )
...{
size = zis.Read(data,0,data.Length) ;
if( size > 0 )
streamWriter.Write( data,0,size ) ;
else
break ;
}
streamWriter.Close() ;
}
}
zis.Close() ;
return true ;
}
}
//最后别忘了using ICSharpCode.SharpZipLib.Zip ;
- .net framework 2.0 中新增的两个压缩类及SharpZipLib类
- .NET Framework 2.0 中新增的两个压缩类
- .NET Framework 2.0 中新增的两个压缩类
- .NET Framework 2.0 中新增的两个压缩类
- .NET Framework 2.0中新增的两个压缩类
- .NET Framework 2.0 中新增的两个压缩类
- .NET Framework 2.0 中新增的两个压缩类
- c#2.0中新增的两个压缩类
- c#2.0中新增的两个压缩类
- c#2.0中新增的两个压缩类
- c#2.0中新增的两个压缩类(downmoon原创)c#
- c#2.0中新增的两个压缩类(downmoon原创)c#
- .NET Framework 2.0 类型库中新增的常用功能
- .net 压缩文件 SharpZipLib压缩
- SharpZipLib Zip操作类库 压缩解压
- SharpZipLib压缩文件夹及文件
- SharpZipLib压缩文件夹及文件
- .NET Framework 4.0 中数组的新增功能
- 。NET技术高级交流群【14725941】现已180余人
- sendmsg()和recvmsg()应用的简单例子。
- (转)史上第一强:Discuz!源代码分析系列(1)--common.inc.php
- 野指针
- 在sendmsg()与recvmsg()间传递FD值。
- .net framework 2.0 中新增的两个压缩类及SharpZipLib类
- JSP和SERVLET学习笔记
- 关于webpart的一个重大问题,需要专家解答!!!
- Session详解
- Freetextbox使用方法介绍
- c语言运算符优先级
- 控件FreeTextBox介绍
- 别再让注射遗传下去---杜绝对象查询注射
- Sqlite中文排序研究