我尝试压缩文件夹中的所有文件,然后将它们保存到另一个文件夹中。
错误:
该名称指定现有目录。请指定文件名。参数名称:fileName
的AppConfig:
<appSettings>
<add key="ZipLocation" value="c:\example\start"/>
<add key="ZipSaveLocation" value="c:\example\extract"/>
</appSettings>
代码:
//using Ionic.Zip;
static void Main(string[] args)
{
string startPath = ConfigurationManager.AppSettings["ZipLocation"].ToString();
string zipPath = ConfigurationManager.AppSettings["ZipSaveLocation"].ToString();
int key = Convert.ToInt32(Console.ReadLine());
try
{
using (ZipFile zip = new ZipFile())
{
String[] filenames = System.IO.Directory.GetFiles(startPath);
foreach (String filename in filenames)
{
Console.WriteLine("Adding {0}...", filename);
ZipEntry e = zip.AddFile(filename);
e.Comment = "bla bla.";
}
zip.Comment = String.Format("This zip archive was created by the CreateZip example application on machine '{0}'", System.Net.Dns.GetHostName());
zip.Save(zipPath);
}
}
catch (Exception)
{
throw;
}
}
答案 0 :(得分:1)
你有这个:
string zipPath = ConfigurationManager.AppSettings["ZipSaveLocation"].ToString();
然后你称之为:
zip.Save(zipPath);
zipPath不是文件,它是一个文件夹。
通过执行以下操作修复代码:
zip.Save(Path.Combine(zipPath,"newZipFile.zip"));
答案 1 :(得分:1)
您必须提供的文件不仅仅是路径:
例如,将c:\example\extract
更改为c:\example\extract\extract.zip
。
修改强>
使用以下内容:
zip.Save(Path.Combine(zipPath, Path.GetDirectoryName(startPath)));