当前位置: 首页>>代码示例>>C#>>正文


C# ZipFile.AddFileStream方法代码示例

本文整理汇总了C#中ZipFile.AddFileStream方法的典型用法代码示例。如果您正苦于以下问题:C# ZipFile.AddFileStream方法的具体用法?C# ZipFile.AddFileStream怎么用?C# ZipFile.AddFileStream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ZipFile的用法示例。


在下文中一共展示了ZipFile.AddFileStream方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SaveImpl

        private void SaveImpl(String fileName, bool rebuildZipEntries)
        {
            using (ExposeReadOnly())
            {
                var newzip = new ZipFile(){Encoding = Encoding.UTF8};

                // bug. here we face a potential tho very unprobable sync problem
                // if we've imported some nodes from another vault and are now unbinding them
                // it's possible that the vault will right now undergo certain changes that
                // won't be propagated to the nodes we've just unbound
                Root.GetValuesRecursive(ValueKind.RegularAndInternal).ForEach(Bind);
                Root.GetBranchesRecursive().ForEach(Bind);

                // mapping between values/branches and entries in the new file
                var newZeIndex = new Dictionary<IElement, String>();

                // save all values -> this will also automatically create corresponding branches
                foreach (Value value in Root.GetValuesRecursive(ValueKind.RegularAndInternal))
                {
                    var contentStream = value.ContentStream.FixupForBeingSaved();
                    var valueZe = newzip.AddFileStream(value.Name, value.VPath.Parent.ToZipPathDir(), contentStream);
                    newZeIndex.Add(value, valueZe.FileName);

                    if (value.Metadata.Raw != null)
                        newzip.AddFileStream(value.Name + "$", value.VPath.Parent.ToZipPathDir(), value.Metadata.Raw.AsStream());
                }

                // despite of the previous step having created the branches, 
                // we still need to explicitly add them in order to store the metadata
                foreach(Branch branch in Root.GetBranchesRecursive())
                {
                    var branchZe = newzip.AddDirectoryByName(branch.VPath.ToZipPathDir());
                    newZeIndex.Add(branch, branchZe.FileName);

                    if (branch.Metadata.Raw != null)
                        newzip.AddFileStream("$", branch.VPath.ToZipPathDir(), branch.Metadata.Raw.AsStream());
                }

                // root metadata requires special treatment since root doesn't get enumerated
                if (Root.Metadata.Raw.IsNeitherNullNorEmpty())
                    newzip.AddFileStream("$", String.Empty.ToZipPathDir(), Root.Metadata.Raw.AsStream());

                if (rebuildZipEntries)
                {
                    GC.Collect(); // is this really necessary here?
                    var deletedButStillAlive = BoundElements.Select(wr => wr.IsAlive ? (IElement)wr.Target : null)
                        .Where(el => el != null)
                        .Except(Root.GetValuesRecursive(ValueKind.RegularAndInternal).Cast<IElement>())
                        .Except(Root.GetBranchesRecursive().Cast<IElement>())
                        .Except(Root.MkArray())
                        .Distinct();

                    // fixup metadata/content streams of deleted and not yet gcollected nodes
                    Action<Action> neverFail = a => { try { a(); } catch { /* just ignore */ } };
                    deletedButStillAlive.ForEach(el => neverFail(() => el.CacheInMemory()));

                    // only now can we dispose the previous zip instance
                    // previously it was necessary to extract streams we're going to repack
                    if (Zip != null)
                    {
                        Zip.Dispose();
                    }

                    Zip = newzip;
                    newzip.Save(fileName);

                    // fixup content/metadata streams to reference the new file/vpaths
                    var opt = Zip.Entries.ToDictionary(ze => ze.FileName, ze => ze);
                    foreach (Value value in Root.GetValuesRecursive(ValueKind.RegularAndInternal))
                    {
                        var contentFile = newZeIndex[value];
                        var metadataFile = contentFile + "$";

                        value.SetContent(() => opt[contentFile].ExtractEager());
                        value.RawSetMetadata(() => opt.GetOrDefault(metadataFile).ExtractEager());
                    }

                    foreach (Branch branch in Root.GetBranchesRecursive())
                    {
                        var metadataFile = newZeIndex[branch] + "$";
                        branch.RawSetMetadata(() => opt.GetOrDefault(metadataFile).ExtractEager());
                    }

                    // root metadata requires special treatment since root doesn't get enumerated
                    Root.RawSetMetadata(() => (opt.GetOrDefault("/$") ?? opt.GetOrDefault("$")).ExtractEager());

                    // set the changes in stone
                    Root.AfterSave();
                    Root.GetBranchesRecursive().Cast<Branch>().ForEach(b => b.AfterSave());
                    Root.GetValuesRecursive(ValueKind.RegularAndInternal).Cast<Value>().ForEach(v => v.AfterSave());
                }
                else
                {
                    if (Uri == fileName)
                    {
                        throw new InvalidOperationException("Saving vault into its source file requires rebuilding ZIP entries.");
                    }
                    else
                    {
                        newzip.Save(fileName);
//.........这里部分代码省略.........
开发者ID:xeno-by,项目名称:datavault,代码行数:101,代码来源:ZipVault.cs


注:本文中的ZipFile.AddFileStream方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。