使用winrar通过c#或批处理自动提取ISO

使用winrar通过c#或批处理自动提取ISO,c#,batch-file,winrar,C#,Batch File,Winrar,我正在尝试将一个ISO提取到一个同名文件夹中,末尾没有.ISO 我在winrar上遇到了一个问题,因为当我在ISO文件夹中启动seach时,它不会启动提取 更新了应答码 private void ExtractISO(string toExtract, string folderName) { // reads the ISO CDReader Reader = new CDReader(File.Open(toExtract, FileMode.Open

我正在尝试将一个ISO提取到一个同名文件夹中,末尾没有.ISO

我在winrar上遇到了一个问题,因为当我在ISO文件夹中启动seach时,它不会启动提取

更新了应答码

private void ExtractISO(string toExtract, string folderName)
    {
        // reads the ISO
        CDReader Reader = new CDReader(File.Open(toExtract, FileMode.Open), true);
        // passes the root directory the folder name and the folder to extract
        ExtractDirectory(Reader.Root, folderName /*+ Path.GetFileNameWithoutExtension(toExtract)*/ + "\\", "");
        // clears reader and frees memory
        Reader.Dispose();
    }

    private 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 Ex)
        {
            AppendDirectory(Path.GetDirectoryName(path));
        }
    }
用户选择要提取的文件夹(.ISO)以提取。然后我在后台工作程序的Process.Start()中使用它。这似乎只是打开了安装软件,并没有将ISO提取到所需的文件夹名称

提前感谢你的帮助

或者,如果有人能给我一个批次来提取ISO,并从c#传递到extract和文件夹名中调用它,那也会很有帮助

谢谢

试试这个:

string Desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
Process.Start("Winrar.exe", string.Format("x {0} {1}",
   Desktop + "\\test.rar",
   Desktop + "\\SomeFolder"));
这将把文件
test.rar
提取到文件夹
SomeFolder
。您可以将.rar扩展名更改为.iso,其工作原理相同

据我所知,在您当前的代码中,没有给出提取文件的命令,也没有必须提取的文件路径。试试这个例子,让我知道它是否有效=]


另外,如果要隐藏提取屏幕,可以将
YourProcessInfo.WindowStyle
设置为
ProcessWindowStyle.Hidden
,如果外部类库正常

然后使用或提取ISO的

这两个类库可以管理ISO并提取它们

对于
DiscUtils
,您可以在我提供的链接中找到一些ISO管理代码[
CDReader
Class]

但是对于
SevenZipSharp
,请浏览类库源代码并找到要提取的代码,或者通过谷歌查找

要获取文件夹的名称,只需使用
Path.GetFileNameWithoutExtension((字符串)ISOFileName)
,对于名为
“ISOFile.iso”
的iso,它将返回
“ISOFile”
。然后,您可以将其用于所需的路径

更新 使用DiscUtils提取ISO图像的代码:

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));
    }
}
将其与以下内容一起使用:

ExtractISO(ISOFileName, Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\");
工作!我测试过了

当然,您可以随时为代码添加更多优化


此代码只是一个基本代码

对于UDF或在无需服务(DISM)后制作Windows ISO文件,上述公认的答案对我不起作用,因此我尝试了使用DiscUtils的这种工作方法

using DiscUtils;
public static void ReadIsoFile(string sIsoFile, string sDestinationRootPath)
        {
            Stream streamIsoFile = null;
            try
            {
                streamIsoFile = new FileStream(sIsoFile, FileMode.Open);
                DiscUtils.FileSystemInfo[] fsia = FileSystemManager.DetectDefaultFileSystems(streamIsoFile);
                if (fsia.Length < 1)
            {
                MessageBox.Show("No valid disc file system detected.");
            }
            else
            {
                DiscFileSystem dfs = fsia[0].Open(streamIsoFile);                    
                ReadIsoFolder(dfs, @"", sDestinationRootPath);
                return;
            }
        }
        finally
        {
            if (streamIsoFile != null)
            {
                streamIsoFile.Close();
            }
        }
    }

public static void ReadIsoFolder(DiscFileSystem cdReader, string sIsoPath, string sDestinationRootPath)
    {
        try
        {
            string[] saFiles = cdReader.GetFiles(sIsoPath);
            foreach (string sFile in saFiles)
            {
                DiscFileInfo dfiIso = cdReader.GetFileInfo(sFile);
                string sDestinationPath = Path.Combine(sDestinationRootPath, dfiIso.DirectoryName.Substring(0, dfiIso.DirectoryName.Length - 1));
                if (!Directory.Exists(sDestinationPath))
                {
                    Directory.CreateDirectory(sDestinationPath);
                }
                string sDestinationFile = Path.Combine(sDestinationPath, dfiIso.Name);
                SparseStream streamIsoFile = cdReader.OpenFile(sFile, FileMode.Open);
                FileStream fsDest = new FileStream(sDestinationFile, FileMode.Create);
                byte[] baData = new byte[0x4000];
                while (true)
                {
                    int nReadCount = streamIsoFile.Read(baData, 0, baData.Length);
                    if (nReadCount < 1)
                    {
                        break;
                    }
                    else
                    {
                        fsDest.Write(baData, 0, nReadCount);
                    }
                }
                streamIsoFile.Close();
                fsDest.Close();
            }
            string[] saDirectories = cdReader.GetDirectories(sIsoPath);
            foreach (string sDirectory in saDirectories)
            {
                ReadIsoFolder(cdReader, sDirectory, sDestinationRootPath);
            }
            return;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
使用DiscUtils;
公共静态void ReadIsoFile(字符串sIsoFile、字符串sdestentiationrootpath)
{
streamIsoFile=null;
尝试
{
streamIsoFile=新文件流(sIsoFile,FileMode.Open);
DiscUtils.FileSystemInfo[]fsia=FileSystemManager.DetectDefaultFileSystems(streamIsoFile);
如果(fsia.Length<1)
{
Show(“未检测到有效的光盘文件系统”);
}
其他的
{
DiscFileSystem dfs=fsia[0]。打开(StreamIsFile);
ReadIsoFolder(dfs,@“”,sdestinitionRootPath);
返回;
}
}
最后
{
if(streamIsoFile!=null)
{
streamIsoFile.Close();
}
}
}
公共静态void ReadIsoFolder(discsfilesystem cdReader、string sIsoPath、string sDestinationRootPath)
{
尝试
{
字符串[]saFiles=cdReader.GetFiles(sIsoPath);
foreach(安全文件中的字符串sFile)
{
DiscFileInfo dfiso=cdReader.GetFileInfo(sFile);
字符串sDestinationPath=Path.Combine(sDestinationRootPath,dfiso.DirectoryName.Substring(0,dfiso.DirectoryName.Length-1));
如果(!Directory.Exists(sDestinationPath))
{
Directory.CreateDirectory(sdestinitionpath);
}
字符串sDestinationFile=Path.Combine(sDestinationPath,dfiso.Name);
SparseStream streamIsoFile=cdReader.OpenFile(sFile,FileMode.Open);
FileStream fsDest=newfilestream(sDestinationFile,FileMode.Create);
字节[]baData=新字节[0x4000];
while(true)
{
int nReadCount=streamIsoFile.Read(baData,0,baData.Length);
如果(nReadCount<1)
{
打破
}
其他的
{
fsDest.Write(baData,0,nReadCount);
}
}
streamIsoFile.Close();
fsDest.Close();
}
字符串[]saDirectories=cdReader.GetDirectories(sIsoPath);
foreach(saDirectories中的字符串sddirectory)
{
ReadIsFolder(cdReader、SDdirectory、SDDestinationRootPath);
}
返回;
}
捕获(例外情况除外)
{
Show(例如ToString());
}
}
它从应用程序源ISOReader中提取,但根据我的要求进行了修改


总来源可在

上找到,我最近遇到了这种.iso提取问题。在尝试了几种方法之后,7zip为我完成了这项工作,您只需确保在您的系统上安装了最新版本的7zip。也许会有帮助 尝试 {

            Process cmd = new Process();
            cmd.StartInfo.FileName = "cmd.exe";
            cmd.StartInfo.RedirectStandardInput = true;
            cmd.StartInfo.RedirectStandardOutput = true;
            cmd.StartInfo.CreateNoWindow = false;
            cmd.StartInfo.UseShellExecute = false;
            cmd.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
            cmd.Start();

            cmd.StandardInput.WriteLine("C:");
            //Console.WriteLine(cmd.StandardOutput.Read());
            cmd.StandardInput.Flush();

            cmd.StandardInput.WriteLine("cd C:\\\"Program Files\"\\7-Zip\\");
            //Console.WriteLine(cmd.StandardOutput.ReadToEnd());
            cmd.StandardInput.Flush();


            cmd.StandardInput.WriteLine(string.Format("7z x -y -o{0} {1}", source, copyISOLocation.TempIsoPath));
            //Console.WriteLine(cmd.StandardOutput.ReadToEnd());
            cmd.StandardInput.Flush();
            cmd.StandardInput.Close();
            cmd.WaitForExit();
            Console.WriteLine(cmd.StandardOutput.ReadToEnd());
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message + "\n" + e.StackTrace);
            if (e.InnerException != null)
            {
                Console.WriteLine(e.InnerException.Message + "\n" + e.InnerException.StackTrace);
            }
        }

如何使用并避免向第三方可执行文件抛出?您应该能够创建一个命令调用并以这种方式执行。请看这里的参考@DavidHeffernan从iso中提取文件的这一部分,可以用于提取完整的.iso吗?以及如何提取?如果您可以提取文件,那么您可以提取完整的.iso关于iso。如果你想知道如何做,那么你可以阅读文档。Hi刚试过,收到错误消息“找不到存档”,我将backgroundworker中的代码改为你给我的代码,并更改了文件