有一个需求,是根据多个模板文件生成固定格式的文件与文件夹结构,实现较简单,做一下记录:

首先创建需要的树形文件夹

    //创建学科目录CreateDir(name_abbr);protected void CreateDir(string name_abbr){string subject_path = Base_Dir + "/" + name_abbr;if (!Directory.Exists(subject_path))Directory.CreateDirectory(subject_path);string subject_gb_path = subject_path + "/gb";if (!Directory.Exists(subject_gb_path))Directory.CreateDirectory(subject_gb_path);string subject_gb_SearchBar_path = subject_gb_path + "/SearchBar";if (!Directory.Exists(subject_gb_SearchBar_path))Directory.CreateDirectory(subject_gb_SearchBar_path);}

创建文件:

string Base_Dir = @System.AppDomain.CurrentDomain.BaseDirectory + @"DownloadFiles";
string filePath = Base_Dir + "/" + name_abbr+"/"+ fileName + ".xml";
string txt = "写入文本的内容";
System.IO.File.WriteAllText(filePath, txt, Encoding.UTF8);

压缩文件与文件夹:


压缩学科文件夹生成学科压缩包 name_abbr.zip
string name_abbr = "计算机科学";
string Base_Dir = @System.AppDomain.CurrentDomain.BaseDirectory + @"DownloadFiles";
string sourceFilePath = Base_Dir + "/" + name_abbr;
string destinationZipFilePath = Base_Dir + "/" + name_abbr + ".zip";
ZipHelper.CreateZip(sourceFilePath, destinationZipFilePath);public class ZipHelper{/// <summary>/// 压缩文件/// </summary>/// <param name="sourceFilePath">待拷贝文件或文件夹 绝对路径</param>/// <param name="destinationZipFilePath"> 生成zip文件的绝对路径 </param>public static void CreateZip(string sourceFilePath, string destinationZipFilePath){if (sourceFilePath[sourceFilePath.Length - 1] != System.IO.Path.DirectorySeparatorChar)sourceFilePath += System.IO.Path.DirectorySeparatorChar;ZipOutputStream zipStream = new ZipOutputStream(File.Create(destinationZipFilePath));zipStream.SetLevel(6);  // 压缩级别 0-9CreateZipFiles(sourceFilePath, zipStream, sourceFilePath);zipStream.Finish();zipStream.Close();}/// <summary>/// 递归压缩文件/// </summary>/// <param name="sourceFilePath">待压缩的文件或文件夹路径</param>/// <param name="zipStream">打包结果的zip文件路径(类似 D:\WorkSpace\a.zip),全路径包括文件名和.zip扩展名</param>/// <param name="staticFile"></param>private static void CreateZipFiles(string sourceFilePath, ZipOutputStream zipStream, string staticFile){Crc32 crc = new Crc32();string[] filesArray = Directory.GetFileSystemEntries(sourceFilePath);foreach (string file in filesArray){if (Directory.Exists(file))                     //如果当前是文件夹,递归{CreateZipFiles(file, zipStream, staticFile);}else                                            //如果是文件,开始压缩{FileStream fileStream = File.OpenRead(file);byte[] buffer = new byte[fileStream.Length];fileStream.Read(buffer, 0, buffer.Length);string tempFile = file.Substring(staticFile.LastIndexOf("\\") + 1);ZipEntry entry = new ZipEntry(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);}}}}

从服务器上下载压缩包保存至本地:

string Base_Dir = @System.AppDomain.CurrentDomain.BaseDirectory + @"DownloadFiles";
this.OutPutZipFile(Request.RequestContext.HttpContext.ApplicationInstance.Context,  "target.zip");//调用方式
protected void OutPutZipFile(HttpContext context, string fileName){string filePath = Base_Dir + "/" + fileName ;FileStream fs = new FileStream(filePath, FileMode.Open);//使用字节流的方式打开byte[] bytes = new byte[(int)fs.Length];fs.Read(bytes, 0, bytes.Length);fs.Close();context.Response.ContentType = "application/octet-stream";context.Response.AddHeader("Title", fileName);context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));context.Response.BinaryWrite(bytes);context.Response.Flush();context.Response.End();}

删除文件以及文件夹

string Base_Dir = @System.AppDomain.CurrentDomain.BaseDirectory + @"DownloadFiles";
string foladerPath = Base_Dir + "\\" + "target.zip";
DeleteDirectory(Base_Dir, foladerPath);//删除文件
DeleteDirectory(Base_Dir, foladerPath.Replace(".zip", ""));//删除文件夹private void DeleteDirectory(string directoryPath, string fileName){//删除文件for (int i = 0; i < Directory.GetFiles(directoryPath).ToList().Count; i++){if (Directory.GetFiles(directoryPath)[i] == fileName){File.Delete(fileName);}}//删除文件夹for (int i = 0; i < Directory.GetDirectories(directoryPath).ToList().Count; i++){if (Directory.GetDirectories(directoryPath)[i] == fileName){Directory.Delete(fileName, true);}}}

参考博客:

总结C#获取当前路径的7种方法

C# 删除文件以及文件夹

C#的几种压缩文件方法

C#从服务器下载文件的四种方式

C#打包文件夹成zip格式(包括文件夹和子文件夹下的所有文件)

c#之从服务器下载压缩包,并解压

C# 在服务器生成文件/文件夹并压缩下载到本地相关推荐

  1. 网站服务器上生成csr,Zeus Web服务器生成CSR文件的方法

    申请SSL证书是必须要生成CSR文件的,不同的服务器生成CSR文件的方法是不一样的,为了能照顾到大家,安信SSL证书会不定期更新不同服务器生成CSR的方法,这篇文章主要为大家介绍Zeus Web服务器 ...

  2. 服务器csr信息是什么,服务器生成csr文件

    服务器生成csr文件 内容精选 换一换 安装证书前,需要获取证书文件和密码文件,请根据申请证书时选择的证书请求文件生成方式来选择操作步骤:如果申请证书时,证书请求文件选择系统生成CSR,具体操作请参见 ...

  3. 如何在Jetty Java HTTPS Servlet Web服务器生成CSR文件

    SSL证书申请流程有点复杂,首先是下单,然后生成CSR文件,最后是CA机构进行审核.不同服务器生成CSR文件的方法有些不一样. 1.运行私钥生成命令 通过运行以下命令生成密钥库和私钥: keytool ...

  4. Zeus Web服务器生成CSR文件的方法

    申请ssl证书是必须要生成CSR文件的,不同的服务器生成CSR文件的方法是不一样的,为了能照顾到大家,我们会不定期更新不同服务器生成CSR的方法,下面主要为大家介绍Zeus Web服务器生成CSR文件 ...

  5. 微信小程序实现生成二维码功能并下载到本地

    微信小程序实现生成二维码功能并下载到本地 背景 实现 备注 背景 有这样一个需求,后台返回了url地址,微信小程序将url地址转成二维码图片,展示在页面上,并且该二维码图片可下载到用户手机相册中 实现 ...

  6. 服务器生成php文件夹下,PHP创建文件以供下载,而不在服务器上保存

    终极目标:我想创建一个网页,用户可以在表单中输入信息.有了这些信息,我想创建一个html文件(下面称为test-download.html),方法是将给定的信息插入模板,然后强制下载.因为我想在即将到 ...

  7. 服务器生成镜像文件,云服务器 生成镜像

    云服务器 生成镜像 内容精选 换一换 可以考虑如下方案:(推荐)使用云备份服务为云服务器创建定时备份.当云服务器故障时,选择需要恢复的时间点所对应的备份副本,将其制作为整机镜像,然后使用该镜像申请新的 ...

  8. php 复制文件夹并压缩到最小_php压缩多个文件打包成zip并下载到本地

    完成时间:2018-01-03 展现方式:整个相册图片的导出(下载到本地)直接在页面上生成下载没有什么另存为什么默认路径 基本步骤: 1.用户点击按钮跳转到对应相册导出的方法 ---->2.在方 ...

  9. 租用的外网Server(ubuntu)上传文件到Dropbox,再下载回本地

    最近需要下载大量的外网东西,虽然有科学上网支撑,但是还是不是很方便,因此决定采用迂回策略: 首先租用外网服务器,快速下载需要的东西,甚至可以下载完后进行预处理,减小体积: 然后外网服务器往回传输到本地 ...

  10. 服务器生成js文件,Next.js 静态生成和服务器端渲染

    Next.js具有两种形式的预渲染:静态生成和服务器端渲染 getStaticProps(静态生成):在构建时获取数据. getStaticPaths(静态生成):根据数据指定要[动态]渲染的[动态路 ...

最新文章

  1. GPU运行Tensorflow详细教程及错误解决
  2. 分布式定时任务调度系统技术选型
  3. 推荐些在线小制作小工具
  4. 同步、异步、阻塞、非阻塞
  5. 100. Same Tree
  6. 爬虫-练习-爬取访问后可见的内容
  7. 比dropout更好的方法_比较自己的更好方法
  8. 不使用额外空间交换2个数据的源代码
  9. 谈谈JavaScript中的数组、集合及效率
  10. dup,dup2函数【转】
  11. Maven运行报错:-Dmaven.multiModuleProjectDirectory system propery is not set.
  12. Python字符串加密
  13. [CareerCup] 18.4 Count Number of Two 统计数字2的个数
  14. 偏最小二乘法_实例讲解:简明扼要最小二乘法计算过程
  15. 如何快速转载网页博客
  16. IAR(For STM32) 安装,配置,工程创建,下载,调试
  17. If you are using WebLogic you will need to add ‘org.slf4j‘ to prefer-application-packages in
  18. 【vn.py】SpreadTrading价差交易
  19. 电感和磁珠有哪些区别?
  20. 最好的礼物是忠言,最好的修养是宽恕。

热门文章

  1. 【三维路径规划】基于matlab麻雀搜索算法无人机三维路径规划【含Matlab源码 171期】
  2. 【腐蚀率预测】基于matlab GUI BP神经网络钢筋腐蚀率预测【含Matlab源码 107期】
  3. SPSS调节效应检验(图文+数据集)【SPSS 045期】
  4. c语言程序设计精髓第七周编程题,C语言程序设计精髓编程题在线测试
  5. artsy 爬虫_让我们得到Artsy! 使用神经网络创建自定义Snapchat过滤器!
  6. ai转型指南_穿越AI转型的转折点
  7. Oracle的sql条件带有,oraclesql语句的if
  8. vs中c语言图形显示,VS2010/MFC编程入门之四十九(图形图像:CDC类及其屏幕绘图函数)...
  9. python苹果版安装包_新手必看。关于Python3——windows安装与运行(详细版)
  10. oracle如何修改字段为空的为0,Oracle中字段为空处理成0