C# 根目录是什么,或者如何在DotNetZip中设置目录
DotNetZip从哪里获得用于保存的根目录。所有保存示例都不显示目录 我的目标是递归一个文件夹和子文件夹。在每个文件夹中,我想将所有文件压缩到一个压缩文件中,并删除源文件C# 根目录是什么,或者如何在DotNetZip中设置目录,c#,file-io,dotnetzip,C#,File Io,Dotnetzip,DotNetZip从哪里获得用于保存的根目录。所有保存示例都不显示目录 我的目标是递归一个文件夹和子文件夹。在每个文件夹中,我想将所有文件压缩到一个压缩文件中,并删除源文件 private void CopyFolder(string srcPath, string dstPath) { if (!Directory.Exists(dstPath)) Directory.CreateDirectory(dstPath); s
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);
}
}
private void CopyFolder(字符串srcPath、字符串dstPath)
{
如果(!Directory.Exists(dstPath))
CreateDirectory(dstPath);
string[]files=Directory.GetFiles(srcPath);
串味精;
字符串zipFileName;
使用(ZipFile z=new ZipFile(Path.Combine(srcPath,String.Format(“Archive{0:yyyyMMdd}.zip”,DateTime.Now)))
{
z、 ReadProgress+=新事件处理程序(z_ReadProgress);
foreach(文件中的字符串文件)
{
FileInfo fi=新的FileInfo(文件);
AddLog(String.Format(“添加{0}”,文件));
z、 AddFile(文件);
}
//z、 保存(Path.Combine(srcPath,String.Format(“Archive{0:yyyyMMdd}.zip”,DateTime.Now));
z、 Save();
如果(删除源)
{
foreach(文件中的字符串文件)
{
文件。删除(文件);
}
}
zipFileName=z.名称;
}
如果(!仅限压缩)
Copy(Path.Combine(srcPath,zipFileName),Path.Combine(dstPath,Path.GetFileName(zipFileName));
string[]folders=Directory.GetDirectories(sourcePath);
foreach(文件夹中的字符串文件夹)
{
字符串名称=Path.GetFileName(文件夹);
字符串dest=Path.Combine(dstPath,name);
控制台写入线(ln);
log.Add(ln);
msg=String.Format(“{3}{4}开始复制:{0}{4}目录:{1}{4}到:{2}”,DateTime.Now.ToString(“G”),name,dest,ln,Environment.NewLine);
AddLog(msg);
if(递归文件夹)
CopyFolder(文件夹,目的地);
msg=String.Format(“复制的目录:{0}{4}到:{1}\nAt:{2}{3}”,文件夹,dest,DateTime.Now.ToString(“G”),Environment.NewLine);
AddLog(msg);
}
}
它是相对于的路径,或者是绝对路径。这基本上是路径的标准过程
编辑:保存到的路径与zip中的目录无关。要么:
using(ZipFile f = new ZipFile("zip_dir/foo.zip"))
{
f.AddFile("foo.txt");
f.Save();
}
或
执行正确的操作,即在./zip_dir/foo.zip创建一个包含单个foo.txt文件的zip文件。当然,您可以在zip中创建子目录,但这是一个单独的问题。您正在使用的构造函数ZipFile z=new ZipFile(…)采用的路径是保存zip文件的位置。有点晚了,可能我们正在讨论两个不同的版本,但它可能仍然对其他人有帮助 您可以使用AddFile的第二个参数指定文件在存档中的存储位置。
@“\”
表示所有文件都将存储在根目录中
z.AddFile(file,@"\");
这是真的,我已经实现了,但它会在zip中创建子目录,我不希望这样。这是如何设置的?使用System.File.IO中类的静态方法?如果愿意,您可以使用Directory.SetCurrentDirectory,并具有所需的权限()。它不是必需的,因为您总是可以用另一种方式指定路径。
z.AddFile(file,@"\");