1 // Copyright (c) 2017 Matthew Brennan Jones <matthew.brennan.jones@gmail.com> 2 // Boost Software License - Version 1.0 3 // A simple message box for the D programming language 4 // https://github.com/workhorsy/d-message-box 5 6 module extract; 7 8 9 import std.stdio : stdout, stderr; 10 import compressed_file : CompressedFile; 11 12 13 void extractFiles(string target_dir, immutable CompressedFile[] compressed_files, void delegate(int percent) progress_cb) { 14 import std.file : exists, mkdir, copy, mkdirRecurse, chdir, getcwd; 15 import std.path : buildPath, dirName, baseName; 16 import std.conv : to; 17 import std.zlib : uncompress; 18 import std.base64 : Base64; 19 import std.stdio : File; 20 21 progress_cb(0); 22 23 // Make the project directory if it does not exist 24 string root_path = getcwd(); 25 if (! exists(root_path)) { 26 mkdir(root_path); 27 } 28 29 // Extract all the files 30 double count = compressed_files.length.to!double; 31 foreach (i, entry ; compressed_files) { 32 // Move the dialog percent forward 33 int percent = ((i.to!double / count) * 100.0f).to!int; 34 progress_cb(percent); 35 36 string full_name = buildPath(target_dir, entry.name); 37 38 // Skip if the file already exists 39 if (exists(full_name)) { 40 stdout.writefln("skipping extraction of: %s", entry.name); 41 continue; 42 } 43 44 // Extract the file 45 stdout.writefln("Extracting: %s", full_name); 46 47 // Unbase64 and uncompress the data 48 ubyte[] unb64ed = Base64.decode(entry.data); 49 ubyte[] data = cast(ubyte[]) uncompress(unb64ed); 50 51 // Make the directory if it does not exist 52 string dir_name = dirName(full_name); 53 if (! exists(dir_name)) { 54 mkdirRecurse(dir_name); 55 } 56 57 // Write the data to a file 58 auto out_file = File(full_name, "wb"); 59 out_file.write(cast(char[])data); 60 out_file.close(); 61 } 62 63 progress_cb(100); 64 }