1 /** 2 * ae.sys.archive 3 * 4 * License: 5 * This Source Code Form is subject to the terms of 6 * the Mozilla Public License, v. 2.0. If a copy of 7 * the MPL was not distributed with this file, You 8 * can obtain one at http://mozilla.org/MPL/2.0/. 9 * 10 * Authors: 11 * Vladimir Panteleev <vladimir@thecybershadow.net> 12 */ 13 14 module ae.sys.archive; 15 16 import std.exception; 17 import std.file; 18 import std.path; 19 import std.process; 20 import std.string; 21 22 import ae.sys.file; 23 import ae.sys.install.sevenzip; 24 25 /// Unzips a .zip file to the target directory. 26 void unzip(string zip, string target) 27 { 28 import std.zip; 29 auto archive = new ZipArchive(zip.read); 30 foreach (name, entry; archive.directory) 31 { 32 auto path = buildPath(target, name); 33 ensurePathExists(path); 34 if (name.endsWith(`/`)) 35 { 36 if (!path.exists) 37 path.mkdirRecurse(); 38 } 39 else 40 std.file.write(path, archive.expand(entry)); 41 } 42 } 43 44 /// Unpacks an archive to the specified directory. 45 /// Uses std.zip for .zip files, and invokes 7-Zip for 46 /// other file types (installing it locally if necessary). 47 void unpack(string archive, string target) 48 { 49 if (archive.toLower().endsWith(".zip")) 50 archive.unzip(target); 51 else 52 { 53 sevenZip.require(); 54 target.mkdirRecurse(); 55 auto pid = spawnProcess([sevenZip.exe, "x", "-o" ~ target, archive]); 56 enforce(pid.wait() == 0, "Extraction failed"); 57 } 58 }