Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何删除Dotnetzip中的zip文件_C#_Memorystream_Dotnetzip - Fatal编程技术网

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。