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

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

None.gif using  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        
/**//// 
InBlock.gif        
/// 对目标文件夹进行压缩,将压缩结果保存为指定文件
InBlock.gif        
/// 

InBlock.gif        
/// 目标文件夹
ExpandedSubBlockEnd.gif        
/// 压缩文件

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        
/**//// 
InBlock.gif        
/// 对目标压缩文件解压缩,将内容解压缩到指定文件夹
InBlock.gif        
/// 

InBlock.gif        
/// 压缩文件
ExpandedSubBlockEnd.gif        
/// 解压缩目录

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/aiyagaze/archive/2006/11/30/576995.html

你可能感兴趣的:(使用GZipStream类压缩和解压文件夹)