如何在创建zip文件时从文件名中删除guid

本文关键字:文件名 删除 guid 文件 创建 zip | 更新日期: 2023-09-27 18:19:47

当用户上传多个文档时,我会将它们的文件存储在我的项目中,如下所示:

 Guid id;
 id = Guid.NewGuid();
 string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"),
 Path.GetFileName(id + item.FileName));
 item.SaveAs(filePath);

所以文件在我的项目中是这样保存的:

  1. 1250a2d5-cd40-4bcc-a979-9d6f2cd62b9fLog.txt
  2. bdb31966-e3c4-4344-b02c-305c0eb0fa0aLogging.txt

现在,当创建zip文件时,我在提取zip文件时得到了与此文件相同的名称,但我不希望在用户下载文件后在我的文件名中使用guid。

但是,我试图从文件名中删除guid,但出现错误System.IO.FileNotFoundException

这是我的代码:

using (var zip = new ZipFile())
{
    var str = new string[] { "1250a2d5-cd40-4bcc-a979-9d6f2cd62b9fLog.txt", "bdb31966-e3c4-4344-b02c-305c0eb0fa0aLogging.txt" }; //file name are Log.txt and Logging.txt
    string[] str1 = str .Split(',');
    foreach (var item in str1)
    {
        string filePath = Server.MapPath("~/Uploads/" + item.Substring(36));//as guid are of 36 digits
        zip.AddFile(filePath, "files");
    }
    zip.Save(memoryStream);//Getting error here
}

如何在创建zip文件时从文件名中删除guid

ZipFile正在抛出异常,因为它在磁盘上找不到文件,因为你给了它一个不存在的文件名(通过执行.Substring())。要使其工作,你必须使用file.Copy重命名文件,并使用新文件名,然后将相同的文件名提供给Zip.AddFile().

  var orgFileName = "1250a2d5-cd40-4bcc-a979-9d6f2cd62b9fLog.txt";
  var newFileName = orgFileName.Substring (36);
  File.Copy (orgFileName, newFileName, true);
  zip.AddFile (newFileName);

您应该使用archive和ArchiveEntry。粗略的代码狙击如何做(我不测试它):

    using(var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true)) {
        {
            //using(var zip = new ZipFile()) {
            var str = new string[] { "1250a2d5-cd40-4bcc-a979-9d6f2cd62b9fLog.txt", "bdb31966-e3c4-4344-b02c-305c0eb0fa0aLogging.txt" }; //file name are Log.txt and Logging.txt
                                                                                                                                         //string[] str = str.Split(',');
            foreach(var item in str) {
                using(var entryStream = archive.CreateEntry("files/" + item.Substring(36)).Open()) {
                    string filePath = Server.MapPath("~/Uploads/" + item);
                    var content = File.ReadAllBytes(filePath);
                    entryStream.Write(content, 0, content.Length);
                }
            }
        }
    }

使用DotNetZip:的示例

using (ZipFile zip = new ZipFile())
{
var str = new string[] { "1250a2d5-cd40-4bcc-a979-9d6f2cd62b9fLog.txt", "bdb31966-e3c4-4344-b02c-305c0eb0fa0aLogging.txt" };
            foreach(var item in str) {
                    string filePath = Server.MapPath("~/Uploads/" + item);
                    var content = File.ReadAllLines(filePath);
                    ZipEntry e = zip.AddEntry("files/" + item.Substring(36), content);
                }
            }  
zip.Save(memoryStream);
}

从@kevin那里获得消息来源,我设法解决了这个问题:

    List<string> newfilename1 = new List<string>();
        using (var zip = new ZipFile())
        {
            var str = new string[] { "1250a2d5-cd40-4bcc-a979-9d6f2cd62b9fLog.txt", "bdb31966-e3c4-4344-b02c-305c0eb0fa0aLogging.txt" }; //file name are Log.txt and Logging.txt
            string[] str1 = str .Split(',');
            foreach (var item in str1)
            {
                  string filePath = Server.MapPath("~/Uploads/" + item);
                            string newFileName = Server.MapPath("~/Uploads/" + item.Substring(36));
                            newfilename1.Add(newFileName);
                            System.IO.File.Copy(filePath,newFileName);
                            zip.AddFile(newFileName,"");
            }
            zip.Save(memoryStream);
 foreach (var item in newfilename1)
                    {
                        System.IO.File.Delete(item);
                    }
        }