我已經編寫了將壓縮文件的編碼字符串存儲到temp路徑中的代碼,現在我想將編碼的zipfile字符串存儲到內存流而不是臨時路徑。有人可以幫助我如何讀取流並將其作爲字符串傳遞給ZipFile類...我使用DOTNETZIP庫來解壓密碼抗原文件。 請看下面我的代碼。將壓縮文件解壓縮到內存流 - C#
string tempPath = Path.GetTempPath();
foreach (ActivityMimeAttachment a in attachments.Entities)
{
if (a.FileName.EndsWith(".zip", StringComparison.OrdinalIgnoreCase))
{
string strcontent = a.Body;
byte[] filecontent = Convert.FromBase64String(strcontent); // unpack the base-64 to a blob
File.WriteAllBytes(tempPath + a.FileName, filecontent); // Working code creates a zip file
string attachmentfile = tempPath + a.FileName;
using (ZipFile zip = new ZipFile(attachmentfile))
{
foreach (ZipEntry entry in zip.Entries)
{
if ((entry.FileName.EndsWith(".xml", StringComparison.OrdinalIgnoreCase)) ||
(entry.FileName.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase)))
{
entry.ExtractWithPassword(tempPath, "password");
FileStream inFile;
byte[] binaryData;
string file = tempPath + entry.FileName;
inFile = new FileStream(file, FileMode.Open, FileAccess.Read);
binaryData = new Byte[inFile.Length];
long bytesRead = inFile.Read(binaryData, 0,
(int)inFile.Length);
inFile.Close();
你不能只是將你的字節數組轉換爲內存流嗎? Stream stream = new MemoryStream(filecontent); – k3davis
感謝您的回覆。你是對的。我可以轉換,但我的問題是如何從內存流中讀取它並將其作爲字符串傳遞給ZIP文件。 – Kittu