Part 2 of 5: Unpacking the XSN

To unpack XSN one should use extract.exe utility. It can be found in the %SystemRoot%\system32 directory or in the Microsoft Cabinet Software Development Kit. For the command usage details just run “extract /?”.

In this part we will also introduce several auxiliary functions that will be extensively used throughout the entire discussion topic.

var ExtractExeLocation = "%SystemRoot%\\SYSTEM32";

var ExtractExe = Combine(ExtractExeLocation, "extract.exe");

 

function Combine(dir, file)

{

      return dir + "\\" + file ;

}

 

function QuoteStr(string)

{

      return ("\"" + string + "\"");

}

 

function Exec(string, flagWait)

{

      if (flagWait == null)

            flagWait = true;

      var WScriptShell = WScript.CreateObject("WScript.Shell");

      return WScriptShell.Run(string, 0, flagWait);

}

 

function ExtractFilesFromXSN(xsnInputPath, outputDirectory)

{

      var Parameters;

 

      // add the location to place extracted files,

      // a flag "/Y" that prevents prompting before overwriting

      // an existing file, and a flag "/E" that orders to

      // extract all files

      if (outputDirectory != null)

            Parameters = " /Y /E /L " + QuoteStr(outputDirectory);

 

      Exec(QuoteStr(ExtractExe)

            + Parameters

            + " " + QuoteStr(xsnInputPath), true);

} 

Now we have a tool to unpack XSN. All of the InfoPath form template files are accessible for changing. In the next part of the series we will be modifying the manifest itself.