C# 复制文件并下载

因为该项目背景是需要把一些模板和另一台服务器的附件都打包下载,所以我们要先把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 压缩包了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值