[英]How do I remove a zip file in Dotnetzip
我正在嘗試刪除DotNetZip
中的zip
文件,因為它就像transaction
一樣(要么完整,要么根本不)。
目前,我已經將count
incremented
了兩次,目的是為了打破應用程序並查看它是否保存了zip
。
確實如此,它使用 2 個文件而不是 3 個文件保存zip
,正如我所說,我希望它表現得像一個transaction
。
using (ZipFile zip = new ZipFile(outputdirectory))
{
var count = 0;
// string invoiceNumber = documents[count].GetElementsByTagName("InvoiceNumber")[count].InnerText + ".xml";
if (documents.Count == 1)
{
string invoiceNumber = documents[0].GetElementsByTagName("InvoiceNumber")[0].InnerText + ".xml";
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.GetEncoding("UTF-8");
SaveFiles(documents[0], invoiceNumber, settings);
resultValue = InvoiceResult.Success;
}
else
{
foreach (var document in documents)
{
try
{
string invoiceNumber = documents[count].GetElementsByTagName("InvoiceNumber")[0].InnerText + ".xml";
count++;
using (MemoryStream memoryStream = new MemoryStream())
{
document.Save(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);
zip.AddEntry($"{invoiceNumber}.xml", memoryStream);
zip.Save();
}
}
catch (Exception)
{
var wrongZip = ZipFile.Read(outputdirectory);
wrongZip.Dispose();
resultValue = InvoiceResult.CannotCreateZipFile;
// throw new IOException($"Zip file in path {outputdirectory} has already been created. Please delete file if you want to make a new one.");
}
count++;
}
}
}
我在Stackoverflow
上找到了這篇文章。
它說讀取stream
的zip
然后將其Dispose
。 我已經嘗試過了,但是zip
文件仍然存在。
沒有錯誤,只是不刪除文件。
該代碼中有很多混亂。 計數器和foreach
的組合,循環重新保存壓縮文件,不必要地重新讀取已經打開的壓縮文件......
由於您確實說過保存過程中的任何錯誤都會導致完全中止,因此將try-catch
放在ZipFile
的using
塊周圍會更有意義。 畢竟, using
塊的功能之一是清理 object,即使發生異常也是如此。
至於循環重新保存,就個人而言,我只是制作一個MemoryStream
對象數組,並在每次循環迭代中填寫一個,將其鏈接到ZipFile
作為文件,並在整個循環之后只保存一次ZipFile
. 然后,您可以安全地從數組中MemoryStream
對象。 這樣,它們在您保存ZipFile
時同時存在。 如果您只在ZipFile
的using
塊之后處理它們,那么您 100% 確定ZipFile
object 不再需要它們。
如果您這樣做,則在發生錯誤時不會寫入任何內容,因為它永遠不會到達zip.Save()
行。 所以也不需要刪除任何東西。
try
{
Int32 docCount = documents.Count;
MemoryStream[] streams = null;
using (ZipFile zip = new ZipFile(outputdirectory))
{
if (docCount == 1)
{
string invoiceNumber = documents[0].GetElementsByTagName("InvoiceNumber")[0].InnerText + ".xml";
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.GetEncoding("UTF-8");
SaveFiles(documents[0], invoiceNumber, settings);
resultValue = InvoiceResult.Success;
}
else
{
streams = new MemoryStream[docCount]
// Since we need an index for both the documents and streams array,
// use 'for' and not 'foreach'
for (Int32 i = 0; i < docCount; ++i)
{
var doc = documents[i];
string invoiceNumber = doc.GetElementsByTagName("InvoiceNumber")[0].InnerText + ".xml";
MemoryStream str = new MemoryStream();
// Store stream in array so you can dispose it later
streams[i] = str;
doc.Save(str);
str.Seek(0, SeekOrigin.Begin);
zip.AddEntry($"{invoiceNumber}.xml", str);
}
zip.Save();
}
}
if (streams != null)
for (Int32 i = 0; i < docCount; ++i)
if (streams[i] != null)
streams[i].Dispose();
}
catch (Exception)
{
resultValue = InvoiceResult.CannotCreateZipFile;
}
不過,這似乎確實缺少一些細節。 1-file 邏輯似乎根本不使用ZipFile
,這意味着ZipFile
的using
塊在技術上可以放在else
中。 此外,多文件邏輯從不設置resultValue
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.