这是我的代码。
using (FileStream zipToOpen = new FileStream(@"D:\test\1.txt", FileMode.Open))
{
using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
{
ZipArchiveEntry readmeEntry = archive.CreateEntry("Readme.txt");
using (StreamWriter writer = new StreamWriter(readmeEntry.Open()))
{
writer.WriteLine("Information about this package.");
writer.WriteLine("========================");
}
}
// I reused this stream again and below code just a sample.
// It would get exception
using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
{
//System.ArgumentException: 'Update mode requires a stream with read, write, and seek capabilities.'
}
}
是不是由'stream无法在C#中重新引发此异常?如果答案是肯定的,那么任何办公文件都可以让我参考吗?
谢谢,如果您能提供一些帮助,我们将不胜感激。
ZipArchive
承担对其所处理的Stream
的生命周期控制,并在处理it时进行处理。为避免这种情况,请在构造函数重载中将leaveOpen
设置为true
。但是:在用法之间您可能还需要rewind流,因此在两个zipToOpen.Position = 0;
块之间需要using
。有趣的是,似乎[[不太可能 1.txt
是一个zip文件,但是...我想可能是!]]