代码发布在本地或者发布到iis上,没有出现这个问题,但是使用镜像发布后,压缩文件出现多层目录
压缩文件使用的是using ICSharpCode.SharpZipLib.Zip;
代码如下:
public static void ZipFile(DirectoryInfo directory, string zipedFile, int Level)
{
try
{
if (File.Exists(zipedFile))
{
File.Delete(zipedFile);
}
var fileToZipS = directory.GetFiles();
foreach (var fileToZip in fileToZipS)
{
string fileName = fileToZip.FullName;
//如果文件没有找到,则报错
if (!File.Exists(fileName))
{
throw new System.IO.FileNotFoundException("指定要压缩的文件: " + fileName + " 不存在!");
}
}
//进行文件压缩
using (FileStream ZipFile = File.Create(zipedFile))
{
using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile))
{
foreach (var fileToZip in fileToZipS)
{
string fileName = fileToZip.FullName.Substring(fileToZip.FullName.LastIndexOf("\\") + 1);
using (FileStream fs = File.OpenRead(fileToZip.FullName))
{
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
ZipEntry ZipEntry = new ZipEntry(fileName);
ZipStream.PutNextEntry(ZipEntry);
ZipStream.SetLevel(Level);
ZipStream.Write(buffer, 0, buffer.Length);
}
}
ZipStream.Finish();
ZipStream.Close();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
public IActionResult DownLoad()
{
var zipPath = ConvertFilesPath + "/project.zip";
ZipHelper.ZipFile(new DirectoryInfo(ConvertFilesPath), zipPath, 2);
var fileStream = new FileStream(zipPath, FileMode.Open, FileAccess.Read);
return File(fileStream, GetContentType(zipPath), "project.zip");
}