接上篇:【NCRE】回收考生答题文件--文件夹的重命名和删除
二、将文件夹进行压缩
压缩方法分享:
/// <summary>
/// 压缩文件
/// </summary>
/// <paramname="sourceFilePath"></param>
/// <paramname="destinationZipFilePath"></param>
public static void CreateZip(stringsourceFilePath, string destinationZipFilePath)
{
if(sourceFilePath[sourceFilePath.Length - 1] !=System.IO.Path.DirectorySeparatorChar)
sourceFilePath +=System.IO.Path.DirectorySeparatorChar;
ZipOutputStream zipStream = newZipOutputStream(File.Create(destinationZipFilePath));
zipStream.SetLevel(6); // 压缩级别 0-9
CreateZipFiles(sourceFilePath,zipStream);
zipStream.Finish();
zipStream.Close();
}
/// <summary>
/// 递归压缩文件
/// </summary>
/// <paramname="sourceFilePath">待压缩的文件或文件夹路径</param>
/// <paramname="zipStream">打包结果的zip文件路径(类似D:\WorkSpace\a.zip),全路径包括文件名和.zip扩展名</param>
/// <paramname="staticFile"></param>
private static voidCreateZipFiles(string sourceFilePath, ZipOutputStream zipStream)
{
Crc32 crc = new Crc32();
string[] filesArray =Directory.GetFileSystemEntries(sourceFilePath);
foreach (string file infilesArray)
{
if(Directory.Exists(file)) //如果当前是文件夹,递归
{
CreateZipFiles(file,zipStream);
}
else //如果是文件,开始压缩
{
FileStream fileStream =File.OpenRead(file);
byte[] buffer = newbyte[fileStream.Length];
fileStream.Read(buffer, 0,buffer.Length);
string tempFile =file.Substring(sourceFilePath.LastIndexOf("\\") + 1);
ZipEntry entry = newZipEntry(tempFile);
entry.DateTime =DateTime.Now;
entry.Size =fileStream.Length;
fileStream.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
zipStream.PutNextEntry(entry);
zipStream.Write(buffer, 0,buffer.Length);
}
}
}
此压缩文件的弊端:
用此方法进行压缩后,所有的文件夹都到根目录上来了。例如我将计算机一级考生文件进行压缩。
源文件内容:
压缩后内容:(所有的内容都在,但是所有的文件夹都到一个目录下了。)
由于此文件夹压缩方法还需要再进行优化,所以,在考生登陆的时候没有从MongoDB下载考生文件,要不然考生答题是会受影响的。我们选择的方法是,在打包程序的时候,将考生文件一同打包进去,默认按照路径为C盘的 根目录,当考生点击登录的时候,再将文件夹复制到指定的考生目录下。
文件夹复制方法分享:
#region"复制文件夹"
/// <summary>
/// 复制文件夹
/// </summary>
/// <param name="strFromPath"></param>
/// <param name="strToPath"></param>
/// <returns></returns>
public static Boolean Copy(string strFromPath, string strToPath)
{
Boolean flag = false;
//如果源文件夹不存在,则创建
if (!Directory.Exists(strFromPath))
{
Directory.CreateDirectory(strFromPath);
}
//取得要拷贝的文件夹名
string strFolderName = strFromPath.Substring(strFromPath.LastIndexOf("\\") +
1, strFromPath.Length - strFromPath.LastIndexOf("\\") - 1);
//如果目标文件夹中没有源文件夹则在目标文件夹中创建源文件夹
if (!Directory.Exists(strToPath + "\\" + strFolderName))
{
Directory.CreateDirectory(strToPath + "\\" + strFolderName);
}
//创建数组保存源文件夹下的文件名
string[] strFiles = Directory.GetFiles(strFromPath);
//循环拷贝文件
for (int i = 0; i < strFiles.Length; i++)
{
//取得拷贝的文件名,只取文件名,地址截掉。
string strFileName = strFiles[i].Substring(strFiles[i].LastIndexOf("\\") + 1, strFiles[i].Length - strFiles[i].LastIndexOf("\\") - 1);
//开始拷贝文件,true表示覆盖同名文件
File.Copy(strFiles[i], strToPath + "\\" + strFolderName + "\\" + strFileName, true);
}
//创建DirectoryInfo实例
DirectoryInfo dirInfo = new DirectoryInfo(strFromPath);
//取得源文件夹下的所有子文件夹名称
DirectoryInfo[] ZiPath = dirInfo.GetDirectories();
for (int j = 0; j < ZiPath.Length; j++)
{
//获取所有子文件夹名
string strZiPath = strFromPath + "\\" + ZiPath[j].ToString();
//把得到的子文件夹当成新的源文件夹,从头开始新一轮的拷贝
Copy(strZiPath, strToPath + "\\" + strFolderName);
}
flag = true;
return flag;
}
#endregion
未完待续~~下篇介绍MongoDB的上传和下载