RarReader is a push reader akin to System.Xml.XmlReader.

Create a RarReader from a single stream or an IEnumerable of streams for a multi-volume rar case. Below is an example for the multi-volume case. The IEnumerable for GetStreams is lazy so it is only called when the next stream is required to allow for the next stream to be provided on-the-fly.

The RarReader only has a current entry defined and must be moved to the next entry via MoveToNextEntry(). Once the reader has extracted an entry, it cannot be extracted again, it can only be moved. If the current entry is not extract, then the reader will move forward on the data stream until the next entry is found or the archive ends.

        private static void TestStream()
        {
            using (RarReader reader = RarReader.Open(GetStreams(), RarOptions.None))
            {
                while (reader.MoveToNextEntry())
                {
                    Console.WriteLine(reader.Volume.IsFirstVolume);
                    Console.WriteLine(reader.Volume.IsMultiVolume);

                    Console.WriteLine(reader.Entry.LastModifiedTime);
                    Console.WriteLine(reader.Entry.IsDirectory);
                    Console.WriteLine(reader.Entry.Size);

                    reader.WriteEntryToDirectory(@"C:\Test\Temp");
                }
            }
        }

        private static IEnumerable<Stream> GetStreams()
        {
            int i = 2;
            while (true)
            {
                i++;
                string s;
                if (i < 10)
                {
                    s = "0" + i;
                }
                else
                {
                    s = i.ToString();
                }
                yield return new ReadOnlyStream()
                             {
                                 Stream = File.OpenRead(@"C:\Test\RarArchive.part" + s + ".rar")
                             };
            }
        }

Last edited Apr 5, 2011 at 3:33 PM by adamhathcock, version 9

Comments

No comments yet.