同様の質問はどれも、私が探しているものではありません!
次のコードの何が問題になっていますか?
files
はファイル コンテンツのテキスト配列でfileNames
、対応するファイル名配列です。
このコードは、最後から 2 行目の Save メソッドで常に失敗しますが、ストリームが閉じられる理由がわかりません!
result = new MemoryStream();
using (ZipFile zipFile = new ZipFile())
{
for (int i = 0; i < files.Count(); i++)
{
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
Byte[] bytes = encoding.GetBytes(files[i]);
using (MemoryStream fs = new MemoryStream(bytes))
{
zipFile.AddEntry(fileNames[i], fs);
}
}
zipFile.Save(result);
}
助けてくれてありがとう - ここで必死になっています!
これは@spenderの最初のコメントに基づく私の解決策ですが、以下に投稿された彼の解決策はおそらくより良いです.
try
{
result = new MemoryStream();
List<Stream> streams = new List<Stream>();
if (files.Count > 0)
{
using (ZipFile zipFile = new ZipFile())
{
for (int i = 0; i < files.Count(); i++)
{
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
Byte[] bytes = encoding.GetBytes(files[i]);
streams.Add(new MemoryStream(bytes));
zipFile.AddEntry(fileNames[i], streams[i]);
}
zipFile.Save(result);
}
}
}
catch (Exception ex)
{
throw;
}