将zip文件转换为MSI文件/ EXE文件/ windows安装文件

时间:2011-07-19 06:28:29

标签: c#

我想将一个zip文件转换为MSI文件/ EXE文件/ windows安装文件,以便安装在网络中的远程计算机上。

到目前为止,我可以使用以下C#代码制作包含用户选择的可执行文件和相关支持文件等的zip文件。


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 Operation Error");
        }
        this.Dispose();
    }

我不知道如何开发C#代码将zip文件转换为MSI文件/ EXE文件/ windows安装文件。

请帮助我,如果有人有想法解决这个问题。

先谢谢。

0 个答案:

没有答案
相关问题