C# 如何删除Dotnetzip中的zip文件
我正在尝试删除C# 如何删除Dotnetzip中的zip文件,c#,memorystream,dotnetzip,C#,Memorystream,Dotnetzip,我正在尝试删除DotNetZip中的zip文件,就像transaction(要么完全删除,要么根本不删除) 目前,我已经将我的计数增加了两次,目的是破坏应用程序,看看它是否保存邮政编码 确实如此,它用2个文件而不是3个文件保存了zip,正如我所说的,我希望它像事务一样运行 using (ZipFile zip = new ZipFile(outputdirectory)) { var count = 0; // string i
DotNetZip
中的zip
文件,就像transaction
(要么完全删除,要么根本不删除)
目前,我已经将我的计数增加了两次,目的是破坏应用程序,看看它是否保存邮政编码
确实如此,它用2个文件而不是3个文件保存了zip
,正如我所说的,我希望它像事务一样运行
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
上找到了这篇文章
它说读取zip的流
,然后Dispose
。我已经试过了,但是zip
文件仍然存在
没有错误,只是不删除文件。代码中有很多混乱;计数器和每个
的组合,zipfile的循环重新保存,已经打开的zipfile的不必要重新读取
因为您确实说过保存过程中的任何错误都会导致完全中止,所以使用
块将try-catch
放在ZipFile
的周围更有意义。使用
块的的功能之一就是清理对象,即使在出现异常的情况下也是如此
至于循环的重新保存,我个人只需创建一个MemoryStream
对象数组,在每次循环迭代中,填写一个,将其链接到ZipFile
作为文件,并在整个循环后只保存一次ZipFile
。然后,您可以安全地从阵列中处置MemoryStream
对象。这样,在保存ZipFile
时,它们都同时存在。如果您仅在使用
块的ZipFile
后处理它们,则您可以100%确定ZipFile
对象不再需要它们
如果这样做,在发生错误时将不会写入任何内容,因为它永远不会到达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;
}
试试看
{
Int32 docCount=documents.Count;
MemoryStream[]streams=null;
使用(ZipFile zip=new ZipFile(outputdirectory))
{
如果(docCount==1)
{
字符串invoiceNumber=documents[0]。GetElementsByTagName(“invoiceNumber”)[0]。InnerText+“.xml”;
XmlWriterSettings=新的XmlWriterSettings();
settings.Encoding=Encoding.GetEncoding(“UTF-8”);
保存文件(文档[0]、发票编号、设置);
resultValue=InvoiceResult.Success;
}
其他的
{
streams=新内存流[docCount]
//因为我们需要文档和流数组的索引,
//使用“for”而不是“foreach”
对于(Int32 i=0;i
不过,这似乎确实缺少一些细节。1-file逻辑似乎根本没有使用ZipFile
,这意味着ZipFile
的using
块从技术上讲可以放在else
中。此外,多文件逻辑从不设置resultValue
只使用System.IO.file.Delete(outputdirectory)可以吗;我的意思是,我也必须读取并处理该文件吗?只需使用System.IO.file.Delete
库在DotNetZip
库(未测试)中处理后删除zip即可。这并不是说。。。它只是说zipfile对象需要处理。只需使用
块将try-catch块放在块周围。你说它无论如何都需要在失败时完全中止,然后该对象已经被释放,你可以随时安全地删除它。顺便说一下。。。在添加文件之间保存zip文件有什么意义吗?最后只保存一次不是更简单吗?那你就不应该有不完整的拉链了。或者这是因为您使用MemoryStream
s作为文件源?因为这闻起来很像一个,因为它每次都会重写整个zipfile。