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

只谈情不闲聊 提交于 2019-12-12 02:44:15

问题


When user uploads multiple documents I am storing their files in my project like this:

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

So files are saved like this in my project:

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

Now when creating zip files I am getting same name of this files when extracting zip files but I don't want guid in my file name after user downloads file.

However I have tried to remove guid from my file name but getting error System.IO.FileNotFoundException.

This is my code:

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
}

Can anybody help me with this?


回答1:


ZipFile is throwing an exception because it can't find the file on disk as you have given it a name of a file that does not exist (by doing a .Substring()). To make it work you would have to rename the file using File.Copy with your new file name and then give that same file name to Zip.AddFile().

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



回答2:


You should use archive and ArchiveEntry. The rough code snipets how to do it (i don't test it):

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

sample for using 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);
}



回答3:


Taking source from @kevin answer i have manage to solve this:

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


来源:https://stackoverflow.com/questions/33407205/how-to-remove-guid-from-file-name-when-creating-zip-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!