I need to unzip a file while running a C++ program (as described in Waiting for unzip to finish before carrying on C++ code on a RedHat machine)
To do this I currently do something like this:
system("unzip /usr/bin/File/ZippedFile.gz -d /usr/bin/File/)
Which unzips “/usr/bin/File/ZippedFile.gz” to “/usr/bin/File/ZippedFile” with no problems.
This works fine. However I have noticed that many people seem to say that using
system()
is taboo.
People don’t seem to like it as due to security and system resources (as discussed here: http://www.cplusplus.com/forum/articles/11153/).
But as I want the program to wait until the unzip is complete, is there a viable alternative?
You can use plain
zlib, or theboost::iostreamgzip facility.System isn’t wrong per se, but you could also write a replacement for it which doesn’t use the shell with
fork,exec,waitandmkstemp. This is cumbersome though. Usingboost::gzip_decompressoris the best C++ option to me if you decompress single files. Forking and friends may be better if you need to unzip a directory. Be sure to read aboutmkstemp.For a crash course on fork and exec family: http://www.yolinux.com/TUTORIALS/ForkExecProcesses.html
For an example of using
waitfor your child to terminate: http://support.sas.com/documentation/onlinedoc/sasc/doc/lr2/wait.htmFor reference about creating a temporary directory: http://www.gnu.org/s/hello/manual/libc/Temporary-Files.html