谁能告诉我为什么下面的代码不起作用?我正在使用SharpZipLib API用于Zip流,这是目前从他们的网站上DL'ed的最新版本。我试图使用这个逻辑来合并一个zip文件的内容到另一个,而不必在磁盘上执行IO,因为预期的zip文件可能包含保留的windows文件名。我已经尝试过使用多个不同的源和目标zip文件(包含保留名称和不包含这些名称的文件)。代码不会抛出任何异常,并且如果在每次写入操作之前检查缓冲区,则可以看到它包含真实数据,但在整个操作完成后,目标zip文件的大小没有改变,您可以探索它确认没有新文件(代码应该添加的文件)实际上已添加到目标文件。 :(将zip条目提取到另一个Zip文件
public static void CopyToZip(string inArchive, string outArchive)
{
ZipOutputStream outStream = null;
ZipInputStream inStream = null;
try
{
outStream = new ZipOutputStream(File.OpenWrite(outArchive));
outStream.IsStreamOwner = false;
inStream = new ZipInputStream(File.OpenRead(inArchive));
ZipEntry currentEntry = inStream.GetNextEntry();
while (currentEntry != null)
{
byte[] buffer = new byte[1024];
ZipEntry newEntry = new ZipEntry(currentEntry.Name);
newEntry.Size = currentEntry.Size;
newEntry.DateTime = currentEntry.DateTime;
outStream.PutNextEntry(newEntry);
int size = 0;
while ((size = inStream.Read(buffer, 0, buffer.Length)) > 0)
{
outStream.Write(buffer, 0, size);
}
outStream.CloseEntry();
currentEntry = inStream.GetNextEntry();
}
outStream.IsStreamOwner = true;
}
catch (Exception e)
{
throw e;
}
finally
{
try { outStream.Close(); }
catch (Exception ignore) { }
try { inStream.Close(); }
catch (Exception ignore) { }
}
}
我遵循这些示例来创建我在此处发布的代码。此时不管它是否覆盖,上面的代码根本不会改变目标zip文件的状态。如果它是覆盖,至少我会有东西来调试......我不需要使用memeory流。我的意思是“不使用磁盘”是因为我无法将一个zip文件的内容提取到硬盘上,然后将其重新打包到另一个zip文件中。由于windows文件的命名规则,它必须从一个zip文件移动到下一个文件,而不需要在HD上解压缩。 – 2012-08-07 21:38:59
我使用了File.OpwnWrite()的新FileStream(outArchive,FileMode.Open)。它根本没有改变结果。 – 2012-08-07 21:45:43