using Ionic.Zip;
using System.Text;
using System;
using System.Collections.Generic;
protected void Button1_Click(object sender, EventArgs e)
{
string _sTaskId = TaskID;
string _sFormId = FormID;
Unionstars.Bpm.Task item = Unionstars.Bpm.Task.FindByKey(_sTaskId);
if (item != null)
{
try
{
//通过taskid获取Tokenid
IList<Unionstars.Bpm.TokenLast> tokenlastlist = Unionstars.Bpm.TokenLast.FindAll(new string[] { "TaskId" }, new string[] { item.ID });
if (tokenlastlist != null && tokenlastlist.Count > 0)
{
Unionstars.Bpm.TokenLast tokenlastitem = tokenlastlist[0];
//文件名
string dirName = "游学" + (DateTime.Now.ToString("yyyyMMddHHmmss"));
//文件路径
string filePath = @AppDomain.CurrentDomain.BaseDirectory + @"UploadFile\游学文件下载\" + dirName + @"\";
string normalizedPath = System.Text.RegularExpressions.Regex.Replace(filePath, @"[/]{2,}", "/");
string[] aryReg = {"?", "<", ">", "(", ")", "*", "(", ")", "、", "\r\n" };
for (int j = 0; j < aryReg.Length; j++)
{
filePath = filePath.Replace(aryReg[j], string.Empty);
//pdfPath = pdfPath.Replace(aryReg[j], string.Empty);
}
//压缩路径
string zippdfPath = AppDomain.CurrentDomain.BaseDirectory + @"UploadFile\游学文件下载\" + dirName + ".zip";
//文件夹不存在创建
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
var list = Attachment.FindByName("FormID", FormID);
if (!string.IsNullOrEmpty(list.FilePath))
{
string directoryPath = Directory.GetParent(list.FilePath).FullName;//获取文件所在饿文件夹地址
CopyFolder(directoryPath, filePath);
}
using (ZipFile zip = new ZipFile(zippdfPath, Encoding.UTF8))
{
zip.AddDirectory(filePath);
zip.Save();
}
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
//Response.AddHeader("Content-Disposition", "attachment;filename=" + BiaoTi.Text + (DateTime.Now.ToString("yyyyMMddHHmmss")) + ".zip");
//Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("游学" + (DateTime.Now.ToString("yyyyMMddHHmmss")) + ".zip"));
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.Default;
Response.WriteFile(zippdfPath);
Response.Flush();
Response.End();
}
}
catch (Exception ex)
{
Unionstars.Trace.Log.WriteLine("拷贝文件失败1:" + ex.ToString());
}
}
else
{
//提示失败
string strScript = "<script>alert('未找到表单信息!');</script>";
string key = "start";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), key, strScript);
}
}
public static void CopyFolder(string sourceFolder, string destFolder)
{
try
{
//如果目标路径不存在,则创建目标路径
if (!System.IO.Directory.Exists(destFolder))
{
System.IO.Directory.CreateDirectory(destFolder);
}
//得到原文件根目录下的所有文件
string[] files = System.IO.Directory.GetFiles(sourceFolder);
foreach (string file in files)
{
string name = System.IO.Path.GetFileName(file);
string dest = System.IO.Path.Combine(destFolder, name);
System.IO.File.Copy(file, dest, true);//复制文件
}
//得到原文件根目录下的所有文件夹
//string[] folders = System.IO.Directory.GetDirectories(sourceFolder);
//foreach (string folder in folders)
//{
// string name = System.IO.Path.GetFileName(folder);
// string dest = System.IO.Path.Combine(destFolder, name);
// CopyFolder(folder, dest);//构建目标路径,递归复制文件
//}
}
catch (Exception ex)
{
Unionstars.Trace.Log.WriteLine("拷贝文件失败2:" + ex.ToString());
}
}
//复制单个文件到另一个文件夹
public static void CopyFile(string sourceFilePath, string destinationFolderPath)
{
// 获取源文件的完整路径和目标文件夹路径
string destinationFilePath = Path.Combine(destinationFolderPath, Path.GetFileName(sourceFilePath));
// 复制文件
File.Copy(sourceFilePath, destinationFilePath, true); // true表示如果目标文件存在则覆盖它
}
C#将一个文件所在文件夹里面的所有文件复制到另一个文件夹。复制当个文件到另一个文件夹
于 2024-06-20 09:51:13 首次发布