5

TL;DR

the sample code given on the site's home page does not compile. How to list all the volumes in a multi-part RAR file?

Problem definition

I have a Windows 10 folder with lots of multi-part RAR files. The 'root' rar and its parts need to be segregated and then moved into their own folders. I can do this manually by creating a folder and inspecting the file names such that the root rar and then 'part01.rar', 'part02.rar', etc. are all in the same directory.

When there are dozens of multi-part files the manual process is tedious, so I am writing a C# app. What I have learned so far is that the naming conventions with these multi-part RAR files is not consistent and this has left me in the awkward exercise of examining their file names and inferring where they belong by string manipulation. Uncool.

So I am importing SharpCompress with a view towards getting the file names scientifically and precisely. There is an example on on its documentation page which appears to do EXACTLY what I want. I have wrapped a method around it and now present it to you as 'what I have tried'.

private static void GetFileNames(FileInfo rarFileInfo)
{
    var archive = ArchiveFactory.Open(rarFileInfo.FullName);
    foreach (var entry in archive.Entries)
    {
        if (!entry.IsDirectory)
        {
            Console.WriteLine(entry.FilePath);
        }
    }
}

...and note the red text produced by VS...

enter image description here

It doesn't compile because 'FilePath' cannot be resolved! Do I have the wrong framework? I am using .NET 4.5 under VS2013. Theoretically there should be a lot of questions in here already because the documentation sample on the home site doesn't compile, but there's not (I checked).

Question: How to use SharpCompress to identify all the volumes associated with a given RAR such that their names can be available to a developer.


NOTE: I could also use the WinRar API to unpack the files into their own directories, but this is not what the client wants. I will actually unpack them later, the object is to get all the associated volumes together in their own folders.

leppie
  • 115,091
  • 17
  • 196
  • 297
Gayot Fow
  • 8,710
  • 1
  • 35
  • 48

1 Answers1

4

Fixing the Example

Note that this is a red herring Your problem appears to be that you are mismatching documentation and library

The last version on CodePlex of SharpCompress is 0.10.3, for which (when I download it), your code and the example code works fine.

However, the versions existent on NuGet and GitHub are 0.12.4

In this version, it appears that the member of IArchiveElement of FullPath has been dropped.

The documentation for this version is available on github, and the example is changed thus:

var archive = ArchiveFactory.Open(@"C:\Code\sharpcompress\TestArchives\sharpcompress.zip");
foreach (var entry in archive.Entries)
{
    if (!entry.IsDirectory)
    {
        Console.WriteLine(entry.Key);
        entry.WriteToDirectory(@"C:\temp", ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
    }
}

It would appear that archive.Entries is now a dictionary (or similar) of IArchiveEntry, and the path is used as the key for the dictionary, rather than as a member of IArchiveEntry itself.

So the short answer is that you should replace Entry.FullPath with Entry.Key in your code above. I'm not clear on how this helps identify which archives need to be combined together however.

How to actually solve your problem

What you actually want is to find what volumes make up a multi-part rar. Which involves using the specific features of the "Rar" clases, and not the base interfaces used above. This should work:

    static void PrintVoulmes(string ArchivePath)
    {
       RarArchive openedArchive = RarArchive.Open(ArchivePath);
       foreach (RarVolume vol in openedArchive.Volumes)
       {
           Console.WriteLine(vol.VolumeFile.FullName);
       }
    }

Note that VolumeFile is of the standard .net FileInfo type. Also note: This was tested with version 0.10.3, because I can't get a version of 0.12.4 for VS2012, apparently. THere is a chance that Property names have been changed here as well.

Community
  • 1
  • 1
CMaster
  • 389
  • 3
  • 16