What is the root directory or how to set a directory in DotNetZip
where DotNetZip gets the root directory to save. All save examples do not display the directory.
My goal is to overwrite the folder and subfolders. In each folder, I want to zip all files into one ZIP and delete the original files.
private void CopyFolder(string srcPath, string dstPath)
{
if (!Directory.Exists(dstPath))
Directory.CreateDirectory(dstPath);
string[] files = Directory.GetFiles(srcPath);
string msg;
string zipFileName;
using (ZipFile z = new ZipFile(Path.Combine(srcPath,String.Format("Archive{0:yyyyMMdd}.zip", DateTime.Now))))
{
z.ReadProgress += new EventHandler<ReadProgressEventArgs>(z_ReadProgress);
foreach (string file in files)
{
FileInfo fi = new FileInfo(file);
AddLog(String.Format("Adding {0}", file));
z.AddFile(file);
}
//z.Save(Path.Combine(srcPath, String.Format("Archive{0:yyyyMMdd}.zip", DateTime.Now)));
z.Save();
if (deleteSource)
{
foreach (string file in files)
{
File.Delete(file);
}
}
zipFileName = z.Name;
}
if (!compressOnly)
File.Copy(Path.Combine(srcPath,zipFileName), Path.Combine(dstPath, Path.GetFileName(zipFileName)));
string[] folders = Directory.GetDirectories(sourcePath);
foreach (string folder in folders)
{
string name = Path.GetFileName(folder);
string dest = Path.Combine(dstPath, name);
Console.WriteLine(ln);
log.Add(ln);
msg = String.Format("{3}{4}Start Copy: {0}{4}Directory: {1}{4}To: {2}", DateTime.Now.ToString("G"), name, dest, ln, Environment.NewLine);
AddLog(msg);
if (recurseFolders)
CopyFolder(folder, dest);
msg = String.Format("Copied Directory: {0}{4}To: {1}\nAt: {2}{3}", folder, dest, DateTime.Now.ToString("G"), Environment.NewLine);
AddLog(msg);
}
}
+2
a source to share
3 answers
This is a path relative to the current working directory or an absolute path. This is the standard procedure for paths.
EDIT: The path you are saving has nothing to do with the directories inside the zip. Or:
using(ZipFile f = new ZipFile("zip_dir/foo.zip"))
{
f.AddFile("foo.txt");
f.Save();
}
or
using(ZipFile f = new ZipFile())
{
f.AddFile("foo.txt");
f.Save("zip_dir/foo.zip");
}
does the right thing, which is create a zip file in. / zip_dir / foo.zip containing one file foo.txt. You can of course create subdirectories in zip, but that's a separate issue.
0
a source to share