// -------------------------- CreatePackage -------------------------- /// <summary> /// Creates a package zip file containing specified /// content and resource files.</summary> private static void CreatePackage(string packagePath, bool withFiles, bool withDB, Player p) { // Create the Package if (withDB) { Server.s.Log("Saving DB..."); SaveDatabase("SQL.sql"); Server.s.Log("Saved DB to SQL.sql"); } Server.s.Log("Creating package..."); using (ZipPackage package = (ZipPackage)ZipPackage.Open(packagePath, FileMode.Create)) { if (withFiles) { Server.s.Log("Collecting Directory structure..."); string currDir = Directory.GetCurrentDirectory() + "\\"; List <Uri> partURIs = GetAllFiles(new DirectoryInfo("./"), new Uri(currDir)); Server.s.Log("Structure complete"); Server.s.Log("Saving data..."); foreach (Uri loc in partURIs) { if (!Uri.UnescapeDataString(loc.ToString()).Contains(packagePath)) { // Add the part to the Package ZipPackagePart packagePart = (ZipPackagePart)package.CreatePart(loc, ""); // Copy the data to the Document Part using (FileStream fileStream = new FileStream( "./" + Uri.UnescapeDataString(loc.ToString()), FileMode.Open, FileAccess.Read)) { CopyStream(fileStream, packagePart.GetStream()); } // end:using(fileStream) - Close and dispose fileStream. } } // end:foreach(Uri loc) } if (withDB) { // If we don't want to back up database, we don't do this part. Server.s.Log("Compressing Database..."); ZipPackagePart packagePart = (ZipPackagePart)package.CreatePart(new Uri("/SQL.sql", UriKind.Relative), "", CompressionOption.Normal); CopyStream(File.OpenRead("SQL.sql"), packagePart.GetStream()); Server.s.Log("Database compressed."); } // end:if(withFiles) Server.s.Log("Data saved!"); } // end:using (Package package) - Close and dispose package. Player.SendMessage(p, "Server backup (" + (withFiles ? "Everything" + (withDB ? "" : " but Database") : "Database") + "): Complete!"); Server.s.Log("Server backed up!"); }// end:CreatePackage()
private ZipPackageRelationship CreateThemeOverridePart(ZipPackage p, ZipPackagePart partToCopy) { var id = GetIxFromChartUri(_chart.UriChart.OriginalString); ThemeOverrideUri = GetNewUri(p, "/xl/theme/themeOverride{0}.xml", ref id); var rel = _chart.Part.CreateRelationship(ThemeOverrideUri, TargetMode.Internal, ExcelPackage.schemaThemeOverrideRelationships); ThemeOverridePart = p.CreatePart(ThemeOverrideUri, ExcelPackage.contentTypeThemeOverride); ThemeOverrideXml = new XmlDocument(); ThemeOverrideXml.Load(partToCopy.GetStream()); foreach (var themeRel in partToCopy.GetRelationships()) { var uri = OfficeOpenXml.Utils.UriHelper.ResolvePartUri(themeRel.SourceUri, themeRel.TargetUri); var toPart = _chart.Part.Package.CreatePart(uri, PictureStore.GetContentType(uri.OriginalString)); var imageRel = ThemeOverridePart.CreateRelationship(uri, TargetMode.Internal, themeRel.RelationshipType); SetRelIdInThemeDoc(ThemeOverrideXml, themeRel.Id, imageRel.Id); var stream = partToCopy.GetStream(); var b = stream.GetBuffer(); toPart.GetStream().Write(b, 0, b.Length); } ThemeOverrideXml.Save(ThemeOverridePart.GetStream(FileMode.CreateNew)); partToCopy.Package.Dispose(); return(rel); }
private int CreateColorPart(ZipPackage p) { var id = GetIxFromChartUri(_chart.UriChart.OriginalString); ColorsUri = GetNewUri(p, "/xl/charts/colors{0}.xml", ref id); _chart.Part.CreateRelationship(ColorsUri, TargetMode.Internal, ExcelPackage.schemaChartColorStyleRelationships); ColorsPart = p.CreatePart(ColorsUri, ExcelPackage.contentTypeChartColorStyle); return(id); }
static void SaveDatabase(ZipPackage package) { Server.s.Log("Compressing Database..."); Uri uri = new Uri("/SQL.sql", UriKind.Relative); PackagePart part = package.CreatePart(uri, "", CompressionOption.Normal); CopyStream(File.OpenRead("SQL.sql"), part.GetStream()); Server.s.Log("Database compressed"); }
internal void ResetRecordXml(ZipPackage pck) { var cacheRecord = new XmlDocument(); cacheRecord.LoadXml("<pivotCacheRecords xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" count=\"0\" />"); ZipPackagePart recPart; if (pck.PartExists(CacheRecordUri)) { recPart = pck.GetPart(CacheRecordUri); } else { recPart = pck.CreatePart(CacheRecordUri, ExcelPackage.schemaPivotCacheRecords); } cacheRecord.Save(recPart.GetStream(FileMode.Create, FileAccess.Write)); }
static void SaveFiles(ZipPackage package, List <Uri> partURIs) { foreach (Uri loc in partURIs) { string file = Uri.UnescapeDataString(loc.ToString()); if (file.Contains(path)) { continue; } try { PackagePart part = package.CreatePart(loc, ""); using (Stream src = new FileStream("./" + file, FileMode.Open, FileAccess.Read)) CopyStream(src, part.GetStream()); } catch (Exception ex) { Server.s.Log("Failed to save file: " + file); Server.ErrorLog(ex); } } }
public static void WriteZip(string packagePath, string[] filePathes, string[] contentTypes, HttpResponse response, string fileName) { if (filePathes.Length != contentTypes.Length) { throw new ArgumentException("The lengths of the number of files and the content types should be equal"); } response.Clear(); response.AddHeader("content-disposition", "attachment; filename=" + fileName + ".zip"); response.ContentType = "application/zip"; using (ZipPackage package = (ZipPackage)Package.Open(packagePath, FileMode.Create)) { for (int i = 0; i < filePathes.Length; i++) { string destFilename = ".\\" + Path.GetFileName(filePathes[i]); Uri uri = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.RelativeOrAbsolute)); if (package.PartExists(uri)) { package.DeletePart(uri); } PackagePart part = package.CreatePart(uri, "", CompressionOption.Normal); using (FileStream fileStream = new FileStream(filePathes[i], FileMode.Open, FileAccess.Read)) { using (Stream dest = part.GetStream()) { CopyStream(fileStream, dest); } } } package.Flush(); } response.Write(packagePath); response.Flush(); response.Close(); }
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; }