convert a zip file to MSI file/EXE file/windows installation file
I want to convert a zip file to MSI file/EXE file/windows installation file to install in remote computers in the network.
Up to now, I can make a zip file with user selected executable files and related support files etc. using the following C# code.
private void btnPackaging_Click(object sender, EventArgs e)
{
// make sure there are files to zip
if (listBox1.Items.Count < 1)
{
MessageBox.Show("There are no files queued for the zip operation", "Empty File Set");
return;
}
// make sure there is a destination defined
if (textBox1.Text == string.Empty)
{
MessageBox.Show("No destination file has been defined.", "Save To Empty");
return;
}
label3.Visible = true;
label3.Refresh();
// name the zip file whatever the folder is named
// by splitting the file path to get the folder name
string[] sTemp = textBox1.Text.Split('\\');
string sZipFileName = sTemp[sTemp.Length - 1].ToString();
// check to see if zipped file already exists
// user may rename it in the text box if it does.
FileInfo fi = new FileInfo(textBox1.Text + "\\" + sZipFileName + ".zip");
if (fi.Exists)
{
// move it to the folder
try
{
StringBuilder sb = new StringBuilder();
sb.Append("The file " + sZipFileName + " already exists. ");
sb.Append("You may rename it in the save to text box.");
MessageBox.Show(sb.ToString(), "Existing File Name");
textBox1.Focus();
return;
}
catch
{
MessageBox.Show("Rename the file or select a new location.", "File Error");
return;
}
}
// Check for the existence of the target folder and
// create it if it does not exist
if (!System.IO.Directory.Exists(textBox1.Text + "\\TempZipFile\\"))
{
System.IO.Directory.CreateDirectory(textBox1.Text + "\\TempZipFile\\");
}
// Set up a string to hold the path to the temp folder
string sTargetFolderPath = (textBox1.Text + "\\TempZipFile\\");
// Process the files and move each into the target folder
for (int i = 0; i < listBox1.Items.Count; i++)
{
string filePath = listBox1.Items[i].ToString();
FileInfo fi2 = new FileInfo(filePath);
if (fi2.Exists)
{
// move it to the folder
try
{
fi2.CopyTo(sTargetFolderPath + fi2.Name, true);
}
catch
{
// clean up if the operation failed
System.IO.Directory.Delete(sTargetFolderPath);
MessageBox.Show("Could not copy files to temp folder.", "File Error");
return;
}
}
}
// zip up the files
try
{
label3.Visible = true;
label3.Refresh();
string[] filenames = Directory.GetFiles(sTargetFolderPath);
// Zip up the files - From SharpZipLib Demo Code
using (ZipOutputStream s = new ZipOutputStream(File.Create(textBox1.Text + "\\" + sZipFileName + ".zip")))
{
s.SetLevel(9); // 0-9, 9 being the highest level of compression
byte[] buffer = new byte[4096];
foreach (string file in filenames)
{
ZipEntry entry = new ZipEntry(Path.GetFileName(file));
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(file))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
}
s.Finish();
s.Close();
}
// remove the progress bar
label3.Visible = false;
// clean up files by deleting the temp folder and its content
System.IO.Directory.Delete(textBox1.Text + "\\TempZipFile\\", true);
// Notify user
MessageBox.Show("Zip file " + textBox1.Text + " created.");
// empty everything
listBox1.Items.Clear();
textBox1.Text = string.Empty;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Zip Ope开发者_JAVA百科ration Error");
}
this.Dispose();
}
I don't have any idea, how to develop C# code to convert zip file into MSI file/EXE file/windows installation file.
Please help me, if anybody have an idea to solve this.
Thanks in Advance.
精彩评论