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 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 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(_ => { });
        }
        public void CreateTest()
        {
            string filePath = @"F:\ziptest";
            string fileName = "tes.zip";

            var zipfilepath = new ZipFilePath()
            {
                FilePath = filePath, FileName = fileName
            };
            var zip = new com.study.core.utility.io.compression.ZipArchiveFiles(zipfilepath);

            zip.AddFilesInFolder(@"F:\강의\대용량아키텍처설계\대용량아키텍처설계");
            zip.CreateFile();

            string zipfile = System.IO.Path.Combine(zipfilepath.FilePath, zipfilepath.FileName);

            Assert.IsTrue(System.IO.File.Exists(zipfile));
        }
        public void CreateFolderTest()
        {
            string sourcepath = @"F:\강의\대용량아키텍처설계\대용량아키텍처설계";
            string filePath   = @"F:\ziptest";
            string fileName   = "foldertest.zip";

            var zipfilepath = new ZipFilePath()
            {
                FilePath = filePath, FileName = fileName
            };
            //var zip = new com.study.core.utility.io.compression.ZipArchiveFolder();

            var targetfile = System.IO.Path.Combine(zipfilepath.FilePath, zipfilepath.FileName);

            string createfile = com.study.core.utility.io.compression.ZipArchiveFolder.CreateFromDirectory(sourcepath, targetfile);


            Assert.IsTrue(System.IO.File.Exists(createfile));
        }
        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((_) => { });
        }
        public void ExtractFolderTest()
        {
            string filePath = @"F:\ziptest";
            string fileName = "foldertest.zip";

            var zipfilepath = new ZipFilePath()
            {
                FilePath = filePath, FileName = fileName
            };
            //var zip = new com.study.core.utility.io.compression.ZipArchiveFolder();

            var    sourcepath   = System.IO.Path.Combine(filePath, fileName);
            string tatgetfolder = System.IO.Path.Combine(filePath, System.IO.Path.GetFileNameWithoutExtension(fileName));

            com.study.core.utility.io.compression.ZipArchiveFolder.ExtractToDirectory(sourcepath, tatgetfolder);

            string[] files       = System.IO.Directory.GetFiles(tatgetfolder);
            var      notzipfiles = files.Where(file => System.IO.Path.GetExtension(file) != "zip");


            Assert.IsTrue(notzipfiles.ToList().Count > 0);
        }
Exemple #8
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));
        }
        public void CreateTestStream()
        {
            string filePath = @"F:\ziptest";
            string fileName = "tes.zip";

            var zipfilepath = new ZipFilePath()
            {
                FilePath = filePath, FileName = fileName
            };
            var zip = new com.study.core.utility.io.compression.ZipArchiveFiles(zipfilepath);

            zip.AddFilesInFolder(@"F:\강의\대용량아키텍처설계\대용량아키텍처설계");
            var stream = zip.CreateSream();

            stream.Position = 0;
            string zipfile = System.IO.Path.Combine(zipfilepath.FilePath, zipfilepath.FileName);

            Assert.IsTrue(System.IO.File.Exists(zipfile));

            string           doctype          = "application/octet-stream";
            FileStreamResult fileStreamResult = new FileStreamResult(stream, doctype);

            fileStreamResult.FileDownloadName = fileName;
        }