# file ARCHIVE\_CREATE uses relative paths

**URL:** https://discourse.cmake.org/t/file-archive-create-uses-relative-paths/9013
**Category:** Usage
**Created:** [September 19, 2023, 9:56pm UTC](https://discourse.cmake.org/t/file-archive-create-uses-relative-paths/9013 "2023-09-19T21:56:37Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![K20shores](https://discourse.cmake.org/user_avatar/discourse.cmake.org/k20shores/32/2987_2.png) [@K20shores](https://discourse.cmake.org/u/K20shores)
#### Post date: [September 19, 2023, 9:56pm UTC](https://discourse.cmake.org/t/file-archive-create-uses-relative-paths/9013/1 "2023-09-19T21:56:37Z")

</div>

I’m trying to create some zip files with cmake. I’m either using the archive creation incorrectly or there’s a bug because the internal paths of my zip files are always relative paths.

With this directory structure:

```auto
:: tree
.
├── CMakeLists.txt
└── dir
    ├── a
    │ └── a.txt
    └── b
        └── b.txt

```

And this CMakeLists.txt file:

```auto
cmake_minimum_required(VERSION 3.21)

project(
  test
  LANGUAGES CXX
)

file(GLOB subdirectories LIST_DIRECTORIES true "${CMAKE_CURRENT_SOURCE_DIR}/dir/*")

foreach(subdirectory ${subdirectories})
    if(IS_DIRECTORY ${subdirectory})
        # Extract the directory name from the path
        get_filename_component(directory_name ${subdirectory} NAME)

        # set the output file name
        set(output_archive "${CMAKE_CURRENT_SOURCE_DIR}/${directory_name}.zip")

        message(STATUS "subdirectory: ${subdirectory}")

        # create the archive
        file(ARCHIVE_CREATE
          OUTPUT "${output_archive}"
          PATHS 
              "${subdirectory}"
          FORMAT "zip"
        )
        message(STATUS "Created archive: ${output_archive}")
    endif()
endforeach()

```

I end up with two zip files that cannot be unzipped without telling unzip to remove the relative paths, unless in the build directory. This is a problem since applications like Finder cannot open archives like this.

```auto
:: mkdir build && cd build

:: cmake ..
-- The CXX compiler identification is AppleClang 14.0.3.14030022
...
-- Build files have been written to: ...

:: ls ../
CMakeLists.txt a.zip b.zip build dir

:: cd ..

:: zipinfo a.zip 
Archive: a.zip
Zip file size: 369 bytes, number of entries: 2
drwxr-xr-x 2.0 unx 0 bx stor 23-Sep-19 16:41 ../dir/a/
-rw-r--r-- 2.0 unx 3 bX defN 23-Sep-19 16:41 ../dir/a/a.txt
2 files, 3 bytes uncompressed, 5 bytes compressed: -66.7%

:: zipinfo b.zip 
Archive: b.zip
Zip file size: 369 bytes, number of entries: 2
drwxr-xr-x 2.0 unx 0 bx stor 23-Sep-19 16:41 ../dir/b/
-rw-r--r-- 2.0 unx 3 bX defN 23-Sep-19 16:41 ../dir/b/b.txt
2 files, 3 bytes uncompressed, 5 bytes compressed: -66.7%

:: unzip a.zip 
Archive: a.zip
warning: skipped "../" path component(s) in ../dir/a/
warning: skipped "../" path component(s) in ../dir/a/a.txt
replace dir/a/a.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: A
  inflating: dir/a/a.txt 

:: cd build 

:: unzip ../a.zip 
Archive: ../a.zip
warning: skipped "../" path component(s) in ../dir/a/
   creating: dir/a/
warning: skipped "../" path component(s) in ../dir/a/a.txt
  inflating: dir/a/a.txt             

:: ls
CMakeCache.txt CMakeFiles Makefile cmake_install.cmake dir

```

The issue is that the archive is created with relative paths, as can be seen by the `zipinfo` output. How do I create the archive without these relative paths?

---

<div class="post-metadata">

### Author: ![K20shores](https://discourse.cmake.org/user_avatar/discourse.cmake.org/k20shores/32/2987_2.png) [@K20shores](https://discourse.cmake.org/u/K20shores)
#### Post date: [September 20, 2023, 2:31pm UTC](https://discourse.cmake.org/t/file-archive-create-uses-relative-paths/9013/2 "2023-09-20T14:31:29Z")

</div>

Since it may be a possible issue, [I filed one](https://gitlab.kitware.com/cmake/cmake/-/issues/25260).
