c# 压缩文件
递归实现压缩文件夹和子文件夹。
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, 0, 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, 0, 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, 0, 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[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]; using (GZipStream decompressedStream = new GZipStream(sourceStream, CompressionMode.Decompress, true)) { int total = 0; for (int offset = 0; ; ) { int bytesRead = decompressedStream.Read(buffer, offset, 100); if (bytesRead == 0) break; offset += bytesRead; total += bytesRead; } using (FileStream destinationStream = new FileStream(destinationFile, FileMode.Create)) { destinationStream.Write(buffer, 0, 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, "0",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, "1", path); list.Add(sfi); } } IFormatter formatter = new BinaryFormatter(); using (Stream s = new MemoryStream()) { formatter.Serialize(s, list); s.Position = 0; 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[4096]; int n; while ((n = input.Read(bytes, 0, bytes.Length)) != 0) { destination.Write(bytes, 0, n); } } destination.Flush(); destination.Position = 0; 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 == "1") { string newName = dirPath + f.Filedirnane; using (FileStream fs = new FileStream(newName, FileMode.Create, FileAccess.Write)) { fs.Write(f.FileBuffer, 0, 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[4096]; int n; while ((n = source.Read(bytes, 0, bytes.Length)) != 0) { output.Write(bytes, 0, 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\");