![](/img/trans.png)
[英]ZIP file created with SharpZipLib cannot be opened on Mac OS X
[英]Cannot Delete Zip file Created by SharpZipLib (FastZip)
我正在使用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
看起来SharpPipLib可能是一个错误。 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,但我希望对此进行明确说明。
我实际上只是自己解决了这个问题。 原来不是SharpZipLib / FastZip。 我正在通过System.Net.Mail发送电子邮件,并且已将FastZipped文件作为附件添加到该电子邮件中。 当我尝试删除附件时,遇到了您收到的错误。 事实证明,我要做的就是在发送电子邮件后调用MailMessage.Dispose()。
看一下代码中可能会锁定文件的其他任何项目; 如果可能,请尝试对这些项目进行处理()或关闭()。
我对FastZip API并不熟悉(所以这只是一个小问题),但是您是否需要使用fZip.Close()之类的文件关闭存档?
我本以为关闭程序会做同样的事情,只是一个猜测。
确保完成后关闭SharpZipLib 流 ,可以通过将其构造包装在“使用”语句中来完成此操作...
using(Stream s = new SharpZipLibStream())
{
/*...do stuff, zip stuff.../
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.