使用GZipStream类压缩和解压文件夹

GZipStream压缩与解压
本文介绍了一个基于.NET Framework 2.0中GZipStream类实现的简单压缩与解压方法,通过序列化文件内容及文件名信息,并利用GZipStream进行压缩,最后保存为压缩包文件。
因为要求的压缩和解压非常简单,只有一级目录,而且文件很小,就没有使用SharpZipLib而是自己用.Net 2.0中的GZipStream类写了个简单的。将保存每个文件内容的byte数组和文件名的一个类型的示例放入arraylist里,再对其序列化,压缩序列化的流并保存为压缩包。其实对于多级目录在压缩时对其文件进行递归并在解压时根据文件名称和路径重新构建文件目录就也可以实现了。

None.gifusing System;
None.gif
using System.Text;
None.gif
using System.Runtime.Serialization;
None.gif
using System.Runtime.Serialization.Formatters.Binary;
None.gif
using System.Collections;
None.gif
using System.IO;
None.gif
using System.IO.Compression;
None.gif
None.gif
namespace GreatCHN.GZipCompression
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public class GZipCompress
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 对目标文件夹进行压缩,将压缩结果保存为指定文件
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="dirPath">目标文件夹</param>
ExpandedSubBlockEnd.gif        
/// <param name="fileName">压缩文件</param>

InBlock.gif        public static void Compress(string dirPath, string fileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ArrayList list 
= new ArrayList();
InBlock.gif            
foreach (string f in Directory.GetFiles(dirPath))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
byte[] destBuffer = File.ReadAllBytes(f);
InBlock.gif                SerializeFileInfo sfi 
= new SerializeFileInfo(f, destBuffer);
InBlock.gif                list.Add(sfi);
ExpandedSubBlockEnd.gif            }

InBlock.gif            IFormatter formatter 
= new BinaryFormatter();
InBlock.gif            
using (Stream s = new MemoryStream())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                formatter.Serialize(s, list);
InBlock.gif                s.Position 
= 0;
InBlock.gif                CreateCompressFile(s, fileName);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 对目标压缩文件解压缩,将内容解压缩到指定文件夹
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="fileName">压缩文件</param>
ExpandedSubBlockEnd.gif        
/// <param name="dirPath">解压缩目录</param>

InBlock.gif        public static void DeCompress(string fileName, string dirPath)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
using (Stream source = File.OpenRead(fileName))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
using (Stream destination = new MemoryStream())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
using (GZipStream input = new GZipStream(source, CompressionMode.Decompress, true))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
byte[] bytes = new byte[4096];
InBlock.gif                        
int n;
InBlock.gif                        
while ((n = input.Read(bytes, 0, bytes.Length)) != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            destination.Write(bytes, 
0, n);
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                    destination.Flush();
InBlock.gif                    destination.Position 
= 0;
InBlock.gif                    DeSerializeFiles(destination, dirPath);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private static void DeSerializeFiles(Stream s, string dirPath)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            BinaryFormatter b 
= new BinaryFormatter();
InBlock.gif            ArrayList list 
= (ArrayList)b.Deserialize(s);
InBlock.gif
InBlock.gif            
foreach (SerializeFileInfo f in list)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string newName = dirPath + Path.GetFileName(f.FileName);
InBlock.gif                
using (FileStream fs = new FileStream(newName, FileMode.Create, FileAccess.Write))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    fs.Write(f.FileBuffer, 
0, f.FileBuffer.Length);
InBlock.gif                    fs.Close();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private static void CreateCompressFile(Stream source, string destinationName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
using (Stream destination = new FileStream(destinationName, FileMode.Create, FileAccess.Write))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
using (GZipStream output = new GZipStream(destination, CompressionMode.Compress))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
byte[] bytes = new byte[4096];
InBlock.gif                    
int n;
InBlock.gif                    
while ((n = source.Read(bytes, 0, bytes.Length)) != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        output.Write(bytes, 
0, n);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Serializable]
InBlock.gif        
class SerializeFileInfo
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
public SerializeFileInfo(string name, byte[] buffer)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                fileName 
= name;
InBlock.gif                fileBuffer 
= buffer;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
string fileName;
InBlock.gif            
public string FileName
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
get
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return fileName;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
byte[] fileBuffer;
InBlock.gif            
public byte[] FileBuffer
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
get
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return fileBuffer;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

      在System.IO.Compress命名空间下还有一个DeflateStream类,它使用Deflate 算法来进行压缩和解压,它和GZipStream的工作方式是一样的。

转载于:https://www.cnblogs.com/lzjsky/archive/2011/04/02/2003350.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值