Mkay,所以我在搞乱 DotNetZip,我想不通。
基本上我将两个拉链合并在一起,之前覆盖了后者。为此,我使用以下代码从源 zip 中删除注入 zip 中的所有元素:
foreach (ZipEntry entry in inZip)
{
if (outZip.ContainsEntry(entry.FileName))
{
Log("Removing deprecated entry {0}", entry.FileName);
outZip.RemoveEntry(entry.FileName);
}
}
outZip.Save();
然后我遍历注入 zip 中的所有元素并将它们临时保存到一个文件中,然后将它们重新添加到源 zip 中。像这样:
foreach (ZipEntry entry in inZip)
{
if (!entry.IsDirectory)
{
Log("Attempting to copy {0} to temp", entry.FileName);
string tempName = Path.Combine(tempPath, "tmp.tmp");
string oldName = entry.FileName;
byte[] buffer = new byte[65536];
Stream inStream = null;
FileStream stream = null;
try
{
Log("Reading from entry {0}", entry.FileName);
inStream = entry.OpenReader();
Log("Initializing new stream from path {0}", tempName);
stream = new FileStream(tempName, FileMode.Create, FileAccess.ReadWrite);
int size = 0;
Log("Prepping to read from stream");
while (
(size =
inStream.Read(buffer, 0, buffer.Length)) > 0)
{
Log("Reading {0} bytes from entry {1}", buffer.Length, entry.FileName);
stream.Write(buffer, 0, size);
}
Log("Closing and flushing streams");
inStream.Close();
stream.Flush();
stream.Close();
Log("Opening read stream");
inStream = new FileStream(tempName, FileMode.Open, FileAccess.Read);
Log("Adding new entry");
outZip.AddEntry(oldName, inStream);
}
catch (Exception exe)
{
throw exe;
}
它工作得很好,直到我开始点击最初删除的文件。然后我收到以下错误:
Ionic.Zlib.ZlibException: Bad state (invalid stored block lengths)
谁能帮我找出问题所在?我已将其范围缩小到 while 循环。
while (
(size =
inStream.Read(buffer, 0, buffer.Length)) > 0)
{
Log("Reading {0} bytes from entry {1}", buffer.Length, entry.FileName);
stream.Write(buffer, 0, size);
}