DotNetZip: How to extract files, but ignoring the path in the zipfile?

后端 未结 4 1676
借酒劲吻你
借酒劲吻你 2021-02-14 03:33

Trying to extract files to a given folder ignoring the path in the zipfile but there doesn\'t seem to be a way.

This seems a fairly basic requirement given all the other

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-14 03:53

    You can use the overload that takes a stream as a parameter. In this way you have full control of path where the files will be extracted to.

    Example:

    using (ZipFile zip = new ZipFile(ZipPath))
    {
         foreach (ZipEntry e in zip)
         {
            string newPath = Path.Combine(FolderToExtractTo, e.FileName);
    
            if (e.IsDirectory)
            {
               Directory.CreateDirectory(newPath);
            }
            else
            {
              using (FileStream stream = new FileStream(newPath, FileMode.Create))
                 e.Extract(stream);
            }
         }
    }
    

提交回复
热议问题