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

How to remove guid from file name when creating zip file?

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

 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
}

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);

从@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);
                    }
        }

您应该使用存档和 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);
}