b/s 读取多个FTP文件(图片,视频)压缩到服务器 下载到客户端

其实需求是这样,

要做一键导出,

有图片,有照片,youhtml,存在不同的文件夹,每次下载都必须下载最新数据,因为FTP是随时更新的。

1.这要是一直下载下载,浏览器一直跳窗口,蛋疼的我都看不下去。所以,只能选择别的办法

2.要是放在WEB下载,我觉得肯定会崩溃的呀,所以想选择客户端直接下载,但是必须得装插件,类型ActionX之类的。头给否认了。我凑,只能想别的办法

3.只能先用WEB把流读下来,压缩,然后传给用户,很蛋疼,这要是谁点了,多考验服务器内存CPU,但是头说了,这样也比插件好,好吧,开始做吧 

学习连接

http://www.csharpwin.com/dotnetspace/7579r311.shtml

下面是代码

     #region Zip multi files

        public static int Zip(string destFolder, string[] srcFiles, string folderName, CompressLevel level)
        {
            OperFileFTP operFileFTP = new OperFileFTP();

            ZipOutputStream zipStream = null;

            int count = 0;


            string snappath = "ftp://ip";

            try
            {

                //Use Crc32
                Crc32 crc32 = new Crc32();

                Stream ftpoutStream = null;

                //Create Zip File
                zipStream = new ZipOutputStream(File.Create(destFolder));

                //Specify Level
                zipStream.SetLevel(Convert.ToInt32(level));

                #region Foreach File;
                foreach (string file in srcFiles)
                {
                    Uri u;
                    if (file != null)
                    {
                        string filepath = file;
                        string ftpfilepath = file;
                        if (Path.GetExtension(file) == ".jpg")
                        {
                            u = new Uri(snappath);
                            filepath = filepath.Replace("\\", "/");
                            ftpfilepath = ftpfilepath.Replace("\\", "/");
                        }

                        else
                        {
                            u = new Uri(ftppath);
                        }
                        filepath = filepath.Substring(0, filepath.LastIndexOf("/"));

                        clsFTP cs = new clsFTP(u, entity.User_Name, entity.Pwd);


                        byte[] buffer = cs.DownloadFile(ftpfilepath);



                        //Specify ZipEntry
                        crc32.Reset();
                        crc32.Update(buffer);
                        ZipEntry zipEntry = new ZipEntry(Path.Combine(filepath, Path.GetFileName(file)));
                        zipEntry.DateTime = DateTime.Now;
                        zipEntry.Size = buffer.Length;
                        zipEntry.Crc = crc32.Value;

                        //Put file info into zip stream
                        zipStream.PutNextEntry(zipEntry);

                        zipStream.Write(buffer, 0, buffer.Length);

                        count++;
                    }
                }

                if (!string.IsNullOrEmpty(folderName))
                {
                    byte[] buffer = System.Text.Encoding.Default.GetBytes(folderName);

                    //Specify ZipEntry
                    crc32.Reset();
                    crc32.Update(buffer);
                    ZipEntry zipEntry = new ZipEntry(Path.Combine("", Path.GetFileName("OperationReport.htm")));
                    zipEntry.DateTime = DateTime.Now;
                    zipEntry.Size = buffer.Length;
                    zipEntry.Crc = crc32.Value;

                    //Put file info into zip stream
                    zipStream.PutNextEntry(zipEntry);

                    //Put file data into zip stream
                    zipStream.Write(buffer, 0, buffer.Length);

                    count++;
                }
                #endregion;


                if (ftpoutStream != null)
                    ftpoutStream.Flush();
            }
            catch
            {
                throw;
            }
            finally
            {
                //Clear Resource


                if (zipStream != null)
                {
                    zipStream.Finish();
                    zipStream.Close();
                }
            }
            FileStream stream = new FileStream(destFolder, FileMode.Open);
            //long cl = stream.ContentLength;

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpContext.Current.Server.UrlEncode(Path.GetFileName(destFolder)));

            int bufferSize = 2048;

            int readCount;

            byte[] bufferr = new byte[bufferSize];

            char[] cChar = Encoding.ASCII.GetChars(bufferr);
            //接收FTP文件流

            while ((readCount = stream.Read(bufferr, 0, 2048)) > 0)
            {
                HttpContext.Current.Response.OutputStream.Write(bufferr, 0, readCount);
                HttpContext.Current.Response.Flush();
            }
            stream.Flush();
            stream.Close();

            if (File.Exists(destFolder))
                File.Delete(destFolder);
            return count;

        }

        #endregion

方法如上。。

一下为引用代码。。

时间有限,有机会补充

 

   string[] files = new string[3]; 

   Zip(context.Server.MapPath("Tech_" + id + ".zip"), files, str, CompressLevel.Level8);

 

转载于:https://www.cnblogs.com/yes-you-can/p/3985747.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值