//调用
string sourcePath = @"F:\zazhi\book\2013-09";
//文件夹压缩
zipDir.CompressDirectory(sourcePath, sourcePath + ".zip", (fileName) => { Console.WriteLine("Compressing {0}...", fileName); });
//文件夹解缩
zipDir.DecompressToDirectory(sourcePath + ".zip", sourcePath, (fileName) => { Console.WriteLine("Compressing {0}...", fileName); });
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO.Compression;
using System.IO;
using System.Text;
using System.Diagnostics;
namespace pre.magazine.web
{
public class zipDir
{
public delegate void ProgressDelegate(string sMessage);
#region 压缩文件夹
public static void CompressDirectory(string sInDir, string sOutFile, ProgressDelegate progress)
{
//1.获取要压缩的上层文件夹转为字节数组
string[] sFiles = Directory.GetFiles(sInDir, "*.*", SearchOption.AllDirectories);//搜索当前目录和所有子目录
//2.获取要压缩的文件夹及下的文件长度
int iDirLen = sInDir[sInDir.Length - 1] == Path.DirectorySeparatorChar ? sInDir.Length : sInDir.Length + 1;
//3.用文件流创建压缩后的文件目录及压缩文件名
using (FileStream outFile = new FileStream(sOutFile, FileMode.Create, FileAccess.Write, FileShare.None))
//4.用压缩流压缩刚创建的压缩后的文件目录及压缩文件名
using (GZipStream str = new GZipStream(outFile, CompressionMode.Compress))
//5.用foreach遍历1的字节数组
foreach (string sFilePath in sFiles)
{
//6.截取要压缩的文件夹及下的文件目录
string sRelativePath = sFilePath.Substring(iDirLen);
if (progress != null)
progress(sRelativePath);
//调用压缩文件方法CompressFile(sInDir, sRelativePath, str);//压缩文件
CompressFile(sInDir, sRelativePath, str);//压缩文件
}
}
#endregion
#region 压缩文件
public static void CompressFile(string sDir, string sRelativePath, GZipStream zipStream)
{
//7.将压缩的文件转为char数组
char[] chars = sRelativePath.ToCharArray();
//8.将7的char数组(长度N)压缩
zipStream.Write(BitConverter.GetBytes(chars.Length), 0, sizeof(int));
//9.foreach遍历7的char数组(文件)压缩
foreach (char c in chars)
zipStream.Write(BitConverter.GetBytes(c), 0, sizeof(char));
//10.将文件夹目录和要压缩的文件夹及下的文件连在一起转为字节数组
byte[] bytes = File.ReadAllBytes(Path.Combine(sDir, sRelativePath));
//11.压缩10的文件目录和要压缩的文件数据的(长度N)进行整体压缩
zipStream.Write(BitConverter.GetBytes(bytes.Length), 0, sizeof(int));
//12.压缩10的文件目录和要压缩的文件数据的(文件)进行整体压缩
zipStream.Write(bytes, 0, bytes.Length);
}
#endregion
#region 解压文件
public static bool DecompressFile(string sDir, GZipStream zipStream, ProgressDelegate progress)
{
byte[] bytes = new byte[sizeof(int)];
int Readed = zipStream.Read(bytes, 0, sizeof(int));
if (Readed < sizeof(int))
return false;
int iNameLen = BitConverter.ToInt32(bytes, 0);
bytes = new byte[sizeof(char)];
StringBuilder sb = new StringBuilder();
for (int i = 0; i < iNameLen; i++)
{
zipStream.Read(bytes, 0, sizeof(char));
char c = BitConverter.ToChar(bytes, 0);
sb.Append(c);
}
string sFileName = sb.ToString();
if (progress != null)
progress(sFileName);
bytes = new byte[sizeof(int)];
zipStream.Read(bytes, 0, sizeof(int));
int iFileLen = BitConverter.ToInt32(bytes, 0);
bytes = new byte[iFileLen];
zipStream.Read(bytes, 0, bytes.Length);
string sFilePath = Path.Combine(sDir, sFileName);
string sFinalDir = Path.GetDirectoryName(sFilePath);
if (!Directory.Exists(sFinalDir))
Directory.CreateDirectory(sFinalDir);
using (FileStream outFile = new FileStream(sFilePath, FileMode.Create, FileAccess.Write, FileShare.None))
outFile.Write(bytes, 0, iFileLen);
return true;
}
#endregion
#region 解压文件夹
public static void DecompressToDirectory(string sCompressedFile, string sDir, ProgressDelegate progress)
{
using (FileStream inFile = new FileStream(sCompressedFile, FileMode.Open, FileAccess.Read, FileShare.None))
using (GZipStream zipStream = new GZipStream(inFile, CompressionMode.Decompress, true))
while (DecompressFile(sDir, zipStream, progress)) ;//解压文件
}
#endregion
}
}