private static ArchiveFile CreateFileFromEntry(ArchiveDirectory parent, string name, IArchiveEntry entry)
 {
     return(new ArchiveFile(
                parent,
                name,
                entry.Length,
                entry.CompressedLength));
 }
        private static void RecursiveGetNameLength(ref int value, ArchiveDirectory parent)
        {
            if (parent.Parent != null)
            {
                RecursiveGetNameLength(ref value, parent.Parent);

                value += parent.Name.Length + 1;
            }
        }
        private static void RecursiveAppend(StringBuilder builder, ArchiveDirectory parent)
        {
            if (parent.Parent != null)
            {
                // calling this function again before appending the current
                // name creates the full name by unwinding the hierarchy
                RecursiveAppend(builder, parent.Parent);

                builder.Append(parent.Name);
                builder.Append('/');
            }
        }
        private static ArchiveDirectory CreateDirectoryTree(string[] segments, int offset, int count, ArchiveDirectory topDir)
        {
            if (offset >= count)
            {
                return(topDir);
            }

            string segment = segments[offset];

            if (!topDir.Directories.TryGetValue(segment, out var subDir))
            {
                subDir = new ArchiveDirectory(topDir, segment);
                topDir.Directories.Add(segment, subDir);
            }

            return(CreateDirectoryTree(segments, offset + 1, count, subDir));
        }
        public ArchiveDirectory BuildTree(IArchive archive, ProgressDelegate onProgress)
        {
            var topDir = new ArchiveDirectory(null, archive.Name);

            int processed = 0;

            foreach (IArchiveEntry entry in archive)
            {
                string[] segments = GetPathSegments(entry.FullName);
                if (entry.IsDirectory)
                {
                    CreateDirectoryTree(segments, 0, segments.Length, topDir);
                }
                else
                {
                    if (segments.Length == 1)
                    {
                        string segment = segments[0];
                        var    file    = CreateFileFromEntry(topDir, segment, entry);
                        topDir.Files.Add(segment, file);
                    }
                    else
                    {
                        var    dir     = CreateDirectoryTree(segments, 0, segments.Length - 1, topDir);
                        string segment = segments[segments.Length - 1];

                        var file = CreateFileFromEntry(topDir, segment, entry);
                        dir.Files.Add(segment, file);
                    }
                }
                processed++;
                onProgress?.Invoke(processed / (float)archive.Count);
            }

            return(topDir);
        }
 public ArchiveDirectory(ArchiveDirectory parent, string name) : base(parent, name)
 {
     Directories = new Dictionary <string, ArchiveDirectory>(StringComparer.Ordinal);
     Files       = new Dictionary <string, ArchiveFile>(StringComparer.Ordinal);
 }
 protected ArchiveItem(ArchiveDirectory parent, string name)
 {
     Parent = parent;
     Name   = name;
 }