3

我正在尝试压缩 XML 树并将其用作电子邮件附件。发送带有附件的电子邮件成功,但创建的 zip 文件总是损坏 - 它不是有效的 zip 文件,但包含二进制数据。

问题重现如下,具体见BuildAttachment()

static void Main(string[] args)
{
    try
    {
        var report = new XElement("Report",
            new XElement("Product",
                new XElement("ID", "10000001"),
                new XElement("Name", "abcdefghijklm"),
                new XElement("Group", "nopqrstuvwxyz")
            )
        );
        var mailMessage = BuildMessage(report);
        EmailMessage(mailMessage);
        Thread.Sleep(10000);
    }
    catch (Exception e) { Console.WriteLine(e.Message); }
}
static MailMessage BuildMessage(XElement report)
{
    string from = "email1@address.com";
    string to = "email2@address.com";
    var message = new MailMessage(from, to, "Subject text", "Body text");

    var attachment = BuildAttachment(report);
    message.Attachments.Add(attachment);

    return message;
}
static Attachment BuildAttachment(XElement report)
{
    var inStream = new MemoryStream();
    report.Save(inStream);
    inStream.Position = 0;

    var outStream = new MemoryStream();
    var compress = new DeflateStream(outStream, CompressionMode.Compress);
    inStream.CopyTo(compress);

    outStream.Position = 0;
    return new Attachment(outStream, "report.zip", "application/zip");
}
static void EmailMessage(MailMessage message)
{
    var smtpClient = new SmtpClient("127.0.0.1");
    smtpClient.SendCompleted += SendCompletedCallback;
    smtpClient.SendAsync(message, null);
}
static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
    if (e.Error != null)
        Console.WriteLine(e.Error.ToString());
}

将问题放在上下文中:它是 Windows 服务应用程序的一部分,所以我不想在磁盘上创建文件,并且电子邮件还包含 xslt 转换的 xml 树的备用视图,所以我不想要完全不同解决方案。

任何建议为什么 zip 文件已损坏?

4

4 回答 4

13

我知道这是一个老问题,但是当我搜索相同的问题时它出现了。

这是我使用System.IO.Compression.ZipArchive 添加压缩(zip)附件的解决方案(需要 .NET 4.5 或更高版本) [基于 acraig5075 的答案]:

byte[] report = GetSomeReportAsByteArray();
string fileName = "file.txt";

using (MemoryStream memoryStream = new MemoryStream())
{
    using (ZipArchive zipArchive = new ZipArchive(memoryStream, ZipArchiveMode.Update))
    {
        ZipArchiveEntry zipArchiveEntry = zipArchive.CreateEntry(fileName);
        using (StreamWriter streamWriter = new StreamWriter(zipArchiveEntry.Open()))
        {
            streamWriter.Write(Encoding.Default.GetString(report));
        }
    }

    MemoryStream attachmentStream = new MemoryStream(memoryStream.ToArray());

    Attachment attachment = new Attachment(attachmentStream, fileName + ".zip", MediaTypeNames.Application.Zip);
    mail.Attachments.Add(attachment);
}
于 2014-01-03T13:46:12.517 回答
4

为了将来参考,GZipStream在接受的答案中建议的替代方法是通过如下更改上述代码来生成 .gz 存档。

static Attachment BuildAttachment(XElement report)
{
    var inStream = new MemoryStream();
    report.Save(inStream);
    inStream.Position = 0;

    var outStream = new MemoryStream();
    using (var compress = new GZipStream(outStream, CompressionMode.Compress))
    {
        inStream.CopyTo(compress);
        compress.Close();
    }

    var ms = new MemoryStream(outStream.ToArray());
    Attachment attachment = new Attachment(ms, "report.xml.gz", "application/gzip");
    return attachment;
}

using块和ToArray()调用是必需的。

于 2012-07-24T07:48:35.847 回答
4

您没有创建有效的 zip 文件,您只是创建了一个压缩流并将其写入具有*.zip扩展名的文件。您应该使用 .NET zip 库而不是DeflateStream.

您可以使用DotNetZip Library 之类的东西:

DotNetZip 是一个易于使用、快速、免费的类库和工具集,用于处理 zip 文件或文件夹。压缩和解压缩很容易:使用 DotNetZip,用 VB、C#(任何 .NET 语言)编写的 .NET 应用程序可以轻松创建、读取、提取或更新 zip 文件。对于 Mono 或 MS .NET。

如果您有限制并且无法使用外部库,您可以尝试使用GZipStream并添加带有扩展名的附件,该扩展*.gz名将由常见的压缩工具支持。

作为未来参考的有用信息,.NET 4.5最终ZipArchive将通过该类引入对 zip 归档的本机支持。

于 2012-07-23T14:17:02.810 回答
0

感谢“Neils.R”的回答,我创建了一个静态 C# 函数来管理附件并在大于 1Mb 时将它们压缩。希望这对任何人都有帮助。

public static Attachment CreateAttachment(string fileNameAndPath, bool zipIfTooLarge = true, int bytes = 1 << 20)
{
    if (!zipIfTooLarge)
    {
        return new Attachment(fileNameAndPath);
    }

    var fileInfo = new FileInfo(fileNameAndPath);
    // Less than 1Mb just attach as is.
    if (fileInfo.Length < bytes)
    {
        var attachment = new Attachment(fileNameAndPath);

        return attachment;
    }

    byte[] fileBytes = File.ReadAllBytes(fileNameAndPath);

    using (var memoryStream = new MemoryStream())
    {
        string fileName = Path.GetFileName(fileNameAndPath);

        using (var zipArchive = new ZipArchive(memoryStream, ZipArchiveMode.Create))
        {
            ZipArchiveEntry zipArchiveEntry = zipArchive.CreateEntry(fileName, CompressionLevel.Optimal);

            using (var streamWriter = new StreamWriter(zipArchiveEntry.Open()))
            {
                streamWriter.Write(Encoding.Default.GetString(fileBytes));
            }
        }

        var attachmentStream = new MemoryStream(memoryStream.ToArray());
        string zipname = $"{Path.GetFileNameWithoutExtension(fileName)}.zip";
        var attachment = new Attachment(attachmentStream, zipname, MediaTypeNames.Application.Zip);

        return attachment;
    }
}
于 2017-09-06T09:31:41.007 回答