Thursday, March 24, 2011

Creating CAB in C#, windows 7

///These codes are recycled and taken from various places. I also have added some code but there are various references for this code. I don't own the code entirely. I am putting this up for reference only.


/// Creates a cab file, uses makecab.exe that is already in c:\windows\system32
///

/// Path for the cab file with Name. eg "c:\cabinet1.cab"
/// pass the full path and file names needed to be included in the cab as an string array
///
public static string MakeCab(string cabfilePath,string[] filesToInclude)
{
ArrayList sampleddf = new ArrayList ();

sampleddf.Add( ";*** Sample Source Code MakeCAB Directive file example");
sampleddf.Add( ";");
sampleddf.Add( ".OPTION EXPLICIT ; Generate errors ");
sampleddf.Add(".Set CabinetNameTemplate=" + (cabfilePath.Substring ((cabfilePath.LastIndexOf('\\')+1))));
sampleddf.Add( ".set DiskDirectoryTemplate=CDROM ; All cabinets go in a single directory");

sampleddf.Add( ".Set CompressionType=MSZIP;** All files are compressed in cabinet files");
sampleddf.Add( ".Set UniqueFiles=\"OFF\"");
sampleddf.Add( ".Set Cabinet=on");
sampleddf.Add(".Set DiskDirectory1=" + (cabfilePath.Substring(0,(cabfilePath.LastIndexOf('\\')+1))));
foreach (string s in filesToInclude)
{
sampleddf.Add(s);
}
sampleddf.Add(";*** ");

string[] array = sampleddf.ToArray(typeof(string)) as string[];+6
string sampleddfPath = System.IO.Path.GetTempPath() + "sample.ddf";
sampleddfPath = writeTextFile(sampleddfPath, array, Encoding.UTF8, true);

ExecuteCommandSync("makecab.exe /f " + sampleddfPath);

return cabfilePath;
}

///



/// Executes a shell command synchronously.
///

/// string command
/// string, as output of the command.
public static string ExecuteCommandSync(object command)
{
try
{
// create the ProcessStartInfo using "cmd" as the program to be run,
// and "/c " as the parameters.
// Incidentally, /c tells cmd that we want it to execute the command that follows,
// and then exit.
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);

// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
// Get the output into a string
string result = proc.StandardOutput.ReadToEnd();
// Display the command output.
//Console.WriteLine(result);
return result;
}
catch (Exception objException)
{
// Log the exception
}
return null;
}

public static string writeTextFile(string fpath, string[] lines, Encoding encode, bool overWriteFlag = false)
{
if(overWriteFlag)
{
if (File.Exists(fpath))
{
File.Delete(fpath);
}
}

try
{
File.WriteAllLines(fpath, lines, encode);
return fpath;
}
catch (Exception e)
{
return null;
}
}

No comments:

Node.JS rest api Tutorials