Click here to Skip to main content
15,559,491 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here's what I have in the button click event.

C#
SqlConnection con = new SqlConnection(main.ConnString);
            SqlCommand cmd1 = new SqlCommand();

            cmd1.CommandText = selectstatement;

            cmd1.Connection = con;

            con.Open();

            SqlDataReader r1 = cmd1.ExecuteReader();
            MemoryStream mem = new MemoryStream();
            ZipArchive zip = new ZipArchive(mem, ZipArchiveMode.Update, false);

            foreach (var item in r1)
            {
                //MemoryStream mem2 = new MemoryStream();
                //mem2.CopyTo(zip.CreateEntry(r1["filename"].ToString().Trim()).Open());

                var zipEntry = zip.CreateEntry((string)r1["filename"]);

                //Get the stream of the attachment
                using (var originalFileStream = new MemoryStream((byte[])r1["stored_content"]))
                {
                    using (var zipEntryStream = zipEntry.Open())
                    {
                        //Copy the attachment stream to the zip entry stream
                        originalFileStream.CopyTo(zipEntryStream);
                    }
                }

            }

            sendOutZIP(mem.ToArray(), "FileName.zip");


Here's what the sendOutZIP looks like

C#
private void sendOutZIP(byte[] zippedFiles, string filename)
        {
            Response.Clear();
            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentType = "application/x-compressed";
            Response.Charset = string.Empty;
            Response.Cache.SetCacheability(HttpCacheability.Public);
            Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
            Response.BinaryWrite(zippedFiles);
            Response.OutputStream.Flush();
            Response.OutputStream.Close();
            Response.End();
        }


What I have tried:

The database has contents and when I download it one by one it works.. But when i execute this code it downloads a zip file yea. But it says the zip file is corrupt. What did I do wrong here>
Posted
Updated 13-Apr-21 20:21pm
v2
Comments
Gerry Schmitz 14-Apr-21 0:32am    
Start by comparing lengths.

1 solution

just changed the above code like as follows and it worked!

C#
SqlConnection con = new SqlConnection(main.ConnString);
            SqlCommand cmd1 = new SqlCommand();

            cmd1.CommandText = selectstatement;

            cmd1.Connection = con;

            con.Open();

            SqlDataReader r1 = cmd1.ExecuteReader();
            using (var compressedFileStream = new MemoryStream())
            {
                //Create an archive and store the stream in memory.
                using (var zipArchive = new ZipArchive(compressedFileStream, ZipArchiveMode.Create, false))
                {
                    foreach (var caseAttachmentModel in r1)
                    {
                        //Create a zip entry for each attachment
                        var zipEntry = zipArchive.CreateEntry((string)r1["filename"]);

                        //Get the stream of the attachment
                        using (var originalFileStream = new MemoryStream((byte[])r1["stored_content"]))
                        using (var zipEntryStream = zipEntry.Open())
                        {
                            //Copy the attachment stream to the zip entry stream
                            originalFileStream.CopyTo(zipEntryStream);
                        }
                    }
                }
                sendOutZIP(compressedFileStream.ToArray(), "FileName.zip");
            }
 
Share this answer
 

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