Exemplo n.º 1
0
        public Option <Link <NodeFolderPath, NodeFilePath> > CreateArchive(NodeFolderPath SrcDir,
                                                                           NodeFilePath DstPath, bool Overwrite = true, AppMessageObserver Observer = null)
        {
            try
            {
                DstPath.Folder.CreateIfMissing();

                var dstPath = Overwrite
                    ? DstPath.DeleteIfExists().Require()
                    : (DstPath.Exists() ? DstPath.UniqueName() : DstPath);

                using (var stream = new FileStream(dstPath.AbsolutePath, FileMode.OpenOrCreate))
                {
                    using (var archive = new ZipArchive(stream, ZipArchiveMode.Create))
                    {
                        foreach (var srcFile in SrcDir.Files("*.*", true))
                        {
                            Observer?.Invoke(AddingFileToArchive(srcFile, dstPath));
                            var entry = CreateEntry(SrcDir, srcFile, archive);
                            using (var writer = new StreamWriter(entry.Open()))
                                using (var reader = new StreamReader(srcFile.AbsolutePath))
                                    reader.BaseStream.CopyTo(writer.BaseStream);
                        }
                    }
                }
                return(link(SrcDir, dstPath));
            }
            catch (Exception e)
            {
                return(none <Link <NodeFolderPath, NodeFilePath> >(e));
            }
        }
Exemplo n.º 2
0
        public Option <Link <NodeFiles, NodeFilePath> > CreateArchive(IEnumerable <NodeFilePath> Src,
                                                                      NodeFilePath Dst, bool Overwrite = true)
        {
            try
            {
                var folder = Dst.Folder.CreateIfMissing();
                if (folder.IsNone())
                {
                    return(folder.ToNone <Link <NodeFiles, NodeFilePath> >());
                }

                var dstPath = Overwrite
                    ? Dst.DeleteIfExists().Require()
                    : (Dst.Exists() ? Dst.UniqueName() : Dst);

                using (var stream = new FileStream(Dst.AbsolutePath, FileMode.OpenOrCreate))
                {
                    using (var archive = new ZipArchive(stream, ZipArchiveMode.Create))
                    {
                        foreach (var srcFile in Src)
                        {
                            var entry = CreateEntry(srcFile.Folder.AbsolutePath, srcFile.AbsolutePath, archive);
                            using (var writer = new StreamWriter(entry.Open()))
                                using (var reader = new StreamReader(srcFile.AbsolutePath))
                                    reader.BaseStream.CopyTo(writer.BaseStream);
                        }
                    }

                    return(new Link <ReadOnlyList <NodeFilePath>, NodeFilePath>(metacore.rolist(Src), Dst));
                }
            }
            catch (Exception e)
            {
                return(none <Link <NodeFiles, NodeFilePath> >(e));
            }
        }