How to tell FetchContent to keep archive directory structure

Hello,

I am downloading some header files from a remote URL and unzipping them using FetchContent. This works as expected, except for the fact that the zip files directory structure isn’t being kept the same.

For example, if I take a look at the zip file I am downloading in 7zip, the directory structure looks like this:

- foo/
  - bar/
    - example.h

But when using FetchContent like so:

FetchContent_Declare(
  package_headers
  URL ${header_url}
)
FetchContent_MakeAvailable(package_headers)

The directory structure ends up like this

- build/
  - desktop_debug_shared
    - _deps
      - package_headers-src
        - bar/
          - example.h

This only seems to happen if the zip has only a folder in the top level dir and nothing else.

I would like to keep the top-level folder to keep the include path the same.

Any help is appreciated!

The code that implements that behavior is in extractfile.cmake.in. There’s currently no option to prevent it from discarding a single top level directory. As a workaround, you can use the DOWNLOAD_NO_EXTRACT option to prevent extraction and then perform extraction manually yourself. I acknowledge that’s not ideal, but it is a workaround available to you since CMake 3.6.

Thank you for tracking this behavior. I appreciate the workaround!