SharpLibZip: Add file without path

前端 未结 3 1059
时光说笑
时光说笑 2021-02-19 02:57

I\'m using the following code, using the SharpZipLib library, to add files to a .zip file, but each file is being stored with its full path. I need to only store the file, in t

3条回答
  •  梦谈多话
    2021-02-19 03:23

    My solution was to set the NameTransform object property of the ZipFile to a ZipNameTransform with its TrimPrefix set to the directory of the file. This causes the directory part of the entry names, which are full file paths, to be removed.

    public static void ZipFolderContents(string folderPath, string zipFilePath)
    {
        string[] files = Directory.GetFiles(folderPath);
        using (ZipFile zipFile = ZipFile.Create(zipFilePath))
        {
            zipFile.NameTransform = new ZipNameTransform(folderPath);
            foreach (string file in files)
            {
                zipFile.BeginUpdate();
                zipFile.Add(file);
                zipFile.CommitUpdate();
            }
        }
    }
    

    What's cool is the the NameTransform property is of type INameTransform, allowing customisation of the name transforms.

提交回复
热议问题