本文整理汇总了C#中ICSharpCode.SharpZipLib.Zip.ZipFile.AddDirectory方法的典型用法代码示例。如果您正苦于以下问题:C# ZipFile.AddDirectory方法的具体用法?C# ZipFile.AddDirectory怎么用?C# ZipFile.AddDirectory使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ICSharpCode.SharpZipLib.Zip.ZipFile
的用法示例。
在下文中一共展示了ZipFile.AddDirectory方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetFilesToZip
/// <summary>
/// Iterate thru all the filesysteminfo objects and add it to our zip file
/// </summary>
/// <param name="fileSystemInfosToZip">a collection of files/directores</param>
/// <param name="z">our existing ZipFile object</param>
private static void GetFilesToZip(FileSystemInfo[] fileSystemInfosToZip, ZipFile z)
{
//check whether the objects are null
if (fileSystemInfosToZip != null && z != null)
{
//iterate thru all the filesystem info objects
foreach (FileSystemInfo fi in fileSystemInfosToZip)
{
//check if it is a directory
if (fi is DirectoryInfo)
{
DirectoryInfo di = (DirectoryInfo)fi;
//add the directory
z.AddDirectory(di.FullName);
//drill thru the directory to get all
//the files and folders inside it.
GetFilesToZip(di.GetFileSystemInfos(), z);
}
else
{
//add it
z.Add(fi.FullName);
}
}
}
}
示例2: CompressDataInToFile
public void CompressDataInToFile(string directory, string password, string outputFile)
{
var fullFileListing = Directory.EnumerateFiles(directory, "*.*", SearchOption.AllDirectories);
var directories = Directory.EnumerateDirectories(directory, "*", SearchOption.AllDirectories);
_logger.Information("Creating ZIP File");
using (var zip = new ZipFile(outputFile))
{
zip.UseZip64 = UseZip64.On;
_logger.Information("Adding directories..");
foreach (var childDirectory in directories)
{
_logger.Information(string.Format("Adding {0}", childDirectory.Replace(directory, string.Empty)));
zip.BeginUpdate();
zip.AddDirectory(childDirectory.Replace(directory, string.Empty));
zip.CommitUpdate();
}
_logger.Information("Adding files..");
foreach (var file in fullFileListing)
{
_logger.Information(string.Format("Adding {0}", file.Replace(directory, string.Empty)));
zip.BeginUpdate();
zip.Add(file, file.Replace(directory, string.Empty));
zip.CommitUpdate();
}
_logger.Information("Setting password..");
zip.BeginUpdate();
zip.Password = password;
zip.CommitUpdate();
}
}
示例3: SetContent
void SetContent(string loc, byte[] content)
{
ZipFile zipFile = new ZipFile(FileLoc);
// Must call BeginUpdate to start, and CommitUpdate at the end.
zipFile.BeginUpdate();
if (loc.Contains('/'))
{
int i = loc.IndexOf('/');
string dir = loc.Remove(i + 1, loc.Length - i - 1);
if (zipFile.FindEntry(dir, true) < 0)
{
zipFile.AddDirectory(dir);
}
}
CustomStaticDataSource sds = new CustomStaticDataSource();
using (MemoryStream ms = new MemoryStream(content))
{
sds.SetStream(ms);
// If an entry of the same name already exists, it will be overwritten; otherwise added.
zipFile.Add(sds, loc);
// Both CommitUpdate and Close must be called.
zipFile.CommitUpdate();
zipFile.Close();
}
}
示例4: AddDirectory
void AddDirectory(string loc)
{
ZipFile zipFile = new ZipFile(FileLoc);
// Must call BeginUpdate to start, and CommitUpdate at the end.
zipFile.BeginUpdate();
zipFile.AddDirectory(loc);
// Both CommitUpdate and Close must be called.
zipFile.CommitUpdate();
zipFile.Close();
}
示例5: AddDirectory
void AddDirectory(string loc)
{
ZipFile zipFile = new ZipFile(this.zipArchive);
// Must call BeginUpdate to start, and CommitUpdate at the end.
zipFile.BeginUpdate();
zipFile.AddDirectory(loc);
// Both CommitUpdate and Close must be called.
zipFile.CommitUpdate();
zipFile.IsStreamOwner = false; zipFile.Close();
this.zipArchive.Position = 0;
}
示例6: AddDirectoryFilesToZip
/// <summary>
/// Recursively adds folders and files to archive
/// </summary>
/// <param name="root">The root directory.</param>
/// <param name="zipArchive"></param>
/// <param name="sourceDirectory"></param>
/// <param name="recurse"></param>
private static void AddDirectoryFilesToZip(string root,ZipFile zipArchive, string sourceDirectory, bool recurse)
{
// Add this directory
zipArchive.AddDirectory(root + "/" + sourceDirectory);
// Recursively add sub-folders
if (recurse)
{
string[] directories = Directory.GetDirectories(sourceDirectory);
foreach (string directory in directories)
{
AddDirectoryFilesToZip(root,zipArchive, directory, recurse);
}
}
// Add files
string[] filenames = Directory.GetFiles(sourceDirectory);
foreach (string filename in filenames)
{
string name = filename;
if (name.StartsWith(@".\"))
{
name = name.Substring(2, name.Length - 2);
}
if (!String.IsNullOrEmpty(root))
{
name = root + @"\" + name;
}
Console.WriteLine("{0} -> {1}", filename, name);
zipArchive.Add(filename, name);
}
}
示例7: GenerateImage
/// <summary>
/// Generates the image.
/// </summary>
/// <remarks>CFI, 2012-02-24</remarks>
private void GenerateImage()
{
#region Check parameter:
if (!Directory.Exists(textBoxInputFilePath.Text))
throw new DirectoryNotFoundException("The input directory was not found!");
if (!Directory.Exists(Path.GetDirectoryName(textBoxOutputFile.Text)))
throw new DirectoryNotFoundException("The output path is invalid!");
if (textBoxStickName.Text.Length > 11 || textBoxStickName.Text == string.Empty)
throw new ArgumentException("Invalid Stick name!");
if (textBoxStickID.Text.Length != 9 || textBoxStickID.Text[4] != '-')
throw new ArgumentException("Invalid Stick ID!");
#endregion
string outputFilePath = Path.ChangeExtension(textBoxOutputFile.Text, ".MLifterStick");
#region Create Zip:
Stream output = File.Create(outputFilePath);
ZipOutputStream zipStream = new ZipOutputStream(output);
ZipFile zip = new ZipFile(output);
#endregion
#region Fill Zip:
zip.BeginUpdate();
int pos = 1;
string[] files = Directory.GetFiles(textBoxInputFilePath.Text, "*.*", SearchOption.AllDirectories);
List<string> folders = new List<string>();
foreach (string file in files)
{
toolStripStatusLabelMessage.Text = string.Format("Adding file {0} of {1}...", pos, files.Length);
toolStripProgressBarStatus.Value = Convert.ToInt32(pos++ * 1.0 / files.Length * 100);
Application.DoEvents();
string dir = Path.GetDirectoryName(file).Remove(0, textBoxInputFilePath.Text.Length);
string zipFile = Path.Combine(dir, Path.GetFileName(file));
if (dir.Length > 0)
{
if (!folders.Contains(dir))
{
zip.AddDirectory(dir);
folders.Add(dir);
}
}
zip.Add(new FileDataSource(file), zipFile);
}
toolStripStatusLabelMessage.Text = "Saving image. THIS COULD RUN VERY LONG - PLEASE WAIT!";
Application.DoEvents();
zip.CommitUpdate();
#endregion
VolumeDataStorage vds = new VolumeDataStorage();
#region setting file attributes:
foreach (string file in files)
{
string dir = Path.GetDirectoryName(file).Remove(0, textBoxInputFilePath.Text.Length);
if (dir.StartsWith("\\"))
dir = dir.Remove(0, 1);
string zipFile = Path.Combine(dir, Path.GetFileName(file)).Replace(@"\", "/");
ZipEntry entry = zip.GetEntry(zipFile);
if (entry == null)
continue;
FileAttributes attr = (new FileInfo(file)).Attributes;
if (!(attr == FileAttributes.Archive || attr == FileAttributes.Normal))
vds.FileAttributeList.Add(entry.Name, attr);
toolStripStatusLabelMessage.Text = "Setting file FileAttributes to: " + zipFile;
}
foreach (string folder in folders)
{
FileAttributes attr = (new DirectoryInfo(Path.Combine(textBoxInputFilePath.Text, folder))).Attributes;
if (!(attr == FileAttributes.Archive || attr == FileAttributes.Normal || attr == FileAttributes.Directory))
vds.FolderAttributeList.Add(folder, attr);
toolStripStatusLabelMessage.Text = "Setting folder FileAttributes to: " + folder;
}
#endregion
#region Set Comment:
vds.VolumeLabel = textBoxStickName.Text;
vds.VolumeSerial = textBoxStickID.Text;
string comment = VolumeDataStorage.SerializeData(vds);
File.WriteAllText(Path.Combine(Application.StartupPath, "Comment.txt"), comment);
zip.BeginUpdate();
//.........这里部分代码省略.........