动态创建zip并将文件添加到其中. [英] creating zip dynamically and adding files into it.

查看:157
本文介绍了动态创建zip并将文件添加到其中.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我.在我的代码中,当用户单击一个按钮下载时,我试图动态创建zip文件夹并将音频,图像,视频文件添加到其中,然后将该文件夹下载到用户系统中.我尝试了以下解决方案:

please help me.in my code when user clicks on one button dowload i am trying to create zip folder dynamically and adding audio,images,video files into it and dowloading that folder into user system.i have tried following solution:

if (ds.Tables[0].Rows.Count > 0)
        {
            byte[] buffer = new byte[4096];

            // the path on the server where the temp file will be created!
            string tempFileName = Server.MapPath(@"1/" + Guid.NewGuid().ToString() + ".zip");

            ZipOutputStream zipOutputStream = new ZipOutputStream(File.Create(tempFileName));
            string filePath = String.Empty;
            string fileName = String.Empty;
            int readBytes = 0;

            foreach (DataRow dr in ds.Tables[0].Rows)
            {               
                filePath = Convert.ToString(dr["EvidencePath"]);               
                fileName = Convert.ToString(dr["FilesName"]);
                ZipEntry zipEntry = new ZipEntry(fileName);
                zipOutputStream.PutNextEntry(zipEntry);

                using (FileStream fs = File.OpenRead(filePath))
                {
                    do
                    {
                        readBytes = fs.Read(buffer, 0, buffer.Length);
                        zipOutputStream.Write(buffer, 0, readBytes);

                    } while (readBytes > 0);
                }
            }

            if (zipOutputStream.Length == 0)
            {                
                return;
            }


            zipOutputStream.Finish();
            zipOutputStream.Close();

            Response.ContentType = "application/x-zip-compressed";
            Response.AppendHeader("Content-Disposition", "attachment; filename=YourFile.zip");
            Response.WriteFile(tempFileName);

            Response.Flush();
            Response.Close();

            // delete the temp file 
            if (File.Exists(tempFileName))
                File.Delete(tempFileName);
            //}
        }


但它只会创建文件夹.没有添加我想要的文件.请帮助我,我正在尝试很多


but it only creates folder. not added the files which i want.please help me i am trying lots

推荐答案


我想我的朋友您还没有用谷歌搜索.
请通过以下链接找到答案.
http://csharpdotnetfreak.blogspot.com/2012/06/create-zip-files-in-aspnet-c-vb.html [ http://stackoverflow.com/questions/681827/how-to-create-and-fill-a-zip-file-using-asp-net [ http://stackoverflow.com/questions/2266204/creating-zip- file-from-stream-and-downloading-it [ ^ ]
http://stackoverflow.com/questions/921308/how-can-i-generate-a-temporary-zip-file-then-auto-remove-it-after-it-is-downloa [
Hi,
I think my friend you have not googled it.
Please go through the links below to find your answer.
http://csharpdotnetfreak.blogspot.com/2012/06/create-zip-files-in-aspnet-c-vb.html[^]
http://stackoverflow.com/questions/681827/how-to-create-and-fill-a-zip-file-using-asp-net[^]
http://stackoverflow.com/questions/2266204/creating-zip-file-from-stream-and-downloading-it[^]
http://stackoverflow.com/questions/921308/how-can-i-generate-a-temporary-zip-file-then-auto-remove-it-after-it-is-downloa[^]
All the best.
--Amit


这篇关于动态创建zip并将文件添加到其中.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆