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?
-
@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. -
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?
-
@CybeX said in copy file from resource to disk keeping folder structure (platform independant):
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?
-
//copy whole <from> directory to the folder <to> void copyDirectoryNested(QString from,QString to) { QDirIterator it(from, QDirIterator::Subdirectories); while (it.hasNext()) { QString file_in = it.next(); QFileInfo file_info = QFileInfo(file_in); QString file_out = file_in; file_out.replace(from,to); if(file_info.isFile()) { //is file copy qDebug() << QFile::copy(file_in, file_out); qDebug() << file_in << "<----" << file_out; } if(file_info.isDir()) { //dir mkdir QDir dir(file_out); if (!dir.exists()) qDebug() << "mkpath"<< dir.mkpath("."); } } }
then use above function :
copyDirectoryNested(":/<qrc_folder_location>",<writing_location>);
copyDirectoryNested(":/DefaultAppData",app_data_location);