0

我使用 DotNetZip。

我收到错误

无法对不可搜索的输入流使用 PKZIP 加密

该怎么办

var outputStream = new MemoryStream();
using (var zip = new ZipFile())
{
    zip.Password = "123456!";
    var outputStreamFile = new MemoryStream();
    var userId = m_userRepository.GetuserByLogin(this.User.Identity.Name).UserId;

    using (var streamWriter = new StreamWriter(outputStreamFile))
    {
        streamWriter.WriteLine(m_kamikaze2Repository.GetGameById(gameId, userId).Result);

    }
    zip.AddEntry("result_" + gameId, outputStreamFile);
    zip.Save(outputStream);//error

}
4

2 回答 2

5

问题是outpuStreamFile当您将其添加到ZipFile. StreamWriter.Dispose将处理流,因此只需移动using语句以确保outputStreamFile在调用时可用zip.Save

此外,在添加之前outputStreamFileZipFile您需要倒带它。

using (var outputStreamFile = new MemoryStream()) {
  var userId = m_userRepository.GetuserByLogin(this.User.Identity.Name).UserId;
  var streamWriter = new StreamWriter(outputStreamFile);
  streamWriter.WriteLine(m_kamikaze2Repository.GetGameById(gameId, userId).Result);
  outputStreamFile.Seek(0, SeekOrigin.Begin);
  zip.AddEntry("result_" + gameId, outputStreamFile);     
  zip.Save(outputStream);
}
于 2012-05-15T09:55:15.237 回答
0

试试这样:

using (var outputStream = new MemoryStream())
using (var zip = new ZipFile())
{
    zip.Password = "123456!";

    zip.AddEntry("result_" + gameId, "Some content");
    zip.Save(outputStream);

    byte[] zipFile = outputStream.ToArray();
    // TODO: do something with the zip
}
于 2012-05-15T10:18:40.603 回答