![](/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.