【问题标题】:Deletion of compressed file after extracting it with nunrar用 nunrar 解压后删除压缩文件
【发布时间】:2013-07-04 10:28:09
【问题描述】:

我使用 lib Nunrar site 提取了一个 .rar 文件:

RarArchive.WriteToDirectory(fs.Name, Path.Combine(@"D:\DataDownloadCenter", path2), ExtractOptions.Overwrite);

解压可以正常工作,但是我无法在提取操作后删除原始压缩文件

System.IO.File.Delete(path);

因为文件是被另一个进程使用 孔函数:

 try
           {
               FileStream fs = File.OpenRead(path);
               if(path.Contains(".rar")){

                   try
                   {
                       RarArchive.WriteToDirectory(fs.Name, Path.Combine(@"D:\DataDownloadCenter", path2), ExtractOptions.Overwrite);
                       fs.Close();

                   }
                   catch { }

                   }

           catch { return; }
           finally
           {
               if (zf != null)
               {
                   zf.IsStreamOwner = true; // Makes close also shut the underlying stream
                   zf.Close(); // Ensure we release resources
               }
           }
           try
           {
               System.IO.File.Delete(path);
           }
           catch { }

那么我可以在解压后删除压缩文件吗?

【问题讨论】:

  • fs 的类型是什么(您在哪里使用fs.Name)?我对图书馆不熟悉,但您似乎打开了FileStream 并没有关闭它。
  • 这是一个FileStream,我在删除操作之前做了fs.Close()

标签: c# .net io delete-file


【解决方案1】:

我不知道zf 是什么,但您也可以将其包装在using 语句中。尝试用这个替换你的 FileStream fs 部分

using( FileStream fs = File.OpenRead(path))
{
    if(path.Contains(".rar"))
    {
        try
        {
           RarArchive.WriteToDirectory(fs.Name, Path.Combine(@"D:\DataDownloadCenter", path2), ExtractOptions.Overwrite);
        }
        catch { }
     }
}

这样fs 就会关闭,即使path 不包含.rar。只有在文件名中存在rar 时,您才关闭fs

另外,库是否有自己的流处理?它可能有一个关闭它的方法。

【讨论】:

  • 库没有流处理。对于zfZipFile zf = null;
  • 我也有这个问题。 close() 或 using 似乎都想处置该对象。最好找到另一个库。
【解决方案2】:

nunrar、nether close() 或 using 语句似乎也解决了这个问题。 不幸的是,文档很少,所以我现在使用 SharpCompress library 它是 nunrar 库 according to the devs of nunrar 的一个分支。关于 SharpCompress 的文档也很少(但更少)所以这是我使用的方法:

private static bool unrar(string filename)
{
    bool error = false;
    string outputpath = Path.GetDirectoryName(filename);

    try
    {
        using (Stream stream = File.OpenRead(filename))
        {
            var reader = ReaderFactory.Open(stream);
            while (reader.MoveToNextEntry())
            {
                if (!reader.Entry.IsDirectory)
                {
                    Console.WriteLine(reader.Entry.Key);
                    reader.WriteEntryToDirectory(outputpath, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true });
                }
            }
        }
    }
    catch (Exception e)
    {
        Console.WriteLine("Failed: " + e.Message);
        error = true;
    }

    if (!error)
    {
        File.Delete(filename);
    }
    return error;
}

将以下库添加到顶部

using SharpCompress.Common;
using SharpCompress.Readers;

使用 nuget 安装。此方法适用于 SharpCompress v0.22.0(撰写本文时最新)

【讨论】:

    猜你喜欢
    • 2016-05-02
    • 1970-01-01
    • 1970-01-01
    • 2023-01-08
    • 2011-02-17
    • 1970-01-01
    • 1970-01-01
    • 2015-10-03
    • 2021-12-26
    相关资源
    最近更新 更多