人生是一场不能存盘的RPG,我只能尽量多搞几个Screenshot

March 7, 2007

.net2.0 自带的压缩/解压类GZipStream Class

Filed under: .NET, Code snippets

http://www.codeguru.com/csharp/.net/net_data/sortinganditerating/article.php/c13375/
http://msdn2.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx

支持gzip格式(见RFC 1952),生成的压缩文件后缀为.gz,不能压缩大于4GB的文件
和XP内置的zip格式不兼容

//–压缩
FileStream fs = new FileStream(”es_resume.doc”, FileMode.Open);
byte[] input = new byte[fs.Length];
fs.Read(input, 0, input.Length);
fs.Close();

FileStream fsOutput = new FileStream(”es_resume.gzip”,
FileMode.Create,
FileAccess.Write);
GZipStream zip = new GZipStream(fsOutput, CompressionMode.Compress);

zip.Write(input, 0, input.Length);
zip.Close();
fsOutput.Close();

//–解压
FileStream fs = new FileStream(”es_resume.gzip”, FileMode.Open);
FileStream fsOutput = new FileStream(”es_resume2.doc”,
FileMode.Create,
FileAccess.Write);
GZipStream zip = new GZipStream(fs, CompressionMode.Decompress, true);

byte[] buffer = new byte[4096];
int bytesRead;
bool continueLoop = true;
while (continueLoop)
{
bytesRead = zip.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
break;
fsOutput.Write(buffer, 0, bytesRead);
}
zip.Close();
fsOutput.Close();
fs.Close();

Comments »

The URI to TrackBack this entry is: http://recordsome.blogsome.com/2007/03/07/p228/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.






















Get free blog up and running in minutes with Blogsome
Theme designed by Hadley Wickham