本文整理汇总了C#中ZipFile.AddDirectoryByName方法的典型用法代码示例。如果您正苦于以下问题:C# ZipFile.AddDirectoryByName方法的具体用法?C# ZipFile.AddDirectoryByName怎么用?C# ZipFile.AddDirectoryByName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ZipFile
的用法示例。
在下文中一共展示了ZipFile.AddDirectoryByName方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateZip_AddDirectory_BlankName
public void CreateZip_AddDirectory_BlankName()
{
string zipFileToCreate = Path.Combine(TopLevelDir, "CreateZip_AddDirectory_BlankName.zip");
using (ZipFile zip = new ZipFile(zipFileToCreate))
{
zip.AddDirectoryByName("");
zip.Save();
}
}
示例2: button1_Click
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
ZipFile zf = new ZipFile("C:\\Users\\cpaine\\Desktop\\MyZipFile.zip");
zf.AddDirectoryByName("Adam");
zf.AddFile(ofd.FileName, "Adam");
//zf.AddDirectory(fbd.SelectedPath, "");
if.Save;
}
}
示例3: MainSuccessScenario
public void MainSuccessScenario()
{
var content1 = "content1 " + Guid.NewGuid();
var content2 = "content2 " + Guid.NewGuid();
using (var write = new ZipFile())
{
write.AddFileFromString("file1.txt", "", content1);
write.AddFileFromString("file2.txt", @"Dir1\Dir2\", content2);
write.AddDirectoryByName("Dir2/Dir3");
write.Save("testarc.zip");
write.Dispose();
}
using(var read = new ZipFile("testarc.zip"))
{
Assert.AreEqual(1, read.Entries.Where(e => e.IsDirectory).Count());
Assert.AreEqual(2, read.Entries.Where(e => !e.IsDirectory).Count());
var ms1 = new MemoryStream();
var ms2 = new MemoryStream();
read.Extract(@"file1.txt", ms1);
read.Extract(@"Dir1\Dir2\file2.txt", ms2);
ms1.Seek(0, SeekOrigin.Begin);
ms2.Seek(0, SeekOrigin.Begin);
read.Dispose();
Assert.AreEqual(content1, new StreamReader(ms1).ReadToEnd());
Assert.AreEqual(content2, new StreamReader(ms2).ReadToEnd());
}
using (var write2 = new ZipFile("testarc.zip"))
{
write2.RemoveEntry(@"file1.txt");
write2.Save();
write2.Dispose();
}
using (var read2 = new ZipFile("testarc.zip"))
{
Assert.AreEqual(1, read2.Entries.Where(e => !e.IsDirectory).Count());
Assert.AreEqual(1, read2.Entries.Where(e => e.IsDirectory).Count());
}
}
示例4: Password_UnsetEncryptionAfterSetPassword_wi13909_ZF
public void Password_UnsetEncryptionAfterSetPassword_wi13909_ZF()
{
// Verify that unsetting the Encryption property after
// setting a Password results in no encryption being used.
// This method tests ZipFile.
string unusedPassword = TestUtilities.GenerateRandomPassword();
int numTotalEntries = _rnd.Next(46)+653;
string zipFileToCreate = "UnsetEncryption.zip";
using (var zip = new ZipFile())
{
zip.Password = unusedPassword;
zip.Encryption = EncryptionAlgorithm.None;
for (int i=0; i < numTotalEntries; i++)
{
if (_rnd.Next(7)==0)
{
string entryName = String.Format("{0:D5}", i);
zip.AddDirectoryByName(entryName);
}
else
{
string entryName = String.Format("{0:D5}.txt", i);
if (_rnd.Next(12)==0)
{
var block = TestUtilities.GenerateRandomAsciiString() + " ";
string contentBuffer = String.Format("This is the content for entry {0}", i);
int n = _rnd.Next(6) + 2;
for (int j=0; j < n; j++)
contentBuffer += block;
byte[] buffer = System.Text.Encoding.ASCII.GetBytes(contentBuffer);
zip.AddEntry(entryName, contentBuffer);
}
else
zip.AddEntry(entryName, Stream.Null);
}
}
zip.Save(zipFileToCreate);
}
BasicVerifyZip(zipFileToCreate);
}
示例5: 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);
//.........这里部分代码省略.........