private void DownloadAllZipped(int message_id, HttpContext context) { var mail_box_manager = new MailBoxManager(ConfigurationManager.ConnectionStrings["mail"], 0); var attachments = mail_box_manager.GetMessageAttachments(TenantId, Username, message_id); if (attachments.Any()) { using (var zip = new ZipFile()) { zip.CompressionLevel = CompressionLevel.Level3; zip.AlternateEncodingUsage = ZipOption.AsNecessary; zip.AlternateEncoding = Encoding.GetEncoding(Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage); foreach (var attachment in attachments) { using (var file = mail_box_manager.GetAttachmentStream(attachment)) { var filename = file.FileName; if (zip.ContainsEntry(filename)) { var counter = 1; var temp_name = filename; while (zip.ContainsEntry(temp_name)) { temp_name = filename; var suffix = " (" + counter + ")"; temp_name = 0 < temp_name.IndexOf('.') ? temp_name.Insert(temp_name.LastIndexOf('.'), suffix) : temp_name + suffix; counter++; } filename = temp_name; } zip.AddEntry(filename, file.FileStream.GetCorrectBuffer()); } } context.Response.AddHeader("Content-Disposition", ContentDispositionUtil.GetHeaderValue(ARCHIVE_NAME)); zip.Save(context.Response.OutputStream); } } }