extract ISO with winrar automatically with c# or batch

前端 未结 4 681
时光说笑
时光说笑 2021-01-20 02:36

I\'m trying to extract an ISO to a folder with the same name without .iso on the end.

I\'m having a problem with winrar as it will not start the extract when I start

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-20 03:06

    If external Class Libraries are OK!

    Then use SevenZipSharp or .NET DiscUtils to extract ISO's...

    These two ClassLibraries can manage ISO and Extract them!

    For DiscUtils you can find some codes for ISO Management [CDReader Class] at the Link I provided.

    But For SevenZipSharp, Please Explore the ClassLibrary source and find the Code to Extract or Google to find it!

    To get the Name of the folder just use Path.GetFileNameWithoutExtension((string)ISOFileName) which will return "ISOFile" for an iso named "ISOFile.iso". And then you can use it with your desired path.

    UPDATE

    Code To Extract ISO Image with DiscUtils :

    using DiscUtils;
    using DiscUtils.Iso9660;
    
    void ExtractISO(string ISOName, string ExtractionPath)
    {
        using (FileStream ISOStream = File.Open(ISOName, FileMode.Open))
        {
            CDReader Reader = new CDReader(ISOStream, true, true);
            ExtractDirectory(Reader.Root, ExtractionPath + Path.GetFileNameWithoutExtension(ISOName) + "\\", "");
            Reader.Dispose();
        }
    }
    void ExtractDirectory(DiscDirectoryInfo Dinfo, string RootPath, string PathinISO)
    {
        if (!string.IsNullOrWhiteSpace(PathinISO))
        {
            PathinISO += "\\" + Dinfo.Name;
        }
        RootPath += "\\" + Dinfo.Name;
        AppendDirectory(RootPath);
        foreach (DiscDirectoryInfo dinfo in Dinfo.GetDirectories())
        {
            ExtractDirectory(dinfo, RootPath, PathinISO);
        }
        foreach (DiscFileInfo finfo in Dinfo.GetFiles())
        {
                using (Stream FileStr = finfo.OpenRead())
                {
                    using (FileStream Fs = File.Create(RootPath + "\\" + finfo.Name)) // Here you can Set the BufferSize Also e.g. File.Create(RootPath + "\\" + finfo.Name, 4 * 1024)
                    {
                        FileStr.CopyTo(Fs, 4 * 1024); // Buffer Size is 4 * 1024 but you can modify it in your code as per your need
                    }
                }
        }
    }
    static void AppendDirectory(string path)
    {
        try
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }
        catch (DirectoryNotFoundException Ex)
        {
            AppendDirectory(Path.GetDirectoryName(path));
        }
        catch (PathTooLongException Exx)
        {
            AppendDirectory(Path.GetDirectoryName(path));
        }
    }
    

    Use It with Like This :

    ExtractISO(ISOFileName, Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\");
    

    Working! Tested By Me!

    And Of Course You can always add more Optimization to the code...

    This Code is Just a Basic One!

提交回复
热议问题