#region CPL License
/*
Nuclex.NAnt.Tasks
Copyright (C) 2009 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
using Microsoft.Win32;
using NAnt.Core;
using NAnt.Core.Attributes;
using NAnt.Core.Types;
namespace Nuclex.NAnt.Tasks {
/// Task for extracting files from a 7-Zip archive
[TaskName("sevenzip-extract")]
public class SevenZipExtractTask : Task {
/// Initializes a new 7-Zip extraction task
public SevenZipExtractTask() {
this.toDirectory = new DirectoryInfo(".");
}
/// Archive to create or to extract files from
[TaskAttribute("archive", Required = true), StringValidator(AllowEmpty = false)]
public FileInfo Archive {
get { return this.archive; }
set { this.archive = value; }
}
/// Password the archive is protected with
[TaskAttribute("password", Required = false), StringValidator(AllowEmpty = false)]
public string Password {
get { return this.password; }
set { this.password = value; }
}
/// Directory into which the 7-Zip archive will be extracted
[TaskAttribute("todir"), StringValidator(AllowEmpty = false)]
public DirectoryInfo ToDirectory {
get { return this.toDirectory; }
set { this.toDirectory = value; }
}
/// Executes the task
protected override void ExecuteTask() {
this.Log(
Level.Info,
"Extracting '{0}' to '{1}'.",
this.archive.FullName,
this.toDirectory.FullName
);
string commandLine = string.Format(
"x -y \"{0}\"",
this.archive
);
if(!string.IsNullOrEmpty(this.password)) {
commandLine += " -p\"" + this.password + "\"";
}
if(!Directory.Exists(this.toDirectory.FullName)) {
Directory.CreateDirectory(this.toDirectory.FullName);
}
try {
SevenZip.Run(this.toDirectory.FullName, commandLine);
}
catch(InvalidOperationException exception) {
throw new BuildException(
"Error during 7-Zip decompression operation: " + exception.Message,
this.Location
);
}
}
/// Archive to create or to extract files from
private FileInfo archive;
/// Directory to extract to
private DirectoryInfo toDirectory;
/// Password the archive is protected with
private string password;
}
} // namespace Nuclex.NAnt.Tasks