lovebeerry 2022-01-07 10:48 采纳率: 0%
浏览 42
已结题

asp.net core mvc 多文件压缩,并下载压缩文件,镜像发布后,下载的压缩文件出现多层

代码发布在本地或者发布到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");
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 1月15日
    • 创建了问题 1月7日

    悬赏问题

    • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
    • ¥20 关于web前端如何播放二次加密m3u8视频的问题
    • ¥20 spring boot集成mqtt的使用问题
    • ¥15 使用百度地图api 位置函数报错?
    • ¥15 metamask如何添加TRON自定义网络
    • ¥66 关于川崎机器人调速问题
    • ¥15 winFrom界面无法打开
    • ¥30 crossover21 ARM64版本安装软件问题
    • ¥15 mymetaobjecthandler没有进入
    • ¥15 mmo能不能做客户端怪物