如何在ASP.NET C中将多个文件压缩为单个zip文件# [英] How to zip multiple files to a single zip file in ASP.NET C#
问题描述
大家好,
我需要你的建议,如何创建压缩多个文件的单个zip文件,然后发送附带创建文件的电子邮件。
我使用DotNetZip库创建zip文件。
我可以发送多个压缩文件电子邮件,但我需要一个zip文件来存储整个文件。请看下面的代码,
提前致谢!!
我试过的:
foreach(附件中的KeyValuePair< string,byte [] => doc)
{
附件附件;
MemoryStream memoryStream = new MemoryStream();
使用(ZipFile zip = new ZipFile())
{
zip.AddEntry(doc.Key +。xls,doc.Value);
zip.Save(memoryStream);
memoryStream.Position = 0;
attachment = new Attachment(memoryStream,new ContentType(application / zip)){Name = doc.Key +。zip};
}
message.Attachments.Add(附件);
}
message.To.Add(toAddress);
消息。 Body = mailbody;
message.IsBodyHtml = true;
SmtpClient smtpClient = new SmtpClient(smtp.XXXXXXXX.net);
smtpClient .Send(message);
Hi All,
I need your advice on how to create a single zip file which compress multiple files and then send an email with the created file as attached.
I am using DotNetZip library for creating zip files.
I can able send multiple zipped files through email, but I need one zip file for the entire files. Please see the code below,
Thanks in advance!!
What I have tried:
foreach (KeyValuePair<string, byte[]=""> doc in attachmentColl)
{
Attachment attachment;
MemoryStream memoryStream = new MemoryStream();
using (ZipFile zip = new ZipFile())
{
zip.AddEntry(doc.Key + ".xls", doc.Value);
zip.Save(memoryStream);
memoryStream.Position = 0;
attachment = new Attachment(memoryStream, new ContentType("application/zip")) { Name = doc.Key + ".zip" };
}
message.Attachments.Add(attachment);
}
message.To.Add(toAddress);
message.Body = mailbody;
message.IsBodyHtml = true;
SmtpClient smtpClient = new SmtpClient("smtp.XXXXXXXX.net");
smtpClient.Send(message);
推荐答案
已发布的代码将生成一个附带多个zip文件的电子邮件,但我需要压缩所有文件的单个zip文件。
Posted code will generate an email with multiple zip files as attached but, I need single zip file which compress all the files.
所以......你需要我们告诉你如何将zip文件创建到外面一个循环,并填补它insid e?
这是第三周第一个学生在他的第一个计算机课程应该可以在几分钟内独立完成的事情...
So...you need us to tell you how to move the zip file creation outside a loop, and fill it inside?
That's the kind of thing a third week student on his first computer course should be able to work out on his own in a couple of minutes...
using (ZipFile zip = new ZipFile())
{
foreach (KeyValuePair<string, byte[]> doc in attachmentColl)
{
zip.AddEntry(doc.Key + ".xls", doc.Value);
}
zip.Save(memoryStream);
MemoryStream memoryStream = new MemoryStream();
memoryStream.Position = 0;
Attachment attachment = new Attachment(memoryStream, new ContentType("application/zip")) { Name = doc.Key + ".zip" };
message.Attachments.Add(attachment);
message.To.Add(toAddress);
message.Body = mailbody;
message.IsBodyHtml = true;
SmtpClient smtpClient = new SmtpClient("smtp.XXXXXXXX.net");
smtpClient.Send(message);
}
这篇关于如何在ASP.NET C中将多个文件压缩为单个zip文件#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!