本文整理汇总了C#中ZipFile.AddFiles方法的典型用法代码示例。如果您正苦于以下问题:C# ZipFile.AddFiles方法的具体用法?C# ZipFile.AddFiles怎么用?C# ZipFile.AddFiles使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ZipFile
的用法示例。
在下文中一共展示了ZipFile.AddFiles方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Password_CheckZipPassword_wi13664
public void Password_CheckZipPassword_wi13664()
{
string[] passwords = { null,
"Password!",
TestUtilities.GenerateRandomPassword(),
"_" };
string dirToZip = Path.Combine(TopLevelDir, "zipthis");
int subdirCount;
int entries = TestUtilities.GenerateFilesOneLevelDeep
(TestContext, "wi13664", dirToZip, null, out subdirCount);
string[] filesToZip = Directory.GetFiles("zipthis", "*.*", SearchOption.AllDirectories);
Assert.AreEqual<int>(filesToZip.Length, entries,
"Incorrect number of entries in the directory.");
for (int j = 0; j < passwords.Length; j++)
{
string zipFileToCreate = Path.Combine(TopLevelDir, String.Format("Password_CheckZipPassword_wi13664-{0}.zip", j));
// Create the zip archive
using (ZipFile zip1 = new ZipFile())
{
zip1.Password = passwords[j];
zip1.AddFiles(filesToZip, true, "");
zip1.Save(zipFileToCreate);
}
var r = ZipFile.CheckZipPassword(zipFileToCreate, passwords[j]);
Assert.IsTrue(r, "Bad password in round {0}", j);
}
}
示例2: SilentDeletion_wi10639
public void SilentDeletion_wi10639()
{
string zipFileToCreate = "SilentDeletion.zip";
string dirToZip = "dirToZip";
string extractDir = "extracted";
string password = TestUtilities.GenerateRandomPassword();
string wrongPassword = "passworD";
var files = TestUtilities.GenerateFilesFlat(dirToZip);
TestContext.WriteLine("Creating the zip.");
using (var zip = new ZipFile())
{
zip.Password = password;
zip.AddFiles(files, dirToZip);
zip.Save(zipFileToCreate);
}
TestContext.WriteLine("Extract one file with wrong password.");
// pick a random entry to extract
int ix = -1;
string extractedFile = null;
// perform two passes: first with correct password to extract the
// file. 2nd with incorrect password to see if the file is
// deleted.
Directory.CreateDirectory(extractDir);
for (int i=0; i < 2; i++)
{
try
{
using (var zip = ZipFile.Read(zipFileToCreate))
{
if (i==0)
{
do
{
ix = this._rnd.Next(zip.Entries.Count);
}
while (zip[ix].IsDirectory);
TestContext.WriteLine("Selected entry: {0}", zip[ix].FileName);
extractedFile = Path.Combine(extractDir, zip[ix].FileName.Replace("/","\\"));
TestContext.WriteLine("name for extracted file: {0}", extractedFile);
Assert.IsFalse(File.Exists(extractedFile), "The file exists.");
}
TestContext.WriteLine("Cycle {0}: ExtractWithPassword()", i);
zip[ix].ExtractWithPassword(extractDir,
ExtractExistingFileAction.OverwriteSilently,
(i==0)? password : wrongPassword);
}
}
catch (BadPasswordException)
{
// only swallow exceptions on the first go-round
if (i==0) throw;
}
Assert.IsTrue(File.Exists(extractedFile), "Cycle {0}: The extracted file does not exist.", i);
}
}
示例3: Error_AddFile_Twice
public void Error_AddFile_Twice()
{
int i;
// select the name of the zip file
string zipFileToCreate = Path.Combine(TopLevelDir, "Error_AddFile_Twice.zip");
// create the subdirectory
string subdir = Path.Combine(TopLevelDir, "files");
Directory.CreateDirectory(subdir);
// create a bunch of files
int numFilesToCreate = _rnd.Next(23) + 14;
for (i = 0; i < numFilesToCreate; i++)
TestUtilities.CreateUniqueFile("bin", subdir, _rnd.Next(10000) + 5000);
// Create the zip archive
Directory.SetCurrentDirectory(TopLevelDir);
using (ZipFile zip1 = new ZipFile(zipFileToCreate))
{
zip1.StatusMessageTextWriter = System.Console.Out;
string[] files = Directory.GetFiles(subdir);
zip1.AddFiles(files, "files");
zip1.Save();
}
// this should fail - adding the same file twice
using (ZipFile zip2 = new ZipFile(zipFileToCreate))
{
zip2.StatusMessageTextWriter = System.Console.Out;
string[] files = Directory.GetFiles(subdir);
for (i = 0; i < files.Length; i++)
zip2.AddFile(files[i], "files");
zip2.Save();
}
}
示例4: _Internal_ExtractExisting
private void _Internal_ExtractExisting(int flavor)
{
string zipFileToCreate = Path.Combine(TopLevelDir, String.Format("Error-Extract-ExistingFileWithoutOverwrite-{0}.zip", flavor));
string testBin = TestUtilities.GetTestBinDir(CurrentDir);
string resourceDir = Path.Combine(testBin, "Resources");
Assert.IsTrue(Directory.Exists(resourceDir));
Directory.SetCurrentDirectory(TopLevelDir);
var filenames = Directory.GetFiles(resourceDir);
using (ZipFile zip = new ZipFile(zipFileToCreate))
{
zip.AddFiles(filenames, "");
zip.Comment = "This is a Comment On the Archive";
zip.Save();
}
Assert.AreEqual<int>(TestUtilities.CountEntries(zipFileToCreate), filenames.Length,
"The zip file created has the wrong number of entries.");
// Extract twice: the first time should succeed.
// The second, should fail, because of a failed file overwrite.
// Unless flavor==3, in which case we overwrite silently.
for (int k = 0; k < 2; k++)
{
using (ZipFile zip = ZipFile.Read(zipFileToCreate))
{
if (flavor > 10)
zip.ExtractProgress += OverwriteDecider;
for (int j = 0; j < filenames.Length; j++)
{
ZipEntry e = zip[Path.GetFileName(filenames[j])];
if (flavor == 4)
e.Extract("unpack");
else
e.Extract("unpack", (ExtractExistingFileAction) flavor);
}
}
}
}
示例5: Spanned_InfoZip_Unzip_wi13691
[Timeout(5 * 60*1000)] // to protect against stuck file locks
public void Spanned_InfoZip_Unzip_wi13691()
{
if (!InfoZipIsPresent)
throw new Exception("InfoZip is not present");
TestContext.WriteLine("Creating fodder files... {0}",
DateTime.Now.ToString("G"));
var contentDir = CreateSomeFiles();
var filesToAdd = new List<String>(Directory.GetFiles(contentDir));
int[] segSizes = { 128, 256, 512 };
// Save as segmented, then read/extract with winzip unzip
// for various segment sizes.
for (int k=0; k < segSizes.Length; k++)
{
string trialDir = String.Format("trial.{0}", k);
Directory.CreateDirectory(trialDir);
string zipFile1 = Path.Combine(trialDir, "InitialSave."+k+".zip");
TestContext.WriteLine("");
TestContext.WriteLine("Creating zip... T({0})...{1}",
k, DateTime.Now.ToString("G"));
using (var zip1 = new ZipFile())
{
zip1.AddFiles(filesToAdd, "");
zip1.MaxOutputSegmentSize = segSizes[k] * 1024;
zip1.Save(zipFile1);
}
TestContext.WriteLine("");
TestContext.WriteLine("Extracting...");
string extractDir = Path.Combine(trialDir,"extract");
Directory.CreateDirectory(extractDir);
string args = String.Format("{0} -d {1}",
zipFile1,
extractDir);
this.Exec(infoZipUnzip, args);
string[] filesUnzipped = Directory.GetFiles(extractDir);
Assert.AreEqual<int>(filesToAdd.Count, filesUnzipped.Length,
"Incorrect number of files extracted, trail {0}", k);
}
}
示例6: Spanned_Resave_wi13915
[Timeout(5 * 60*1000)] // to protect against stuck file locks
public void Spanned_Resave_wi13915()
{
TestContext.WriteLine("Creating fodder files... {0}",
DateTime.Now.ToString("G"));
var contentDir = CreateSomeFiles();
var filesToAdd = new List<String>(Directory.GetFiles(contentDir));
int[] segSizes = { 128, 256, 512 };
// Three passes:
// pass 1: save as regular, then resave as segmented.
// pass 2: save as segmented, then resave as regular.
// pass 3: save as segmented, then resave as another segmented archive.
for (int m=0; m < 3; m++)
{
// for various segment sizes
for (int k=0; k < segSizes.Length; k++)
{
string trialDir = String.Format("trial.{0}.{1}", m, k);
Directory.CreateDirectory(trialDir);
string zipFile1 = Path.Combine(trialDir, "InitialSave."+m+"."+k+".zip");
string zipFile2 = Path.Combine(trialDir, "Updated."+m+"."+k+".zip");
TestContext.WriteLine("");
TestContext.WriteLine("Creating zip... T({0},{1})...{2}",
m, k, DateTime.Now.ToString("G"));
using (var zip1 = new ZipFile())
{
zip1.AddFiles(filesToAdd, "");
if (m!=0)
zip1.MaxOutputSegmentSize = segSizes[k] * 1024;
zip1.Save(zipFile1);
}
TestContext.WriteLine("");
TestContext.WriteLine("Re-saving...");
using (var zip2 = ZipFile.Read(zipFile1))
{
if (m==0)
zip2.MaxOutputSegmentSize = segSizes[k] * 1024;
else if (m==2)
zip2.MaxOutputSegmentSize = 1024 * (3 * segSizes[k])/2;
zip2.Save(zipFile2);
}
TestContext.WriteLine("");
TestContext.WriteLine("Extracting...");
string extractDir = Path.Combine(trialDir,"extract");
Directory.CreateDirectory(extractDir);
using (var zip3 = ZipFile.Read(zipFile2))
{
foreach (var e in zip3)
{
TestContext.WriteLine(" {0}", e.FileName);
e.Extract(extractDir);
}
}
string[] filesUnzipped = Directory.GetFiles(extractDir);
Assert.AreEqual<int>(filesToAdd.Count, filesUnzipped.Length,
"Incorrect number of files extracted.");
}
}
}
示例7: Spanned_Resave_Resave
public void Spanned_Resave_Resave()
{
TestContext.WriteLine("Creating fodder files... {0}",
DateTime.Now.ToString("G"));
var contentDir = CreateSomeFiles();
var filesToAdd = new List<String>(Directory.GetFiles(contentDir));
int[] segSizes = { 128, 256, 512 };
// for various segment sizes
for (int k = 0; k < segSizes.Length; k++)
{
string trialDir = String.Format("trial.{0}", k);
Directory.CreateDirectory(trialDir);
string zipFile1 = Path.Combine(trialDir, "InitialSave." + k + ".zip");
string zipFile2 = Path.Combine(trialDir, "Updated." + k + ".zip");
string zipFile3 = Path.Combine(trialDir, "Again." + k + ".zip");
TestContext.WriteLine("");
TestContext.WriteLine("Creating zip... T({0})...{1}", k, DateTime.Now.ToString("G"));
using (var zip1 = new ZipFile())
{
zip1.AddFiles(filesToAdd, "");
zip1.MaxOutputSegmentSize = segSizes[k] * 1024;
zip1.Save(zipFile1);
}
TestContext.WriteLine("");
TestContext.WriteLine("Re-saving...");
using (var zip2 = FileSystemZip.Read(zipFile1))
{
zip2.MaxOutputSegmentSize = segSizes[k] * 1024;
zip2.Save(zipFile2);
}
TestContext.WriteLine("");
TestContext.WriteLine("Deleting initial archive...");
string[] filesToDelete = Directory.GetFiles(trialDir, "InitialSave.*");
foreach (var file in filesToDelete)
{
File.Delete(file);
}
TestContext.WriteLine("");
TestContext.WriteLine("Re-saving again...");
using (var zip3 = FileSystemZip.Read(zipFile2))
{
zip3.MaxOutputSegmentSize = segSizes[k] * 1024;
zip3.Save(zipFile3);
}
TestContext.WriteLine("");
TestContext.WriteLine("Extracting...");
string extractDir = Path.Combine(trialDir, "extract");
Directory.CreateDirectory(extractDir);
using (var zip4 = FileSystemZip.Read(zipFile3))
{
foreach (var e in zip4)
{
TestContext.WriteLine(" {0}", e.FileName);
e.Extract(extractDir);
}
}
string[] filesUnzipped = Directory.GetFiles(extractDir);
Assert.AreEqual<int>(filesToAdd.Count, filesUnzipped.Length,
"Incorrect number of files extracted.");
}
}