因为该项目背景是需要把一些模板和另一台服务器的附件都打包下载,所以我们要先把2边的文件复制到同一个文件夹下,然后在打包下载。步骤如下
1.先判断需要保存的文件夹是否存在
if (!System.IO.Directory.Exists(Server.MapPath("~/ExportFile/")))
System.IO.Directory.CreateDirectory(Server.MapPath("~/ExportFile/"));
2.创建新的存储文件夹
//创建新的存储文件夹
var newFolder = "你自己下载文件夹的名字" + "_" + DateTime.Now.ToString("yyyyMMddHHmmss");
string newFolderPath = Server.MapPath("~/ExportFile/") + newFolder;
System.IO.Directory.CreateDirectory(newFolderPath);
3.复制服务器已有的一些模板文件
string sourceBankFile = Server.MapPath(文件夹路径);
string[] BankFileList = Directory.GetFiles(sourceBankFile);
foreach (string file in BankFileList)
{
System.IO.File.Copy(file, newFolderPath + "/" + System.IO.Path.GetFileName(file), true);
}
4.远程服务器的附件下载到服务器。因为这里是多个项目的附件,所以附件我没有打包放一起,是按项目部来分装的。
List<dynamic> list = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(FileList).ToObject<List<dynamic>>();
foreach (var strList in list)
{
string fileUrl = strList.Url.Value; string projectName = strList.XMMCHTMC;
string fileName = strList.FileName;
//项目部文件夹
var projectFolder = newFolderPath + "/" + projectName;
if (!System.IO.Directory.Exists(projectFolder))
System.IO.Directory.CreateDirectory(projectFolder);
//开始下载附件
HttpWebRequest request = WebRequest.Create(fileUrl) as HttpWebRequest;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream responseStream = response.GetResponseStream();
Stream stream = new FileStream(projectFolder + "/" + fileName, FileMode.Create);
byte[] bArr = new byte[1024];
int size = responseStream.Read(bArr, 0, (int)bArr.Length);
while (size > 0)
{
stream.Write(bArr, 0, size);
size = responseStream.Read(bArr, 0, (int)bArr.Length);
}
stream.Close();
responseStream.Close();
}
5.对下载的文件进行压缩
string zipPath = Server.MapPath("~/ExportFile/") + newFolder + ".rar";
ZipFile(newFolderPath, zipPath);
6.压缩的方法
#region 压缩文件夹
/// <summary>
/// 压缩文件夹
/// </summary>
/// <param name="strFile">目标文件</param>
/// <param name="strZip">压缩后的文件路径</param>
public void ZipFile(string strFile, string strZip)
{
if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar)
strFile += Path.DirectorySeparatorChar;
ZipOutputStream s = new ZipOutputStream(System.IO.File.Create(strZip));
s.SetLevel(6);
zip(strFile, s, strFile);
s.Finish();
s.Close();
}
private void zip(string strFile, ZipOutputStream s, string staticFile)
{
if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar) strFile += Path.DirectorySeparatorChar;
Crc32 crc = new Crc32();
string[] filenames = Directory.GetFileSystemEntries(strFile);
foreach (string file in filenames)
{
if (Directory.Exists(file))
{
zip(file, s, staticFile);
}
else // 否则直接压缩文件
{
//打开压缩文件
FileStream fs = System.IO.File.OpenRead(file);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
string tempfile = file.Substring(staticFile.LastIndexOf("\\") + 1);
ZipEntry entry = new ZipEntry(tempfile);
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);
}
}
}
#endregion
7.就根据你的实际情况随意下载第5步的zipPath 压缩包了。