0

Problem:

How do I copy resource files, while preserving file names, to the disk in a platform independent way?

Example and Explanation:

I have files in my Resource File (.qrc file) e.g.:

:/student/stud_temp              (student/template.txt)
:/student/examples/stud_exOne    (student/examples/student_example1.txt)
:/student/examples/stud_exTwo    (student/examples/student_example2.txt)

:/lecturer/lec_temp              (lecturer/template.txt)
:/lecturer/lec_ex                (lecturer/lecturer_example.txt)
:/lecturer/data/lec_data         (lecturer/data/data_file.dat)

I would like to copy these files on to the disk, for sake of the example, preserve the file structure.

So the resulting folder structure should be something like this (unix system):

$ ls $PWD
student lecturer

$ ls $PWD/student
template.txt
examples

$ ls $PWD/lecturer
template.txt
lecturer_example.txt
data

There is a method suggested here to copy to filesystem, as shown below, but the file name needs to be specified. However this should be automated to get the resource's original filename.

Currently I need to do the following to replicate the folder structure:

QDir::mkpath("../student");    
QFile::copy(":/student/template.txt", "../student/template.txt")

However I cannot believe that this is the best way.

Also, this method is platform dependant, due to the directory separators (trivial problem though).

Are there any better ways to achieve this?

Community
  • 1
  • 1
CybeX
  • 2,060
  • 3
  • 48
  • 115
  • @drescherjm thanks for that. Although the original problem persists. Naming each file you copy over, instead of the directory "tree" being created with each file named correctly in its place – CybeX Jan 24 '17 at 15:35
  • Respect the separator, `QDir` and similars use '/' as separator and translate it to the native one as needed. You can do it manually using the `QDir::toNativeSeparators` if you have to pass it as argument to a native function, for example. – cbuchart Feb 19 '17 at 07:37

0 Answers0