我试图使用MemoryStream创建一个简单的演示文本文件的ZIP存档,如下所示:
using (var memoryStream = new MemoryStream())
using (var archive = new ZipArchive(memoryStream , ZipArchiveMode.Create))
{
var demoFile = archive.CreateEntry("foo.txt");
using (var entryStream = demoFile.Open())
using (var streamWriter = new StreamWriter(entryStream))
{
streamWriter.Write("Bar!");
}
using (var fileStream = new FileStream(@"C:\Temp\test.zip", FileMode.Create))
{
stream.CopyTo(fileStream);
}
}
如果我运行这段代码,就会创建归档文件本身,但foo.txt不会。
然而,如果我直接用文件流替换MemoryStream,存档将被正确创建:
using (var fileStream = new FileStream(@"C:\Temp\test.zip", FileMode.Create))
using (var archive = new ZipArchive(fileStream, FileMode.Create))
{
// ...
}
是否可以使用MemoryStream来创建没有FileStream的ZIP存档?
感谢ZipArchive创建无效的ZIP文件,我得到:
using (var memoryStream = new MemoryStream())
{
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
var demoFile = archive.CreateEntry("foo.txt");
using (var entryStream = demoFile.Open())
using (var streamWriter = new StreamWriter(entryStream))
{
streamWriter.Write("Bar!");
}
}
using (var fileStream = new FileStream(@"C:\Temp\test.zip", FileMode.Create))
{
memoryStream.Seek(0, SeekOrigin.Begin);
memoryStream.CopyTo(fileStream);
}
}
这表明我们需要在使用ZipArchive之前调用Dispose,正如Amir所建议的,这可能是因为它将最后的字节(如校验和)写入存档,使其完整。但是为了不关闭流,这样我们就可以重用它,你需要将true作为第三个参数传递给ZipArchive。
我来晚了,但在某些情况下,您无法访问ZipArchive的构造函数来设置leaveOpen参数,并且不希望将ZIP文件写入磁盘。在我的例子中,我在内部使用的AsiceArchive类创建了一个ZipArchive,但没有将leaveOpen设置为true。
我创建了一个流的子类,将所有调用委托给一个内部流(用ReSharper点击几下)。这个类是不可丢弃的,所以当ZipArchive被丢弃时,内部流不会发生任何变化。
public class NondisposingStreamWrapper : Stream
{
private readonly Stream _streamImplementation;
public NondisposingStreamWrapper(Stream inner) => _streamImplementation = inner;
public override void Flush() => _streamImplementation.Flush();
public override int Read(byte[] buffer, int offset, int count) => _streamImplementation.Read(buffer, offset, count);
public override long Seek(long offset, SeekOrigin origin) => _streamImplementation.Seek(offset, origin);
public override void SetLength(long value) => _streamImplementation.SetLength(value);
public override void Write(byte[] buffer, int offset, int count) => _streamImplementation.Write(buffer, offset, count);
public override bool CanRead => _streamImplementation.CanRead;
public override bool CanSeek => _streamImplementation.CanSeek;
public override bool CanWrite => _streamImplementation.CanWrite;
public override long Length => _streamImplementation.Length;
public override long Position
{
get => _streamImplementation.Position;
set => _streamImplementation.Position = value;
}
}
像这样使用它:
using var memoryStream = new MemoryStream();
var output = new NondisposingStreamWrapper(memoryStream);
using (var archive = new ZipArchive(output, ZipArchiveMode.Create))
{
// add entries to archive
}
memoryStream.Flush();
memoryStream.Position = 0;
// write to file just for testing purposes
File.WriteAllBytes("out.zip", memoryStream.ToArray());
感谢ZipArchive创建无效的ZIP文件,我得到:
using (var memoryStream = new MemoryStream())
{
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
var demoFile = archive.CreateEntry("foo.txt");
using (var entryStream = demoFile.Open())
using (var streamWriter = new StreamWriter(entryStream))
{
streamWriter.Write("Bar!");
}
}
using (var fileStream = new FileStream(@"C:\Temp\test.zip", FileMode.Create))
{
memoryStream.Seek(0, SeekOrigin.Begin);
memoryStream.CopyTo(fileStream);
}
}
这表明我们需要在使用ZipArchive之前调用Dispose,正如Amir所建议的,这可能是因为它将最后的字节(如校验和)写入存档,使其完整。但是为了不关闭流,这样我们就可以重用它,你需要将true作为第三个参数传递给ZipArchive。