无法删除由 SharpZipLib (FastZip) 创建的 Zip 文件
我正在使用 SharpZipLib 中的 FastZip 类创建一个 zip 文件,一旦我关闭程序后,我就无法删除该文件,因为:
“无法删除 zip.zip:它正在被其他人或程序使用。关闭任何程序可能正在使用该文件,然后重试。”
生成文件的代码很简单:
fZip.CreateEmptyDirectories = true;
fZip.CreateZip(filesPath + "\\" + this.zipName, filesPath, false, this.zipFilter);
我尝试使用:
using (FastZip fZip = new FastZip())
{
try
{
fZip.CreateEmptyDirectories = true;
fZip.CreateZip(filesPath + "\\" + this.zipName, filesPath, false, this.zipFilter);
}
catch (Exception)
{
}
}
但它不会转换为 iDisposable
I'm creating a zip file using the class FastZip from SharpZipLib and once I after I close the program, I cannot delete the file because:
"Cannot delete zip.zip: It is being used by another person or program. Close any programs that might be using the file and try again."
The code that is generating the file is simply this:
fZip.CreateEmptyDirectories = true;
fZip.CreateZip(filesPath + "\\" + this.zipName, filesPath, false, this.zipFilter);
I tried using:
using (FastZip fZip = new FastZip())
{
try
{
fZip.CreateEmptyDirectories = true;
fZip.CreateZip(filesPath + "\\" + this.zipName, filesPath, false, this.zipFilter);
}
catch (Exception)
{
}
}
But it doesn't convert to iDisposable
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
看起来这可能是 SharpZipLib 的一个错误。 FastZip.CreateZip 方法有三个重载,其中两个重载使用 File.Create,例如:
它似乎不会关闭流,因此您最好使用:
从技术上讲,您不需要在 using 块中调用 Close ,但我喜欢明确说明这一点。
It looks like it may be a bug with SharpZipLib. The FastZip.CreateZip method has three overloads and two of them create use File.Create such as:
It seems it does not close the stream, so you may be better off using:
Technically you shouldn't need to call Close in the using block, but I like to be explicit about it.
我实际上刚刚自己解决了这个问题。 事实证明它不是 SharpZipLib/FastZip。 我通过 System.Net.Mail 发送电子邮件,并将 FastZipped 文件作为附件附加到该电子邮件中。 当我尝试删除附加文件时,遇到了您收到的错误。 事实证明,我所要做的就是在发送电子邮件后调用 MailMessage.Dispose() 。
查看代码中可能锁定文件的任何其他项目; 如果可能的话,尝试 dispose() 或 close() 这些项目。
I actually just solved this problem myself. Turns out that it wasn't SharpZipLib/FastZip. I was sending an email via System.Net.Mail and had attached the FastZipped file as an attachment to that email. When I tried to delete the attached file, I ran into the error which you received. Turns out that all I had to do was call MailMessage.Dispose() after sending the email.
Take a look at any other items within your code which might be holding a lock on the file; try to dispose() or close() those items if possible.
也许防病毒软件正忙于检查文件? 如果没有,那么获取一个可以告诉您哪些程序打开了文件的程序。
您可以查看:
Perhaps antivirus is busy checking the file? If not, then get a program that can tell you which programs have files open.
You can look at:
我不熟悉 FastZip API(所以这只是一个赌注),但是您是否需要使用 fZip.Close(); 之类的方法关闭存档?
我本以为关闭程序会做同样的事情,但只是猜测。
I'm not familier with the FastZip API (so this is just a punt) but do you need to close the archive with something like fZip.Close();
I would have thought that closing the program would have done the same thing but just a guess.
确保在完成后关闭 SharpZipLib 流,您可以通过将其构造包装在“using”语句中来完成此操作...
Make sure you CLOSE the SharpZipLib stream after you're finished with it, you can do this by wrapping the construction of it inside a "using" statement...
我知道这是一个非常老的问题,但我确实遇到过它,并且能够通过在 Zip 操作完成后立即运行垃圾收集来解决它:
垃圾收集可能不会立即发生,因此也可能需要延迟。 我将删除放入重试循环中,如果删除失败则等待一两秒。
I know this is a really old issue, but I did encounter it and was able to resolve it by running garbage collection immediately after the Zip operation completes:
The garbage collection may not happen immediately, so a delay may also be required. I put my delete in a retry loop, waiting a second or two if the delete fails.