簡體   English   中英

如何從byte [],到MemoryStream,Unzip,然后寫入FileStream

[英]How to go from byte[], to MemoryStream, Unzip, then write to FileStream

我不確定我做錯了什么。 我在抓取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提取的文件。 我究竟做錯了什么?

寫入MemoryStream后,您沒有將位置設置回0:

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

因此,當您嘗試從中讀取時,流位置將在最后,並且您將不會讀取任何內容。 確保回放它:

memoryStream.Position = 0;

此外,您不必手動處理副本。 只需讓CopyTo方法來處理它:

extractedFile.Value.CopyTo(file);

我建議您清理代碼中對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);
}

這只是代碼少得多,避免錯誤的機會要大得多。 代碼中錯誤的首要預測因素是您編寫的代碼行數。

因為我找到了很多簡單操作的代碼,所以這是我的兩分錢。

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);
        }   
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM