无法删除由 SharpZipLib (FastZip) 创建的 Zip 文件

发布于 2024-07-10 20:38:02 字数 728 浏览 5 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

别念他 2024-07-17 20:38:02

看起来这可能是 SharpZipLib 的一个错误。 FastZip.CreateZip 方法有三个重载,其中两个重载使用 File.Create,例如:

CreateZip(File.Create(zipFileName), // ...

它似乎不会关闭流,因此您最好使用:

string zipFileName = System.IO.Path.Combine(filesPath, this.zipName);
using (Stream stream = File.Create(zipFileName))
{
    fZip.CreateZip(stream, filesPath, false, this.zipFilter, null);
    stream.Close();
}

从技术上讲,您不需要在 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:

CreateZip(File.Create(zipFileName), // ...

It seems it does not close the stream, so you may be better off using:

string zipFileName = System.IO.Path.Combine(filesPath, this.zipName);
using (Stream stream = File.Create(zipFileName))
{
    fZip.CreateZip(stream, filesPath, false, this.zipFilter, null);
    stream.Close();
}

Technically you shouldn't need to call Close in the using block, but I like to be explicit about it.

哎呦我呸! 2024-07-17 20:38:02

我实际上刚刚自己解决了这个问题。 事实证明它不是 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.

过潦 2024-07-17 20:38:02

也许防病毒软件正忙于检查文件? 如果没有,那么获取一个可以告诉您哪些程序打开了文件的程序。

您可以查看:

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:

芸娘子的小脾气 2024-07-17 20:38:02

我不熟悉 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.

自由范儿 2024-07-17 20:38:02

确保在完成后关闭 SharpZipLib ,您可以通过将其构造包装在“using”语句中来完成此操作...

using(Stream s = new SharpZipLibStream())
{
   /*...do stuff, zip stuff.../
}

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...

using(Stream s = new SharpZipLibStream())
{
   /*...do stuff, zip stuff.../
}
戏舞 2024-07-17 20:38:02

我知道这是一个非常老的问题,但我确实遇到过它,并且能够通过在 Zip 操作完成后立即运行垃圾收集来解决它:

GC.Collect()

垃圾收集可能不会立即发生,因此也可能需要延迟。 我将删除放入重试循环中,如果删除失败则等待一两秒。

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:

GC.Collect()

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文