如何从byte [],到MemoryStream,Unzip,然后写入FileStream

问题描述 投票:1回答:3

我不确定我做错了什么。我在抓取byte[](即emailAttachment.Body)并将其传递给方法ExtractZipFile后创建的文件,将其转换为MemoryStream然后解压缩,将其作为KeyValuePair返回,然后使用FileStream写入文件。

但是,当我打开新创建的文件时,打开它们时出错。它们无法打开。

以下是同一类

using Ionic.Zip;

var extractedFiles = ExtractZipFile(emailAttachment.Body);

foreach (KeyValuePair<string, MemoryStream> extractedFile in extractedFiles)
{                               
    string FileName = extractedFile.Key;
    using (FileStream file = new FileStream(CurrentFileSystem + 
    FileName.FileFullPath(),FileMode.Create, System.IO.FileAccess.Write))
    {
        byte[] bytes = new byte[extractedFile.Value.Length];
        extractedFile.Value.Read(bytes, 0, (int) xtractedFile.Value.Length);
        file.Write(bytes,0,bytes.Length);
        extractedFile.Value.Close();
     }
}


private Dictionary<string, MemoryStream> ExtractZipFile(byte[] messagePart)
{
    Dictionary<string, MemoryStream> result = new Dictionary<string,MemoryStream>();
    MemoryStream data = new MemoryStream(messagePart);
    using (ZipFile zip = ZipFile.Read(data))
    {
        foreach (ZipEntry ent in zip)
        {
            MemoryStream memoryStream = new MemoryStream();
            ent.Extract(memoryStream);
            result.Add(ent.FileName,memoryStream);
        }   
    }
    return result;
}

有什么我想念的吗?我不想保存原始的zip文件只是从MemoryStream提取的文件。我究竟做错了什么?

c# filestream zipfile memorystream
3个回答
1
投票

写入MemoryStream后,您没有将位置设置回0:

MemoryStream memoryStream = new MemoryStream();
ent.Extract(memoryStream);
result.Add(ent.FileName,memoryStream);

因此,当您尝试从中读取时,流位置将在最后,您将不会读取任何内容。确保回放它:

memoryStream.Position = 0;

此外,您不必手动处理副本。让CopyTo方法来处理它:

extractedFile.Value.CopyTo(file);

1
投票

我建议你清理你在代码中使用MemoryStream

我同意调用memoryStream.Position = 0;将允许此代码正常工作,但在读取和写入内存流时很容易错过。

编写避免错误的代码会更好。

试试这个:

private IEnumerable<(string Path, byte[] Content)> ExtractZipFile(byte[] messagePart)
{
    using (var data = new MemoryStream(messagePart))
    {
        using (var zipFile = ZipFile.Read(data))
        {
            foreach (var zipEntry in zipFile)
            {
                using (var memoryStream = new MemoryStream())
                {
                    zipEntry.Extract(memoryStream);
                    yield return (Path: zipEntry.FileName, Content: memoryStream.ToArray());
                }
            }
        }
    }
}

然后你的调用代码看起来像这样:

foreach (var extractedFile in ExtractZipFile(emailAttachment.Body))
{
    File.WriteAllBytes(Path.Combine(CurrentFileSystem, extractedFile.Path.FileFullPath()), extractedFile.Content);
}

这只是代码少得多,避免错误的机会要大得多。代码中错误的首要预测因素是您编写的代码行数。


1
投票

因为我找到了很多简单操作的代码,所以这是我的两分钱。

using Ionic.Zip;

using (var s = new MemoryStream(emailAttachment.Body))
using (ZipFile zip = ZipFile.Read(s))
{
    foreach (ZipEntry ent in zip)
    {
        string path = Path.Combine(CurrentFileSystem, ent.FileName.FileFullPath())
        using (FileStream file = new FileStream(path, FileAccess.Write))
        {
            ent.Extract(file);
        }   
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.