public static void PackToStream(string[] fileNames, string mainExecutable, Stream stream) { if (fileNames == null) { throw new ArgumentNullException("fileNames"); } ZipPackage pkg = (ZipPackage)ZipPackage.Open(stream, FileMode.Create); foreach (string f in fileNames) { Uri uri = PackUriHelper.CreatePartUri(new Uri(Path.GetFileName(f), UriKind.Relative)); ZipPackagePart p = (ZipPackagePart)pkg.CreatePart(uri, System.Net.Mime.MediaTypeNames.Application.Octet, CompressionOption.Maximum); CopyStream(new FileStream(f, FileMode.Open, FileAccess.Read), p.GetStream()); if (f == mainExecutable) { pkg.CreateRelationship(uri, TargetMode.Internal, "http://schemas.openxmlformats.org/package/2006/relationships/meta data/thumbnail"); } } pkg.Close(); }
private void btnSelectExecutable_Click(object sender, RoutedEventArgs e) { Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait; System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog(); dlg.DefaultExt = "exe"; dlg.Filter = "Executable files (*.exe)|*.exe|All Files (*.*)|*.*"; dlg.Multiselect = true; if (File.Exists(txtExecutable.Text)) { dlg.FileName = txtExecutable.Text; } if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) { if (dlg.FileNames.Count() == 1) { txtExecutable.Text = dlg.FileName; chkNewBinary.IsChecked = true; chkMostRecentBinary.IsChecked = false; txtExecutable.IsEnabled = true; } else if (dlg.FileNames.Count() > 1) { // Create a ZipPackage string fn = Path.GetTempFileName(); ZipPackage pkg = (ZipPackage)ZipPackage.Open(fn, FileMode.Create); string mainFile = ""; int exe_count = 0; foreach (string f in dlg.FileNames) { if (f.EndsWith(".exe")) { mainFile = f; exe_count++; } } if (exe_count != 1) { SelectMainExe sme = new SelectMainExe(); foreach (string f in dlg.FileNames) { sme.lbFiles.Items.Add(f); } sme.Owner = this; Mouse.OverrideCursor = null; if (sme.ShowDialog() == true) { mainFile = sme.selectedFile; } Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait; } foreach (string f in dlg.FileNames) { Uri uri = PackUriHelper.CreatePartUri(new Uri(Path.GetFileName(f), UriKind.Relative)); ZipPackagePart p = (ZipPackagePart)pkg.CreatePart(uri, System.Net.Mime.MediaTypeNames.Application.Octet, CompressionOption.Maximum); CopyStream(new FileStream(f, FileMode.Open, FileAccess.Read), p.GetStream()); if (f == mainFile) { pkg.CreateRelationship(uri, TargetMode.Internal, "http://schemas.openxmlformats.org/package/2006/relationships/meta data/thumbnail"); } } pkg.Close(); txtExecutable.Text = fn; chkNewBinary.IsChecked = true; chkMostRecentBinary.IsChecked = false; txtExecutable.IsEnabled = true; } } Mouse.OverrideCursor = null; }