copy file from resource to disk keeping folder structure (platform independant)



  • 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?



  • Hi @cybex

    In my opinion, using the working directory is a bad idea.
    You should better use one of the standard locations defined in the QStandardPaths class instead.


  • Moderators

    @CybeX said

    However I cannot believe that this is the best way.

    Looks ok. What's wrong with it?

    @CybeX said

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

    Qt uses / as universal separator and translates it to the platform specific ones. You don't have to worry about it yourself.

    What I would worry about though is the usage of ... This is relative to the working directory, which will vary depending on how your app was started. Use applicationDirPath() and make your paths relative to that.



  • @mpergand thanks for the suggestion, although the use of the current directory is just an example to illustrate my question/(issue). I will be using the QTemporaryDir due to the nature of the files I am using.



  • @Chris-Kawa @mpergand

    Looks ok. What's wrong with it?

    Well...nothing but it is an extremely tedious process...

    Ok, let me change the question slightly here to help solve this using a different method.

    Can (and how) am I able to get the original resource file name (e.g. below) from the QResource object.

    template.txt
    

    or

    student/template.txt
    

    I attempted this by using the QResource::fileName() and QResource::absoluteFileName() both return the resource location as a string shown below:

    :/student/stud_temp
    

    Thoughts?


  • Qt Champions 2016

    @CybeX said in copy file from resource to disk keeping folder structure (platform independant):

    @Chris-Kawa @mpergand

    Looks ok. What's wrong with it?

    Well...nothing but it is an extremely tedious process...

    Ok, let me change the question slightly here to help solve this using a different method.

    Can (and how) am I able to get the original resource file name (e.g. below) from the QResource object.

    Why don't you just give up to set alias and use the original file name in qrc?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.