Click here to Skip to main content
16,019,055 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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:

C#
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
Posted
Updated 1-Jul-12 20:25pm
v2
Comments
Sergey Alexandrovich Kryukov 2-Jul-12 2:40am    
What zip library are you using? There are some other...
--SA
Vaishali P. Patil 2-Jul-12 3:18am    
using ICSharpCode.SharpZipLib.Zip;
using System.IO.Compression;

1 solution

 
Share this answer
 
Comments
Vaishali P. Patil 2-Jul-12 2:35am    
thank you. i have tried this :

if (ds.Tables[0].Rows.Count > 0)
{
// Create the ZIP file that will be downloaded. Need to name the file something unique ...
string strNow = String.Format("{0:MMM-dd-yyyy_hh-mm-ss}", System.DateTime.Now);
ZipOutputStream zipOS = new ZipOutputStream(File.Create(Server.MapPath("1/") + strNow + ".zip"));
zipOS.SetLevel(5); // ranges 0 to 9 ... 0 = no compression : 9 = max compression

// Loop through the dataset to fill the zip file
foreach (DataRow dr in ds.Tables[0].Rows)
{
byte[] files = (byte[])(dr["Files"]);
//FileStream strim = new FileStream(Server.MapPath("~/TempFile/" + dr["FileName"]), FileMode.Create);
//strim.Write(files, 0, files.Length);
//strim.Close();
//strim.Dispose();
ZipEntry zipEntry = new ZipEntry(dr["Files"].ToString());
zipOS.PutNextEntry(zipEntry);
zipOS.Write(files, 0, files.Length);
}
zipOS.Finish();
zipOS.Close();

FileInfo file = new FileInfo(Server.MapPath("1/") + strNow + ".zip");
if (file.Exists)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/zip";
Response.WriteFile(file.FullName);
Response.Flush();
file.Delete();
Response.End();
}
}


it gives me error at this line

byte[] files = (byte[])(dr["Files"]); that "Unable to cast object of type 'System.String' to type 'System.Byte[]'."

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900