C#多个文件流打包下载。

z22708387 2015-09-03 02:52:21
调用服务器接口,获取若干个文件的文件流。

此时我想把多个文件流压缩成一个压缩包(.zip .7z)下载。

请各位大神帮忙.

如果使用ICSharpCode.SharpZipLib.dll 不知是否能够做到.

如果不使用第三方插件,是否能够做到?

环境注意:没有服务器创建、删除文件的权限。
所以我想到了用内存。不知道有没有大神做过这样的需求。
在线等!
...全文
1121 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
u014444648 2016-05-04
  • 打赏
  • 举报
回复
引用 3 楼 andywangguanxi 的回复:
[quote=引用 2 楼 z22708387 的回复:] [quote=引用 1 楼 andywangguanxi 的回复:] 试试

 //zip文件输出流
        static ZipOutputStream zos = null;
        //压缩附件
        public static void downloadAttach(string filePath, string strFileName, HttpResponse response)
        {
            MemoryStream ms = null;
            response.ContentType = "application/octet-stream";
            strFileName = HttpUtility.UrlEncode(strFileName).Replace('+', ' ');
            response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName + ".zip");
            ms = new MemoryStream();
            zos = new ZipOutputStream(ms);
            addZipEntry(filePath);
            zos.Finish();
            zos.Close();
            response.Clear();
            response.BinaryWrite(ms.ToArray());
            response.End();
        }
        //添加文件至压缩包
        private static void addZipEntry(string PathStr)
        {
            DirectoryInfo di = new DirectoryInfo(PathStr);
            foreach (DirectoryInfo item in di.GetDirectories())
            {
                addZipEntry(item.FullName);
            }
            foreach (FileInfo item in di.GetFiles())
            {
                FileStream fs = File.OpenRead(item.FullName);
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                string strEntryName = item.FullName.Replace(PathStr, "");
                ZipEntry entry = new ZipEntry(strEntryName);
                zos.PutNextEntry(entry);
                zos.Write(buffer, 0, buffer.Length);
                fs.Close();
            }
        }
downloadAttach方法中的filePath参数是代表的什么?[/quote] 这个方法是把filePaht路径下的所有文件打包成一个zip包。不知道你的文件流怎么来的,你可以对addZipEntry进行修改[/quote] 能不能 指定打包该目录下的几个文件。并不一定要整个文件夹里所有的文件。
衣舞晨风 2015-10-28
  • 打赏
  • 举报
回复
C# 下载带进度条代码(普通进度条) http://blog.csdn.net/jiankunking/article/details/45642597
sezvboyrul 2015-10-27
  • 打赏
  • 举报
回复
上面几位都是通过读取文件路径的方式打包
sezvboyrul 2015-10-27
  • 打赏
  • 举报
回复
有没有找到解决方案,我也急需! 文件都是通过web服务读取,跟应用程序不在一个服务器上。通过web服务读取后如何打包下载
孤独de猫 2015-09-03
  • 打赏
  • 举报
回复
直接用打包工具,做成一个exe,然后下载个文件就是了。 我用是NSIS
showjim 2015-09-03
  • 打赏
  • 举报
回复
.NET4.5有System.IO.Compression.dll,在4.0里面也可以使用。
                using (MemoryStream stream = new MemoryStream())
                {
                    using (ZipArchive zip = new ZipArchive(stream, ZipArchiveMode.Create, true))
                    {
                            zip.CreateEntry("filename").Open().Write(...);
                    }
                    return stream;
                }这只是一个简单的例子,中间的Dispose/using自己处理。
EdsionWang 2015-09-03
  • 打赏
  • 举报
回复
引用 2 楼 z22708387 的回复:
[quote=引用 1 楼 andywangguanxi 的回复:] 试试

 //zip文件输出流
        static ZipOutputStream zos = null;
        //压缩附件
        public static void downloadAttach(string filePath, string strFileName, HttpResponse response)
        {
            MemoryStream ms = null;
            response.ContentType = "application/octet-stream";
            strFileName = HttpUtility.UrlEncode(strFileName).Replace('+', ' ');
            response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName + ".zip");
            ms = new MemoryStream();
            zos = new ZipOutputStream(ms);
            addZipEntry(filePath);
            zos.Finish();
            zos.Close();
            response.Clear();
            response.BinaryWrite(ms.ToArray());
            response.End();
        }
        //添加文件至压缩包
        private static void addZipEntry(string PathStr)
        {
            DirectoryInfo di = new DirectoryInfo(PathStr);
            foreach (DirectoryInfo item in di.GetDirectories())
            {
                addZipEntry(item.FullName);
            }
            foreach (FileInfo item in di.GetFiles())
            {
                FileStream fs = File.OpenRead(item.FullName);
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                string strEntryName = item.FullName.Replace(PathStr, "");
                ZipEntry entry = new ZipEntry(strEntryName);
                zos.PutNextEntry(entry);
                zos.Write(buffer, 0, buffer.Length);
                fs.Close();
            }
        }
downloadAttach方法中的filePath参数是代表的什么?[/quote] 这个方法是把filePaht路径下的所有文件打包成一个zip包。不知道你的文件流怎么来的,你可以对addZipEntry进行修改
z22708387 2015-09-03
  • 打赏
  • 举报
回复
引用 1 楼 andywangguanxi 的回复:
试试

 //zip文件输出流
        static ZipOutputStream zos = null;
        //压缩附件
        public static void downloadAttach(string filePath, string strFileName, HttpResponse response)
        {
            MemoryStream ms = null;
            response.ContentType = "application/octet-stream";
            strFileName = HttpUtility.UrlEncode(strFileName).Replace('+', ' ');
            response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName + ".zip");
            ms = new MemoryStream();
            zos = new ZipOutputStream(ms);
            addZipEntry(filePath);
            zos.Finish();
            zos.Close();
            response.Clear();
            response.BinaryWrite(ms.ToArray());
            response.End();
        }
        //添加文件至压缩包
        private static void addZipEntry(string PathStr)
        {
            DirectoryInfo di = new DirectoryInfo(PathStr);
            foreach (DirectoryInfo item in di.GetDirectories())
            {
                addZipEntry(item.FullName);
            }
            foreach (FileInfo item in di.GetFiles())
            {
                FileStream fs = File.OpenRead(item.FullName);
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                string strEntryName = item.FullName.Replace(PathStr, "");
                ZipEntry entry = new ZipEntry(strEntryName);
                zos.PutNextEntry(entry);
                zos.Write(buffer, 0, buffer.Length);
                fs.Close();
            }
        }
downloadAttach方法中的filePath参数是代表的什么?
EdsionWang 2015-09-03
  • 打赏
  • 举报
回复
试试

 //zip文件输出流
        static ZipOutputStream zos = null;
        //压缩附件
        public static void downloadAttach(string filePath, string strFileName, HttpResponse response)
        {
            MemoryStream ms = null;
            response.ContentType = "application/octet-stream";
            strFileName = HttpUtility.UrlEncode(strFileName).Replace('+', ' ');
            response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName + ".zip");
            ms = new MemoryStream();
            zos = new ZipOutputStream(ms);
            addZipEntry(filePath);
            zos.Finish();
            zos.Close();
            response.Clear();
            response.BinaryWrite(ms.ToArray());
            response.End();
        }
        //添加文件至压缩包
        private static void addZipEntry(string PathStr)
        {
            DirectoryInfo di = new DirectoryInfo(PathStr);
            foreach (DirectoryInfo item in di.GetDirectories())
            {
                addZipEntry(item.FullName);
            }
            foreach (FileInfo item in di.GetFiles())
            {
                FileStream fs = File.OpenRead(item.FullName);
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                string strEntryName = item.FullName.Replace(PathStr, "");
                ZipEntry entry = new ZipEntry(strEntryName);
                zos.PutNextEntry(entry);
                zos.Write(buffer, 0, buffer.Length);
                fs.Close();
            }
        }

111,093

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧