Dumping all files from ISO

Jan 3, 2011 at 9:24 PM

I've been looking at DiscUtils -- it's great, amazing job. I was just wondering, though, is there a simple way to dump all the files from an ISO -- like an "ISOExtract" that's already made? I tried to RTM but didn't find anything in there...

Jan 4, 2011 at 9:04 AM
Edited Jan 4, 2011 at 9:10 AM

Hey steven,

 

here is an example which lists all files of an iso recursivly. When you want to really extract a file, you will need to iso.CopyFile(...)  I guess.

 

using System;
using System.IO;
using DiscUtils.Common;
using DiscUtils.Iso9660;

namespace ISOList
{
    class Program
    {
        static void listFiles(CDReader iso, string parent = "", bool recursive = true)
        {
            if (recursive)
            {
                foreach (string directory in iso.GetDirectories(parent))
                {
                    listFiles(iso, directory, true);
                }
            }

            foreach (string file in iso.GetFiles(parent))
            {
                DiscUtils.DiscFileInfo fi = iso.GetFileInfo(file);
                Console.WriteLine("\\" + fi.FullName + "\t" + fi.Length);
            }
        }

        static void Main(string[] args)
        {
            using (FileStream isoStream = System.IO.File.Open(@"x:\natty-desktop-i386.iso", FileMode.Open))
            {
                CDReader iso = new CDReader(isoStream, true);
                listFiles(iso);
            }
            Console.ReadKey();
        }
    }
}

Have Fun!

Ciao Ephraim

Jan 4, 2011 at 3:29 PM
Edited Jan 4, 2011 at 4:07 PM

I’m having problems with the CopyFile command working in that statement. Do you happen to have any time to take a look at some code?

 

Thanks,

Tony

 


Jan 4, 2011 at 3:33 PM

Whats about posting the code instead of asking if I have time to look at ;)....

Perhaps I can look at it ...

Ciao Ephraim

Jan 4, 2011 at 3:34 PM

        static void doCopyFilesfromISO(CDReader iso, string baseDirectory, string parent = "\\", bool recursive = true)
        {
            if (recursive)
            {
                foreach (string directory in iso.GetDirectories(parent))
                {
                    makeDirectory(baseDirectory, directory);
                    doCopyFilesfromISO(iso, baseDirectory, directory, true);
                }
            }

            foreach (string file in iso.GetFiles(parent))
            {
                string fileToCreate = baseDirectory + file;
                DiscUtils.DiscFileInfo fi = iso.GetFileInfo(Path.Combine(parent, file));
                iso.CopyFile(file, fileToCreate);
            }
        }

        static void copyFilesfromISO(string isoName, string baseDirectory)
        {
            using (FileStream isoStream = System.IO.File.Open(isoName, FileMode.Open))
            {
                CDReader iso = new CDReader(isoStream, true);
                doCopyFilesfromISO(iso, baseDirectory, "\\", true);
            }
        }

Jan 4, 2011 at 3:42 PM

Hey again :)

you should double check if the string fileToCreate contains the correct path, as I had a fault in the first post.
The string file and string directory are absolute to the root of the iso, so you don't need to Path.Combine with the string parent, but you append it to your baseDirectory which might end with
an incorrect path.

On the other hand, I don't know if iso.CopyFile is the correct choice, as I wrote, "I guess".
Perhaps a better way is, to open a filestream of the file in the iso, and copy that stream into a corresponding file on the destination.

So please double check that CopyFile does what you expect it to do, and double check the paths you combine there.

Else I can't see problems so far.

Ciao Ephraim

Jan 4, 2011 at 3:59 PM
Edited Jan 4, 2011 at 4:06 PM

Hi again,

 

The variables I have are:

 

file = “\\BOOT\\FONTS\\CHS_BOOT.TTF”

fileToCreate = “E:\\BOOT\\FONTS\\CHS_BOOT.TTF”

 

I think it might be a case of the wrong tool for the job with CopyFile.

 

Thanks

Tony

Jan 4, 2011 at 4:12 PM

I was just wondering, do you happen to have a code example that shows how to utilize a FileStream to copy a file from an ISO?

Jan 4, 2011 at 4:47 PM

Here's what I wrote and it appears to be working:

 

 

        public static void CopyStream(Stream input, Stream output)
        {
            byte[] buffer = new byte[8 * 1024];
            int len;
            while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                output.Write(buffer, 0, len);
            }
        }

        static void doCopyFile (string file, string fileToCreate, DiscUtils.DiscFileInfo fi, CDReader iso)
        {
            Stream getFileStream = iso.OpenFile(@file, FileMode.Open);
            using (Stream pushFileStream = File.OpenWrite(fileToCreate))
                CopyStream(getFileStream, pushFileStream);
        }

        static void doCopyFilesfromISO(CDReader iso, string baseDirectory, string parent = "\\", bool recursive = true)
        {
            if (recursive)
            {
                foreach (string directory in iso.GetDirectories(parent))
                {
                    makeDirectory(baseDirectory, directory);
                    doCopyFilesfromISO(iso, baseDirectory, directory, true);
                }
            }

            foreach (string file in iso.GetFiles(parent))
            {
                string fileToCreate = baseDirectory + file;
                DiscUtils.DiscFileInfo fi = iso.GetFileInfo(Path.Combine(parent, file));
                doCopyFile(file, fileToCreate, fi, iso);

                // iso.CopyFile(file, fileToCreate);
            }
        }

        static void copyFilesfromISO(string isoName, string baseDirectory)
        {
            using (FileStream isoStream = System.IO.File.Open(isoName, FileMode.Open))
            {
                CDReader iso = new CDReader(isoStream, true);
                doCopyFilesfromISO(iso, baseDirectory, "\\", true);
            }
        }