示例#1
0
        public static async Task CreateEntriesFromDirectoryAsync(this TarWriter writer, string path, string entryBase)
        {
            await CreateEntryFromFileAsync(writer, path, entryBase);

            // Keep a stack of directory enumerations in order to write the
            // tar in depth-first order (which seems to be more common in tar
            // implementations than the breadth-first order that SearchOption.AllDirectories
            // implements).
            var stack = new List <IEnumerator <string> >();
            IEnumerator <string> enumerator = null;

            try
            {
                enumerator = Directory.EnumerateFileSystemEntries(path).GetEnumerator();
                while (enumerator != null)
                {
                    while (enumerator.MoveNext())
                    {
                        var filePath = enumerator.Current;
                        var fileName = filePath.Substring(path.Length + 1);
                        var fi       = new FileInfo(filePath);
                        await writer.CreateEntryFromFileInfoAsync(fi, Path.Combine(entryBase, fileName));

                        if (fi.Attributes.HasFlag(FileAttributes.Directory))
                        {
                            stack.Add(enumerator);
                            enumerator = Directory.EnumerateFileSystemEntries(filePath).GetEnumerator();
                        }
                    }

                    enumerator.Dispose();
                    enumerator = null;
                    if (stack.Count > 0)
                    {
                        enumerator = stack.Last();
                        stack.RemoveAt(stack.Count - 1);
                    }
                }
            }
            finally
            {
                enumerator?.Dispose();

                foreach (var e in stack)
                {
                    e.Dispose();
                }
            }
        }
示例#2
0
        public static Task CreateEntryFromFileAsync(this TarWriter writer, string path, string entryName, CancellationToken cancellationToken)
        {
            var fi = new FileInfo(path);

            return(writer.CreateEntryFromFileInfoAsync(fi, entryName, cancellationToken));
        }