#region GQY 生成多个文件夹压缩包
string zipFilePath =string.Empty;
if (dirPath.EndsWith("\\"))
{
zipFilePath =dirPath.Substring(0, dirPath.Length - 1);
}
zipFilePath = zipFilePath + ".zip";
using (ZipOutputStreams =newZipOutputStream(File.Create(zipFilePath)))
{
for(inti = 0; i < secondFile.Length; i++)
{
ZipFileDictory(dirPath + secondFile[i], s,"");
}
}
#endregion
///<summary>
///递归压缩文件夹方法
///</summary>
///<paramname="FolderToZip">最内层文件夹,直接可以读取文件</param>
///<paramname="s">ZipOutputStream</param>
///<paramname="ParentFolderName">FolderToZip上层如果是根目录,为空。否则就是这个文件夹</param>
privateboolZipFileDictory(string FolderToZip,ZipOutputStream s,stringParentFolderName)
{
boolres = true;
string[]folders, filenames;
ZipEntry entry =null;
FileStream fs =null;
Crc32 crc =newCrc32();
try
{
//创建当前文件夹
entry = new ZipEntry(Path.Combine(ParentFolderName,Path.GetFileName(FolderToZip)+"\\")); //加上 “/”才会当成是文件夹创建
s.PutNextEntry(entry);
s.Flush();
//先压缩文件,再递归压缩文件夹
filenames = Directory.GetFiles(FolderToZip);
foreach (stringfilein filenames)
{
//打开压缩文件
fs = File.OpenRead(file);
byte[] buffer =newbyte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
entry = new ZipEntry(Path.Combine(ParentFolderName,Path.GetFileName(FolderToZip)+"\\" + Path.GetFileName(file)));
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
}
}
catch
{
res = false;
}
finally
{
if (fs != null)
{
fs.Close();
fs = null;
}
if (entry != null)
{
entry = null;
}
GC.Collect();
GC.Collect(1);
}
folders = Directory.GetDirectories(FolderToZip);
foreach(stringfolderin folders)
{
if (!ZipFileDictory(folder, s,Path.Combine(ParentFolderName,Path.GetFileName(FolderToZip))))
{
return false;
}
}
returnres;
}