.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原作 ):
class
clsZip
...
{
public
void
CompressFile(
string
sourceFile,
string
destinationFile)
...
{
//
makesurethesourcefileisthere
if
(File.Exists(sourceFile)
==
false
)
throw
new
FileNotFoundException();
//
Createthestreamsandbytearraysneeded
byte
[]buffer
=
null
;
FileStreamsourceStream
=
null
;
FileStreamdestinationStream
=
null
;
GZipStreamcompressedStream
=
null
;
try
...
{
//
Readthebytesfromthesourcefileintoabytearray
sourceStream
=
new
FileStream(sourceFile,FileMode.Open,FileAccess.Read,FileShare.Read);
//
Readthesourcestreamvaluesintothebuffer
buffer
=
new
byte
[sourceStream.Length];
int
checkCounter
=
sourceStream.Read(buffer,
0
,buffer.Length);
if
(checkCounter
!=
buffer.Length)
...
{
throw
new
ApplicationException();
}
//
OpentheFileStreamtowriteto
destinationStream
=
new
FileStream(destinationFile,FileMode.OpenOrCreate,FileAccess.Write);
//
Createacompressionstreampointingtothedestiantionstream
compressedStream
=
new
GZipStream(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();
}
}
public
void
DecompressFile(
string
sourceFile,
string
destinationFile)
...
{
//
makesurethesourcefileisthere
if
(File.Exists(sourceFile)
==
false
)
throw
new
FileNotFoundException();
//
Createthestreamsandbytearraysneeded
FileStreamsourceStream
=
null
;
FileStreamdestinationStream
=
null
;
GZipStreamdecompressedStream
=
null
;
byte
[]quartetBuffer
=
null
;
try
...
{
//
Readinthecompressedsourcestream
sourceStream
=
new
FileStream(sourceFile,FileMode.Open);
//
Createacompressionstreampointingtothedestiantionstream
decompressedStream
=
new
GZipStream(sourceStream,CompressionMode.Decompress,
true
);
//
Readthefootertodeterminethelengthofthedestiantionfile
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
;
//
Readthecompresseddataintothebuffer
while
(
true
)
...
{
int
bytesRead
=
decompressedStream.Read(buffer,offset,
100
);
if
(bytesRead
==
0
)
break
;
offset
+=
bytesRead;
total
+=
bytesRead;
}
//
Nowwriteeverythingtothedestinationfile
destinationStream
=
new
FileStream(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();
}
}
}