如何在Dotnetzip中删除一个zip文件

时间:2019-11-13 09:31:58

标签: c# memorystream dotnetzip

我正试图删除zip中的DotNetZip文件,就像transaction(完全或完全不删除)一样。

目前,我有两次incremented count,目的是破坏应用程序并查看它是否保存了zip

这样做,它将zip保存为2个文件而不是3个文件,正如我所说的,我希望它表现得像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上找到了这篇文章。

DotNetZip how to delete a file after extracting

它说先读stream中的zip,然后再读Dispose。我已经尝试过了,但是zip文件仍然存在。

没有错误,只是不会删除文件。

1 个答案:

答案 0 :(得分:0)

该代码中存在很多混乱;计数器和foreach的组合,循环重新保存zipfile,不必要地重新读取已经打开的zipfile ...

由于您从字面上说保存过程中的任何错误都将导致完全中止,因此将try-catch 放在 ZipFile'的意义要大得多。的using块。毕竟,using块的功能之一就是清理对象。

就循环重新保存而言,我个人只是制作了一个MemoryStream对象的数组,在每次循环迭代中,填充一个对象,并将其作为文件链接到ZipFile中,并在整个循环之后 仅保存一次ZipFile。然后,您可以安全地从数组中处置MemoryStream对象。这样,它们在保存ZipFile的同时都同时存在。而且,如果仅将它们放置在ZipFile的{​​{1}}块之后,则可以100%确保using对象不再需要它们。

如果以此方式执行操作,则不会发生任何错误,因为它永远不会到达ZipFile行。因此,也无需删除任何内容。

zip.Save()

不过,似乎确实缺少一些细节。 1文件逻辑似乎根本不使用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; } ,这意味着ZipFile的{​​{1}}块从技术上讲可以放在ZipFile内。另外,多文件逻辑从不设置using