递归实现压缩文件夹和子文件夹。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;
using System.Collections;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary; namespace CommonBaseInfo
{
public class Zip
{ #region 压缩解压单一文件
/// <summary>
/// 压缩单一文件
/// </summary>
/// <param name="sourceFile">源文件路径</param>
/// <param name="destinationFile">目标文件路径</param>
public static void CompressFile(string sourceFile, string destinationFile)
{
if (!File.Exists(sourceFile)) throw new FileNotFoundException();
using (FileStream sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
byte[] buffer = new byte[sourceStream.Length];
int checkCounter = sourceStream.Read(buffer, , buffer.Length);
if (checkCounter != buffer.Length) throw new ApplicationException();
using (FileStream destinationStream = new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write))
{
using (GZipStream compressedStream = new GZipStream(destinationStream, CompressionMode.Compress, true))
{
compressedStream.Write(buffer, , buffer.Length);
}
}
}
}
public static void CompressFile(byte[] source, string destinationFile)
{
using (FileStream destinationStream = new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write))
{
using (GZipStream compressedStream = new GZipStream(destinationStream, CompressionMode.Compress, true))
{
compressedStream.Write(source, , source.Length);
}
}
} /// <summary>
/// 解压缩文件
/// </summary>
/// <param name="sourceFile">源文件路径</param>
/// <param name="destinationFile">目标文件路径</param>
public static void DecompressFile(string sourceFile, string destinationFile)
{
if (!File.Exists(sourceFile)) throw new FileNotFoundException();
using (FileStream sourceStream = new FileStream(sourceFile, FileMode.Open))
{
byte[] quartetBuffer = new byte[];
int position = (int)sourceStream.Length - ;
sourceStream.Position = position;
sourceStream.Read(quartetBuffer, , );
sourceStream.Position = ;
int checkLength = BitConverter.ToInt32(quartetBuffer, );
byte[] buffer = new byte[checkLength + ];
using (GZipStream decompressedStream = new GZipStream(sourceStream, CompressionMode.Decompress, true))
{
int total = ;
for (int offset = ; ; )
{
int bytesRead = decompressedStream.Read(buffer, offset, );
if (bytesRead == ) break;
offset += bytesRead;
total += bytesRead;
}
using (FileStream destinationStream = new FileStream(destinationFile, FileMode.Create))
{
destinationStream.Write(buffer, , total);
destinationStream.Flush();
}
}
}
} #endregion #region 压缩解压文件夹
/**/
/// <summary>
/// 对目标文件夹进行压缩,将压缩结果保存为指定文件
/// </summary>
/// <param name="dirPath">目标文件夹</param>
/// <param name="fileName">压缩文件</param>
/// public static void CompressFolderP(string dirPath, string fileName)
{
ArrayList list = new ArrayList();
CompressFolder(dirPath, fileName, dirPath,list); }
public static void CompressFolder(string dirPath, string fileName, string rootdir, ArrayList list)
{ foreach (string d in Directory.GetDirectories(dirPath))
{
byte[] destBuffer = null;
string path = d.Replace(rootdir, "");
SerializeFileInfo sfi = new SerializeFileInfo(d, destBuffer, "",path);
list.Add(sfi);
CompressFolder(d, fileName,rootdir,list); } foreach (string f in Directory.GetFiles(dirPath))
{ if ( (File.GetAttributes(f) & FileAttributes.Hidden) != FileAttributes.Hidden)
{
byte[] destBuffer = File.ReadAllBytes(f);
string path = f.Replace(rootdir, "");
SerializeFileInfo sfi = new SerializeFileInfo(f, destBuffer, "", path);
list.Add(sfi);
} } IFormatter formatter = new BinaryFormatter();
using (Stream s = new MemoryStream())
{
formatter.Serialize(s, list);
s.Position = ;
CreateCompressFile(s, fileName);
}
}
/**/
/// <summary>
/// 对目标压缩文件解压缩,将内容解压缩到指定文件夹
/// </summary>
/// <param name="fileName">压缩文件</param>
/// <param name="dirPath">解压缩目录</param>
/// public static void DeCompressFolder(string fileName, string dirPath)
{ if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
} using (Stream source = File.OpenRead(fileName))
{
using (Stream destination = new MemoryStream())
{
using (GZipStream input = new GZipStream(source, CompressionMode.Decompress, true))
{
byte[] bytes = new byte[];
int n;
while ((n = input.Read(bytes, , bytes.Length)) != )
{
destination.Write(bytes, , n);
}
}
destination.Flush();
destination.Position = ;
DeSerializeFiles(destination, dirPath);
}
}
}
private static void DeSerializeFiles(Stream s, string dirPath)
{
BinaryFormatter b = new BinaryFormatter();
ArrayList list = (ArrayList)b.Deserialize(s);
foreach (SerializeFileInfo f in list)
{
if (f.Fileflag == "")
{
string newName = dirPath + f.Filedirnane;
using (FileStream fs = new FileStream(newName, FileMode.Create, FileAccess.Write))
{
fs.Write(f.FileBuffer, , f.FileBuffer.Length);
fs.Close();
}
}
else
{
if (!Directory.Exists(dirPath+f.Filedirnane))
{
Directory.CreateDirectory(dirPath + f.Filedirnane);
} }
}
}
private static void CreateCompressFile(Stream source, string destinationName)
{
using (Stream destination = new FileStream(destinationName, FileMode.Create, FileAccess.Write))
{
using (GZipStream output = new GZipStream(destination, CompressionMode.Compress))
{
byte[] bytes = new byte[];
int n;
while ((n = source.Read(bytes, , bytes.Length)) != )
{
output.Write(bytes, , n);
}
}
}
} #endregion [Serializable]
class SerializeFileInfo
{
public SerializeFileInfo(string name, byte[] buffer,string flag,string dirname)
{
fileName = name;
fileBuffer = buffer;
fileflag = flag;
filedirnane = dirname;
}
string fileflag;
string filedirnane;
string fileName; public string Filedirnane
{
get
{
return filedirnane;
}
}
public string Fileflag
{
get
{
return fileflag;
}
} public string FileName
{
get
{
return fileName;
}
}
byte[] fileBuffer;
public byte[] FileBuffer
{
get
{
return fileBuffer;
}
}
}
}
}
CommonBaseInfo.Zip.CompressFolderP(@"c:\debug", @"c:\debug.zip");
CommonBaseInfo.Zip.DeCompressFolder(@"c:\debug.zip", @"c:\debug\");