//遍历服务器指定文件夹下的所有文件
string path = "uploads/Image/";
string serverPath = Server.MapPath(path); //创建临时文件夹
string tempName = DateTime.Now.ToString("yyyyMMddHHMMss");
string tempFolder = Path.Combine(serverPath, tempName);
Directory.CreateDirectory(tempFolder); DirectoryInfo folder = new DirectoryInfo(serverPath);
foreach (FileInfo file in folder.GetFiles())
{
string filename = file.Name;
File.Copy(serverPath + "/" + filename, tempFolder + "/" + filename);
}
//ZKHelper.JSHelper.Alert("图片拷贝成功!");
//产生RAR文件,及文件输出
RARSave(tempFolder, tempName);
DownloadRAR(tempFolder + "\\" + tempName + ".rar");
         /// <summary>
/// 生成RAR文件
/// </summary>
/// <param name="path">存放复制文件的目录</param>
/// <param name="rarPatch">RAR文件存放目录</param>
/// <param name="rarName">RAR文件名</param>
private void RARSave(string rarPatch, string rarName)
{
string the_rar;
RegistryKey the_Reg;
Object the_Obj;
string the_Info;
ProcessStartInfo the_StartInfo;
Process the_Process;
try
{
the_Reg = Registry.ClassesRoot.OpenSubKey(@"WinRAR");
the_Obj = the_Reg.GetValue("");
the_rar = the_Obj.ToString();
the_Reg.Close();
the_rar = the_rar.Substring(, the_rar.Length - );
the_Info = " a " + rarName + " -r";
the_StartInfo = new ProcessStartInfo();
the_StartInfo.FileName = "WinRar";//the_rar;
the_StartInfo.Arguments = the_Info;
the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
//打包文件存放目录
the_StartInfo.WorkingDirectory = rarPatch;
the_Process = new Process();
the_Process.StartInfo = the_StartInfo;
the_Process.Start();
the_Process.WaitForExit();
the_Process.Close();
}
catch (Exception)
{
throw;
}
}

下载生成的RAR文件

         /// <summary>
/// 下载生成的RAR文件
/// </summary>
private void DownloadRAR(string file)
{
FileInfo fileInfo = new FileInfo(file);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileInfo.Name);
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.WriteFile(fileInfo.FullName);
Response.Flush();
string tempPath = file.Substring(, file.LastIndexOf("\\"));
//删除临时目录下的所有文件
DeleteFiles(tempPath);
//删除空目录
Directory.Delete(tempPath);
Response.End();
}
          /// <summary>
/// 删除临时目录下的所有文件
/// </summary>
/// <param name="tempPath">临时目录路径</param>
private void DeleteFiles(string tempPath)
{
DirectoryInfo directory = new DirectoryInfo(tempPath);
try
{
foreach (FileInfo file in directory.GetFiles())
{
if (file.Attributes.ToString().IndexOf("ReadOnly") != -)
{
file.Attributes = FileAttributes.Normal;
}
File.Delete(file.FullName);
}
}
catch (Exception)
{
throw;
}
}

最新文章

  1. 如何理解 卷积 和pooling
  2. [转] Unity Mathf 数学运算(C#)
  3. Mac 显示 Finder 隐藏文件
  4. JVM垃圾回收机制总结(4) :新一代的垃圾回收算法
  5. Java Web模块——验证码模块
  6. Demo学习: Basic jQuery
  7. Asp.Net 之 网页快照
  8. Missra开源前端框架
  9. 向Git证明自己的身份,Git别名配置
  10. chrome(转)
  11. PyCharm:2017.3版即将新增科学计算模式,预览版现在可以下载使用
  12. MeasureString 通过文本宽度获取绘制高度
  13. 51nod1556 计算(默慈金数)
  14. java -相关技术
  15. SSH框架整合实现Java三层架构实例(一)
  16. npm安裝、卸載、刪除、撤銷發佈包、更新版本信息
  17. centos下载
  18. GeoServer服务器环境的搭建
  19. c#方法转异步例子
  20. 洛谷P4338 [ZJOI2018]历史(LCT,树形DP,树链剖分)

热门文章

  1. NHibernate之映射文件配置说明(转载1)
  2. xcode下载方式
  3. java鼠标与键盘事件监听
  4. map的使用方法
  5. ref参数的用途
  6. js判断当前操作系统
  7. C#编码标准--编码习惯
  8. discuz3.2x增加邮箱验证功能
  9. iOS学习之数据解析
  10. Scala学习笔记--Akka