3

I use the SharpZipLib for zipping multiple files and download them in my asp.net c# web app. After the download I can't open the zip archive. Windows error message is:

Windows cannot open the folder. The Compressed (zipped) Folder 'filename.zip' is invalid.

This is my code

        if (Session["lstFilesToZip"] != null)
        {
            List<string> filesToZip = (List<string>)Session["lstFilesToZip"];

            if (filesToZip.Count > 0)
            {    
                Response.AddHeader("Content-Disposition", "attachment; filename=fileName.zip");
                Response.ContentType = "application/zip";

                using (var zipStream = new ZipOutputStream(Response.OutputStream))
                {
                    foreach (string filePath in filesToZip)
                    {
                        byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);

                        var fileEntry = new ZipEntry(Path.GetFileName(filePath))
                        {
                            Size = fileBytes.Length
                        };

                        zipStream.PutNextEntry(fileEntry);
                        zipStream.Write(fileBytes, 0, fileBytes.Length);
                    }

                    zipStream.Flush();                        
                    zipStream.Close();                        
                }
            }
        }

I already tried adding

zipStream.Dispose();
zipStream.Finish();

without success.

Maybe someone can help. I can't find the problem.

raven_977
  • 475
  • 2
  • 8
  • 25

0 Answers0