protected override async Task <Action <AsyncCodeActivityContext> > ExecuteAsync(AsyncCodeActivityContext context, CancellationToken cancellationToken)
        {
            // Inputs
            var toCompress  = ToCompress.Get(context);
            var zipFilePath = ZipFilePath.Get(context);

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

            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 => new FileInfo(path))
                              .Where(fi => fi.FullName != Path.GetFullPath(zipFilePath));

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

            var counter   = 0;
            var allInRoot = files.Select(fi => fi.Directory.FullName).Distinct().Count() == 1;

            using (var zip = ZipFile.Open(zipFilePath, mode, Encoding))
            {
                counter = CreateZip(files, allInRoot, zip, mode);
            }

            // Outputs
            return((ctx) => FilesCount.Set(ctx, counter));
        }
        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);
            });
        }
        protected override async Task <Action <AsyncCodeActivityContext> > ExecuteAsync(AsyncCodeActivityContext context, CancellationToken token)
        {
            var zipFilePath = ZipFilePath.Get(context);
            var extractTo   = ExtractTo.Get(context);

            await Task.Run(() =>
            {
                using (var zip = ZipFile.OpenRead(zipFilePath))
                {
                    var dir     = Directory.CreateDirectory(extractTo);
                    var dirPath = dir.FullName;

                    foreach (var entry in zip.Entries)
                    {
                        if (token.IsCancellationRequested)
                        {
                            token.ThrowIfCancellationRequested();
                        }

                        var fullPath = Path.GetFullPath(Path.Combine(dirPath, entry.FullName));

                        if (!fullPath.StartsWith(dirPath, StringComparison.OrdinalIgnoreCase))
                        {
                            throw new IOException(Resources.Unzip_ErrorMsg_OutsideDir);
                        }

                        if (Path.GetFileName(fullPath).Length == 0)
                        {
                            if (entry.Length != 0L)
                            {
                                throw new IOException(Resources.Unzip_ErrorMsg_DirNameWithData);
                            }

                            Directory.CreateDirectory(fullPath);
                        }
                        else
                        {
                            Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
                            entry.ExtractToFile(fullPath, Overwrite);
                        }
                    }
                }
            }, token).ConfigureAwait(false);

            return(_ => { });
        }
        protected override async Task <Action <AsyncCodeActivityContext> > ExecuteAsync(AsyncCodeActivityContext context, CancellationToken cancellationToken)
        {
            // Inputs
            var zipFilePath = ZipFilePath.Get(context);
            var extractTo   = ExtractTo.Get(context);

            using (var zip = ZipFile.OpenRead(zipFilePath))
            {
                var dir     = Directory.CreateDirectory(extractTo);
                var dirPath = dir.FullName;

                foreach (var entry in zip.Entries)
                {
                    var fullPath = Path.GetFullPath(Path.Combine(dirPath, entry.FullName));

                    if (!fullPath.StartsWith(dirPath, StringComparison.OrdinalIgnoreCase))
                    {
                        throw new IOException(Resources.Unzip_ExtractingResultsInOutside_Error);
                    }

                    if (Path.GetFileName(fullPath).Length == 0)
                    {
                        if (entry.Length != 0L)
                        {
                            throw new IOException(Resources.Unzip_DirectoryNameWithData_Error);
                        }

                        Directory.CreateDirectory(fullPath);
                    }
                    else
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
                        entry.ExtractToFile(fullPath, Overwrite);
                    }
                }
            }

            // Outputs
            return((_) => { });
        }
Exemple #5
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));
        }