protected override async Task <Action <AsyncCodeActivityContext> > ExecuteAsync(AsyncCodeActivityContext context, CancellationToken token)
        {
            var entriesCount = 0;
            var foldersCount = 0;
            var filesCount   = 0;
            var filePath     = ZipFilePath.Get(context);

            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException(filePath);
            }

            await Task.Run(() =>
            {
                using (var zip = ZipFile.Open(filePath, ZipArchiveMode.Read))
                {
                    entriesCount = zip.Entries.Count;
                    foldersCount = zip.Entries.Count(entry => string.IsNullOrEmpty(entry.Name));
                    filesCount   = entriesCount - foldersCount;
                }
            }).ConfigureAwait(false);

            return(ctx =>
            {
                EntriesCount.Set(ctx, entriesCount);
                FilesCount.Set(ctx, filesCount);
                FoldersCount.Set(ctx, foldersCount);
            });
        }
Пример #2
0
        protected override async Task <Action <AsyncCodeActivityContext> > ExecuteAsync(AsyncCodeActivityContext context, CancellationToken token)
        {
            var zipFilePath = Path.GetFullPath(ZipFilePath.Get(context));
            var toCompress  = ToCompress.Get(context);
            var encoding    = TextEncoding.Get(context);
            var counter     = 0;

            if (toCompress is string)
            {
                toCompress = new string[] { toCompress.ToString() }
            }
            ;

            await Task.Run(() =>
            {
                var paths       = (IEnumerable <string>)toCompress;
                var directories = paths.Where(Directory.Exists);
                var files       = paths.Except(directories)
                                  .Concat(directories.SelectMany(path => Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories)))
                                  .Select(Path.GetFullPath)
                                  .Where(path => path != zipFilePath);

                var emptyFolders = directories.SelectMany(dir => Directory.EnumerateDirectories(dir, "*", SearchOption.AllDirectories))
                                   .Select(Path.GetFullPath)
                                   .Where(path => !Directory.EnumerateFileSystemEntries(path).Any());

                var entries = files.Concat(emptyFolders).OrderBy(path => path).ToArray();

                var mode = File.Exists(zipFilePath)
                    ? ZipArchiveMode.Update
                    : ZipArchiveMode.Create;

                using (var zip = ZipFile.Open(zipFilePath, mode, encoding))
                    counter = CompressTo(zip, entries, mode, token, null);
            }, token).ConfigureAwait(false);

            return(ctx => FilesCount.Set(ctx, counter));
        }