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);
}