本文整理汇总了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);
//.........这里部分代码省略.........