Getting "All" File Names In An Archive

Apr 23, 2010 at 2:04 AM

I was wondering, how can I get all the file names inside an archive when in contains additional archives? For example, main archive "archive1.7z" contains some files, as well as an "archive2.rar" and an "archive3.zip". How would I get all the file names in archive1.7z, including the ones in archive2.rar and archive3.zip?

I've been using .ArchiveFileNames.ToList<string>() to generate a list of file names in an archive, but it doesn't seem able to get file names when they are in additional archives. The only other method I can think of doing this is by extracting the archive, using ArchiveFileNames on all the extracted archives to create file name lists for them, merging all the file name lists, and then deleting the extracted files. That seems a little bit slow and messy.

Also, thanks to the creator of SevenZipSharp. It has helped me to greatly improve a program I am working on, with ease.

Coordinator
Apr 23, 2010 at 5:10 AM
Edited Apr 23, 2010 at 5:14 AM

Hello.

The only way to do this is to iterate through the outer archive contents and read inner archives

 

private List<ArchiveFileInfo> _ultimateContents = new List<ArchiveFileInfo>();

private void ReadUltimateContents(string fileName)
{
    using (var fs = File.OpenRead(fileName))
    {
        RecursiveAddContents(fs, _ultimateContents);
    }
}

private void RecursiveAddContents(Stream arch, List<ArchiveFileInfo> contents)
{
using (var extr = new SevenZipExtractor(arch))
{
    foreach (var item in extr.ArchiveFileData)
    {
        if (item.FileName.EndsWith("tar")) //any other extension you want
        // the other way is to try to create a SevenZipExtractor on the FileName and if the creation is successful then it is an archive
        {
             // this is the easiest way. It will be much faster in case of solid archives to remember the indices of inner archives and then call ExtractFiles with them only once, instead of calling ExtractFile each time. It will take more memory though.
             using (var ms = new MemoryStream())
             {
                 extr.ExtractFile(item.Index, ms);
                 RecursiveAddContents(ms, contents);
             }
        }
    }
    // or something similar
    contents.AddRange((IEnumerable<ArchiveFileData>)extr.ArchiveFileData);
}

}

 

Apr 24, 2010 at 7:43 PM

Thank you for the help, I will try that out.

Oct 9, 2014 at 2:54 PM
Hi,

How can I get all the file names inside an archive “archive1.tar.gz” when in contains additional archives?
For Example I have a main archive “archive1.zip” and it contains archive “archive2.tar.gz” and “archive3.gz" also archive “archive2.tar.gz” contains files, directory and “.tar.gz” files.
Please let me know how to read the contents of the .tar.gz and .gz file archives.