asp.net文件压缩,下载,物理路径,相对路径,删除文件
知识动手实践一次,就可以变成自己的了。不然一直是老师的,书本的。
这几天做了一个小小的项目,需要用到文件下载功能,期初想到只是单个的文件,后面想到如果很多文件怎么办?于是又想到文件压缩。几经波折实践,总是达到了我想要的效果了。现今把作为一个笔记记录下来,以便下次之用。
- #region 物理路径和相对路径的转换
- //本地路径转换成URL相对路径
- public static string urlconvertor(string imagesurl1)
- {
- string tmpRootDir = HttpContext.Current.Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录
- string imagesurl2 = imagesurl1.Replace(tmpRootDir, ""); //转换成相对路径
- imagesurl2 = imagesurl2.Replace(@"\", @"/");
- //imagesurl2 = imagesurl2.Replace(@"Aspx_Uc/", @"");
- return imagesurl2;
- }
- //相对路径转换成服务器本地物理路径
- public static string urlconvertorlocal(string imagesurl1)
- {
- string tmpRootDir = HttpContext.Current.Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录
- string imagesurl2 = tmpRootDir + imagesurl1.Replace(@"/", @"\"); //转换成绝对路径
- return imagesurl2;
- }
- #endregion
- /// <summary>
- /// 获取物理地址
- /// </summary>
- public static string MapPathFile(string FileName)
- {
- return HttpContext.Current.Server.MapPath(FileName);
- }
- /// <summary>
- /// 普通下载
- /// </summary>
- /// <param name="FileName">文件虚拟路径</param>
- public static bool DownLoadold(string FileName)
- {
- bool bools = false;
- string destFileName = FileName;// urlconvertor(FileName);// MapPathFile(FileName);
- if (File.Exists(destFileName))
- {
- FileInfo fi = new FileInfo(destFileName);
- HttpContext.Current.Response.Clear();
- HttpContext.Current.Response.ClearHeaders();
- HttpContext.Current.Response.Buffer = false;
- HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName(destFileName), System.Text.Encoding.UTF8));
- HttpContext.Current.Response.AppendHeader("Content-Length", fi.Length.ToString());
- HttpContext.Current.Response.ContentType = "application/octet-stream";
- HttpContext.Current.Response.WriteFile(destFileName);
- HttpContext.Current.Response.Flush();
- AiLvYou.Common.DownUnit.FilePicDelete(destFileName);//删除当前下载的文件
- HttpContext.Current.Response.End();
- bools = true;
- }
- return bools;
- }
- //检查文件是否已经存在存在才下载
- public static bool FileExists(string path)
- {
- bool ret = false;
- System.IO.FileInfo file = new System.IO.FileInfo(path);
- if (file.Exists)
- {
- ret = true;
- }
- return ret;
- }
- /// <summary>
- /// 删除单个文件
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- public static bool FilePicDelete(string path)
- {
- bool ret = false;
- System.IO.FileInfo file = new System.IO.FileInfo(path);
- if (file.Exists)
- {
- file.Delete();
- ret = true;
- }
- //else //删除目录
- //{
- // Directory.Delete(file );
- //}
- return ret;
- }
- /// <summary>
- /// 用递归方法删除文件夹目录及文件
- /// </summary>
- /// <param name="dir">带文件夹名的路径</param>
- public static void DeleteFolder(string dir)
- {
- if (Directory.Exists(dir)) //如果存在这个文件夹删除之
- {
- foreach (string d in Directory.GetFileSystemEntries(dir))
- {
- if (File.Exists(d))
- File.Delete(d); //直接删除其中的文件
- else
- DeleteFolder(d); //递归删除子文件夹
- }
- Directory.Delete(dir, true); //删除已空文件夹
- }
- }
第一种压缩方法 用WinRAR 需要安装此软件 ,而且压缩了整个目录,目前还没有解决
- //会有整个目录
- public void RARsave(string patch, 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 - );
- Directory.CreateDirectory(patch);//20151222
- //命令参数
- //the_Info = " a " + rarName + " " + @"C:Test?70821.txt"; //文件压缩
- the_Info = " a " + rarName + " " + patch +" -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 ex)
- {
- throw ex;
- }
- }
- /**//****************************************
- * 函数名称:CopyDir
- * 功能说明:将指定文件夹下面的所有内容copy到目标文件夹下面 果目标文件夹为只读属性就会报错。
- * 参 数:srcPath:原始路径,aimPath:目标文件夹
- * 调用示列:
- * string srcPath = Server.MapPath("test/");
- * string aimPath = Server.MapPath("test1/");
- * EC.FileObj.CopyDir(srcPath,aimPath);
- *****************************************/
- /**//// <summary>
- /// 指定文件夹下面的所有内容copy到目标文件夹下面
- /// </summary>
- /// <param name="srcPath">原始路径</param>
- /// <param name="aimPath">目标文件夹</param>
- public static void CopyDir(string srcPath, string aimPath)
- {
- try
- {
- // 检查目标目录是否以目录分割字符结束如果不是则添加之
- if (aimPath[aimPath.Length - ] != Path.DirectorySeparatorChar)
- aimPath += Path.DirectorySeparatorChar;
- // 判断目标目录是否存在如果不存在则新建之
- if (!Directory.Exists(aimPath))
- Directory.CreateDirectory(aimPath);
- // 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
- //如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法
- //string[] fileList = Directory.GetFiles(srcPath);
- string[] fileList = Directory.GetFileSystemEntries(srcPath);
- //遍历所有的文件和目录
- foreach (string file in fileList)
- {
- //先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
- if (Directory.Exists(file))
- CopyDir(file, aimPath + Path.GetFileName(file));
- //否则直接Copy文件
- else
- File.Copy(file, aimPath + Path.GetFileName(file), true);
- }
- }
- catch (Exception ee)
- {
- throw new Exception(ee.ToString());
- }
- }
使用代码:
- protected void btndown_Click(object sender, EventArgs e)
- {
- string Folder = DateTime.Now.ToString("HHMMss");
- string savefilepath = HttpContext.Current.Server.MapPath("/admin/ueditor/net/htmlfile/upload");
- string savefilepath2 = HttpContext.Current.Server.MapPath("/admin/ueditor/net/htmlfile/upload/image");
- string tempFolder = Path.Combine(savefilepath, Folder);
- Directory.CreateDirectory(tempFolder);
- CopyDir(savefilepath2, tempFolder);
- // RARsave(@"D:\images", tempFolder, Folder);控制了解压目录但是没有文件
- RARsave(tempFolder, tempFolder, Folder);
- }
第二种压缩方法,使用了 ICSharpCode.SharpZipLib.dll 文件。先下载,放到bin目录下,然后引用。
实现代码
- /// <summary>
- /// 生成压缩文件
- /// </summary>
- /// <param name="strZipPath">生成的zip文件的路径</param>
- /// <param name="strZipTopDirectoryPath">源文件的上级目录</param>
- /// <param name="intZipLevel">T压缩等级</param>
- /// <param name="strPassword">压缩包解压密码</param>
- /// <param name="filesOrDirectoriesPaths">源文件路径</param>
- /// <returns></returns>
- private bool Zip(string strZipPath, string strZipTopDirectoryPath, int intZipLevel, string strPassword, string[] filesOrDirectoriesPaths)
- {
- try
- {
- List<string> AllFilesPath = new List<string>();
- if (filesOrDirectoriesPaths.Length > ) // get all files path
- {
- for (int i = ; i < filesOrDirectoriesPaths.Length; i++)
- {
- if (File.Exists(filesOrDirectoriesPaths[i]))
- {
- AllFilesPath.Add(filesOrDirectoriesPaths[i]);
- }
- else if (Directory.Exists(filesOrDirectoriesPaths[i]))
- {
- GetDirectoryFiles(filesOrDirectoriesPaths[i], AllFilesPath);
- }
- }
- }
- if (AllFilesPath.Count > )
- {
- ZipOutputStream zipOutputStream = new ZipOutputStream(File.Create(strZipPath));
- zipOutputStream.SetLevel(intZipLevel);
- zipOutputStream.Password = strPassword;
- for (int i = ; i < AllFilesPath.Count; i++)
- {
- string strFile = AllFilesPath[i].ToString();
- try
- {
- if (strFile.Substring(strFile.Length - ) == "") //folder
- {
- string strFileName = strFile.Replace(strZipTopDirectoryPath, "");
- if (strFileName.StartsWith(""))
- {
- strFileName = strFileName.Substring();
- }
- ZipEntry entry = new ZipEntry(strFileName);
- entry.DateTime = DateTime.Now;
- zipOutputStream.PutNextEntry(entry);
- }
- else //file
- {
- FileStream fs = File.OpenRead(strFile);
- byte[] buffer = new byte[fs.Length];
- fs.Read(buffer, , buffer.Length);
- string strFileName = strFile.Replace(strZipTopDirectoryPath, "");
- if (strFileName.StartsWith(""))
- {
- strFileName = strFileName.Substring();
- }
- ZipEntry entry = new ZipEntry(strFileName);
- entry.DateTime = DateTime.Now;
- zipOutputStream.PutNextEntry(entry);
- zipOutputStream.Write(buffer, , buffer.Length);
- fs.Close();
- fs.Dispose();
- }
- }
- catch
- {
- continue;
- }
- }
- zipOutputStream.Finish();
- zipOutputStream.Close();
- return true;
- }
- else
- {
- return false;
- }
- }
- catch
- {
- return false;
- }
- }
- /// <summary>
- /// Gets the directory files.
- /// </summary>
- /// <param name="strParentDirectoryPath">源文件路径</param>
- /// <param name="AllFilesPath">所有文件路径</param>
- private void GetDirectoryFiles(string strParentDirectoryPath, List<string> AllFilesPath)
- {
- string[] files = Directory.GetFiles(strParentDirectoryPath);
- for (int i = ; i < files.Length; i++)
- {
- AllFilesPath.Add(files[i]);
- }
- string[] directorys = Directory.GetDirectories(strParentDirectoryPath);
- for (int i = ; i < directorys.Length; i++)
- {
- GetDirectoryFiles(directorys[i], AllFilesPath);
- }
- if (files.Length == && directorys.Length == ) //empty folder
- {
- AllFilesPath.Add(strParentDirectoryPath);
- }
- }
调用代码:
- protected void btndowmload_Click(object sender, EventArgs e)
- {
- //http://www.cnblogs.com/LYunF/archive/2012/02/18/2357591.html
- string Folder = DateTime.Now.ToString("HHMMss");
- string savefilepath = HttpContext.Current.Server.MapPath("/admin/ueditor/net/htmlfile/upload");
- string savefilepath2 = HttpContext.Current.Server.MapPath("/admin/ueditor/net/htmlfile/upload/image");
- // 判断目标目录是否存在如果不存在则新建之
- /*
- string newfilepath = Path.Combine(savefilepath, Folder);
- if (!Directory.Exists(newfilepath))
- Directory.CreateDirectory(newfilepath);//问题目录问题
- string newfilepath2 = HttpContext.Current.Server.MapPath("/admin/ueditor/net/htmlfile/upload"+"/"+Folder);
- string strZipTopDirectoryPath = newfilepath2;
- string tempFolder = Path.Combine(newfilepath2, Folder);
- */
- string tempFolder = Path.Combine(savefilepath, Folder);
- string strZipTopDirectoryPath = savefilepath;
- string strZipPath = tempFolder + ".zip";
- int intZipLevel = ;
- string strPassword = "";
- string[] filesOrDirectoriesPaths = new string[] { savefilepath2 };
- bool flag = Zip(strZipPath, strZipTopDirectoryPath, intZipLevel, strPassword, filesOrDirectoriesPaths);
- if (flag)
- {
- ResponseFile(strZipPath);
- }
- }
下载代码:
- protected void ResponseFile(string fileName)
- {
- FileInfo fileInfo = new FileInfo(fileName);
- Response.Clear();
- Response.ClearContent();
- Response.ClearHeaders();
- // Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName); //upload 这里可以改变下载名称
- Response.AddHeader("Content-Disposition", "attachment;filename=upload.zip"); //
- 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 = fileName.Substring(0, fileName.LastIndexOf("\\"));
- AiLvYou.Common.DownUnit.FilePicDelete(fileName);
- // Directory.Delete(tempPath);
- Response.End();
- }
asp.net文件压缩,下载,物理路径,相对路径,删除文件的更多相关文章
- 文件压缩、解压工具类。文件压缩格式为zip
package com.JUtils.file; import java.io.BufferedOutputStream; import java.io.File; import java.io.Fi ...
- php多文件压缩下载
/*php多文件压缩并且下载*/ function addFileToZip($path,$zip){ $handler=opendir($path); //打开当前文件夹由$path指定. whil ...
- Java 多个文件压缩下载
有时候会有多个附件一起下载的需求,这个时候最好就是打包下载了 首先下面这段代码是正常的单个下载 public void Download(@RequestParam("file_path&q ...
- linux文件权限总结(创建root不可以删除文件、只可追加的日志文件等)
文件类型 对于文件和目录的访问权力是根据读访问,写访问,和执行访问来定义的. 我们来看一下 ls 命令的输出结果 [root@iZ28dr6w0qvZ test]# ls -l 总用量 72 -rw- ...
- asp.net mvc 文件压缩下载
压缩文件相关的类: public class ZIPCompressUtil { public static Tuple<bool, Stream> Zip(string strZipTo ...
- ftp多文件压缩下载
@GetMapping(value = "/find") public String findfile(String filePath, String fileNames, Htt ...
- java多个文件压缩下载
public static void zipFiles(File[] srcfile,ServletOutputStream sos){ byte[] buf=new byte[1024]; try ...
- UIwebview 文件的下载与保存,以及mp3文件的播放
这里只是说说异步 单线程下载与文件的保存 以下载一个mp3文件并保存为例: -(void)loading { //设置文件下载地址 NSString *urlString = [NSString st ...
- 【2017-05-30】WebForm文件上传。从服务端删除文件
用 FileUpload控件进行上传文件. <asp:FileUpload ID="FileUpload1" runat="server" /> ...
- 用Spring中的ResponseEntity文件批量压缩下载
我看了很多网上的demo,先生成ZIP压缩文件,然后再下载. 我这里是生成ZIP文件流 进行下载.(核心代码没多少,就是一些业务代码) @RequestMapping(value = "/& ...
随机推荐
- BZOJ3296: [USACO2011 Open] Learning Languages 并查集
Description 农夫约翰的N(2 <= N<=10,000)头奶牛,编号为1.. N,一共会流利地使用M(1<= M <=30,000)种语言,编号从1 .. M., ...
- Python实现自平衡二叉树AVL
# -*- coding: utf-8 -*- from enum import Enum #参考http://blog.csdn.net/niteip/article/details/1184069 ...
- python 元组查找元素返回索引
#create a tuple tuplex = tuple("index tuple") print(tuplex) #get index of the first item w ...
- Django2.0 URL配置
一.实例 先看一个例子: from django.urls import path from . import views urlpatterns = [ path('articles/2003/', ...
- Qt_QString::split测试
1. #define GID_PREFIX "dr_" QString str = "dr__awedr4"; QString str1; QStringLis ...
- C++异常及捕获_01
ZC: Win7x64 + qt-opensource-windows-x86-msvc2010_opengl-5.3.2.exe 1. class AA { public: void A() { & ...
- C89_一些函数
C89 string.h 中的函数: 复制函数 memcpy memmove strcpy strncpy 串接函数 strcat strncat 比较函数 memcmp strcmp strcoll ...
- Cglib方法实现动态代理
除了使用JDK方式产生动态代理外,Java还给我们提供了另外一种产生动态代理的方法,那就是使用cglib. cglib是这样实现动态代理的: · ①.针对类来实现代理 · ②对指定目标类产生一个子类 ...
- Codeforces D - The Child and Zoo
D - The Child and Zoo 思路: 并查集+贪心 每条边的权值可以用min(a[u],a[v])来表示,然后按边的权值从大到小排序 然后用并查集从大的边开始合并,因为你要合并的这两个联 ...
- HTML表单组件
HTML表单组件 一.说明 form标签里面的东西 二.效果图 三.代码 <!DOCTYPE html> <html> <head> <title>Fo ...