public void TestCoreProperties_bug51374() { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); String strDate = "2007-05-12T08:00:00Z"; DateTime date = DateTime.Parse(strDate).ToUniversalTime(); OPCPackage pkg = new ZipPackage(); PackagePropertiesPart props = (PackagePropertiesPart)pkg.GetPackageProperties(); // Created Assert.AreEqual("", props.GetCreatedPropertyString()); Assert.IsNull(props.GetCreatedProperty()); props.SetCreatedProperty((String)null); Assert.AreEqual("", props.GetCreatedPropertyString()); Assert.IsNull(props.GetCreatedProperty()); props.SetCreatedProperty(new Nullable <DateTime>()); Assert.AreEqual("", props.GetCreatedPropertyString()); Assert.IsNull(props.GetCreatedProperty()); props.SetCreatedProperty(new Nullable <DateTime>(date)); Assert.AreEqual(strDate, props.GetCreatedPropertyString()); Assert.AreEqual(date, props.GetCreatedProperty()); props.SetCreatedProperty(strDate); Assert.AreEqual(strDate, props.GetCreatedPropertyString()); Assert.AreEqual(date, props.GetCreatedProperty()); // lastPrinted Assert.AreEqual("", props.GetLastPrintedPropertyString()); Assert.IsNull(props.GetLastPrintedProperty()); props.SetLastPrintedProperty((String)null); Assert.AreEqual("", props.GetLastPrintedPropertyString()); Assert.IsNull(props.GetLastPrintedProperty()); props.SetLastPrintedProperty(new Nullable <DateTime>()); Assert.AreEqual("", props.GetLastPrintedPropertyString()); Assert.IsNull(props.GetLastPrintedProperty()); props.SetLastPrintedProperty(new Nullable <DateTime>(date)); Assert.AreEqual(strDate, props.GetLastPrintedPropertyString()); Assert.AreEqual(date, props.GetLastPrintedProperty()); props.SetLastPrintedProperty(strDate); Assert.AreEqual(strDate, props.GetLastPrintedPropertyString()); Assert.AreEqual(date, props.GetLastPrintedProperty()); // modified Assert.IsNull(props.GetModifiedProperty()); props.SetModifiedProperty((String)null); Assert.IsNull(props.GetModifiedProperty()); props.SetModifiedProperty(new Nullable <DateTime>()); Assert.IsNull(props.GetModifiedProperty()); props.SetModifiedProperty(new Nullable <DateTime>(date)); Assert.AreEqual(strDate, props.GetModifiedPropertyString()); Assert.AreEqual(date, props.GetModifiedProperty()); props.SetModifiedProperty(strDate); Assert.AreEqual(strDate, props.GetModifiedPropertyString()); Assert.AreEqual(date, props.GetModifiedProperty()); pkg.Close(); }
void ImagesListView_ItemCommand(object sender, RadListViewCommandEventArgs e) { if (e.CommandName == "DownloadAllAsZip") { RadListViewDataItem item = ListViewAlbums.SelectedItems[0]; int parentID = Convert.ToInt32(item.GetDataKeyValue("ID").ToString()); string albumName = item.GetDataKeyValue("Name").ToString(); List <DataContext.Image> allImagesFromAlbum = (from a in context.Images where a.AlbumID == parentID select a).ToList(); MemoryStream memStream = new MemoryStream(); Package = ZipPackage.Create(memStream); foreach (var image in allImagesFromAlbum) { Stream stream = new MemoryStream(image.Data); Package.AddStream(stream, image.FileName); } Package.Close(false); memStream.Position = 0; if (memStream != null && memStream.Length > 0) { Response.Clear(); Response.AddHeader("content-disposition", "attachment; filename=" + albumName + ".zip"); Response.ContentType = "application/zip"; Response.BinaryWrite(memStream.ToArray()); Response.End(); } } if (e.CommandName == "DownloadImage") { RadListViewDataItem item = e.ListViewItem as RadListViewDataItem; int imageID = Convert.ToInt32(item.GetDataKeyValue("ID").ToString()); DataContext.Image image = (from i in context.Images where i.ID == imageID select i).First(); byte[] data = image.Data; string name = image.FileName; if (data != null && data.Length > 0) { Response.Clear(); Response.ContentType = "application/octet-stream"; Response.AddHeader("content-disposition", "attachment; filename=" + name); Response.BinaryWrite(data); Response.End(); } } }
public void Save(object parameter) { PersistenceManager manager = new PersistenceManager(); this.compressedStream = new MemoryStream(); ZipPackage zipPackage = ZipPackage.Create(this.compressedStream); this.rawStream = manager.Save(parameter); this.rawStream.Position = 0L; zipPackage.AddStream(this.rawStream, "persistence", ZipCompression.Default, DateTime.Now.Date); zipPackage.Close(false); this.UncompressedSize = this.rawStream.Length; this.CompressedSize = this.compressedStream.Length; }
public void TestTidyStreamOnInvalidFile() { // Spreadsheet has a good mix of alternate file types POIDataSamples files = POIDataSamples.GetSpreadSheetInstance(); FileInfo[] notValidF = new FileInfo[] { files.GetFileInfo("SampleSS.ods"), files.GetFileInfo("SampleSS.txt") }; Stream[] notValidS = new Stream[] { files.OpenResourceAsStream("SampleSS.ods"), files.OpenResourceAsStream("SampleSS.txt") }; foreach (FileInfo notValid in notValidF) { ZipPackage pkg = new ZipPackage(notValid, PackageAccess.READ); Assert.IsNotNull(pkg.ZipArchive); Assert.IsFalse(pkg.ZipArchive.IsClosed); try { pkg.GetParts(); Assert.Fail("Shouldn't work"); } catch (ODFNotOfficeXmlFileException e) { } catch (NotOfficeXmlFileException ne) { } pkg.Close(); Assert.IsNotNull(pkg.ZipArchive); Assert.IsTrue(pkg.ZipArchive.IsClosed); } foreach (InputStream notValid in notValidS) { ZipPackage pkg = new ZipPackage(notValid, PackageAccess.READ); Assert.IsNotNull(pkg.ZipArchive); Assert.IsFalse(pkg.ZipArchive.IsClosed); try { pkg.GetParts(); Assert.Fail("Shouldn't work"); } catch (ODFNotOfficeXmlFileException e) { } catch (NotOfficeXmlFileException ne) { } pkg.Close(); Assert.IsNotNull(pkg.ZipArchive); Assert.IsTrue(pkg.ZipArchive.IsClosed); } }
private bool disposedValue = false; // To detect redundant calls protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { _pbixPackage?.Close(); } // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below. // TODO: set large fields to null. disposedValue = true; } }
public void ReplaceKnownVariable(string variable, string newValue) { string packagePartsTempLocation = Path.GetTempFileName(); ZipPackage partsPackage = GetPackageParts(packagePartsTempLocation); var parts = partsPackage.GetParts(); var enumerator = parts.GetEnumerator(); ZipPackagePart mCodePart = null; while (enumerator.MoveNext()) { if (enumerator.Current.Uri.OriginalString.Contains("/Formulas/Section1.m")) { mCodePart = (ZipPackagePart)enumerator.Current; break; } } string theMCode; using (StreamReader mCodeReader = new StreamReader(mCodePart.GetStream())) { theMCode = mCodeReader.ReadToEnd(); } string pattern = "^shared\\s*" + variable + "\\s*=\\s*\"\\S*\""; Regex r = new Regex(pattern, RegexOptions.Multiline | RegexOptions.IgnoreCase); string updatedMCode = r.Replace(theMCode, string.Format(CultureInfo.InvariantCulture, "shared {0} = \"{1}\"", variable, newValue)); byte[] updateMCodeBytes = Encoding.UTF8.GetBytes(updatedMCode); mCodePart.GetStream().Write(updateMCodeBytes, 0, updateMCodeBytes.Length); mCodePart.GetStream().SetLength(updateMCodeBytes.Length); mCodePart.GetStream().Flush(); partsPackage.Close(); FileInfo fi = new FileInfo(packagePartsTempLocation); _packagePartsLength = Convert.ToUInt32(fi.Length); _packageParts = File.ReadAllBytes(packagePartsTempLocation); File.Delete(packagePartsTempLocation); }
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(); }
void ImagesListView_ItemCommand(object sender, RadListViewCommandEventArgs e) { if (e.CommandName == "DownloadAllAsZip") { RadListViewDataItem item = ListViewAlbums.SelectedItems[0]; int parentID = Convert.ToInt32(item.GetDataKeyValue("ID").ToString()); string albumName = item.GetDataKeyValue("Name").ToString(); List<DataContext.Image> allImagesFromAlbum = (from a in context.Images where a.AlbumID == parentID select a).ToList(); MemoryStream memStream = new MemoryStream(); Package = ZipPackage.Create(memStream); foreach (var image in allImagesFromAlbum) { Stream stream = new MemoryStream(image.Data); Package.AddStream(stream, image.FileName); } Package.Close(false); memStream.Position = 0; if (memStream != null && memStream.Length > 0) { Response.Clear(); Response.AddHeader("content-disposition", "attachment; filename=" + albumName + ".zip"); Response.ContentType = "application/zip"; Response.BinaryWrite(memStream.ToArray()); Response.End(); } } if (e.CommandName == "DownloadImage") { RadListViewDataItem item = e.ListViewItem as RadListViewDataItem; int imageID = Convert.ToInt32(item.GetDataKeyValue("ID").ToString()); DataContext.Image image = (from i in context.Images where i.ID == imageID select i).First(); byte[] data = image.Data; string name = image.FileName; if (data != null && data.Length > 0) { Response.Clear(); Response.ContentType = "application/octet-stream"; Response.AddHeader("content-disposition", "attachment; filename=" + name); Response.BinaryWrite(data); Response.End(); } } }
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; }