I am trying to delete a zip
file in DotNetZip
as it is like transaction
(either do it full or not at all).
Currently, I have incremented
my count
twice, on purpose to break the app and see if it saves the zip
.
Well it does, it saves the zip
with 2 files instead of 3 and as I said I want it to behave like a 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++;
}
}
}
I have found this article on Stackoverflow
.
DotNetZip how to delete a file after extracting
It said read the stream
of the zip
and then Dispose
of it. I have tried that but the zip
file still exists.
No errors, just does not delete the file.