internal IDocumentFile AddResourceFile(Package package, string resourceName, string relativePath, DocumentFileType type) { Debug.Assert(resourceName != null); // return if part already exists var uri = new Uri("./" + relativePath, UriKind.Relative); var file = _files.FirstOrDefault(x => x.Location == uri); if (file != null) { return(file); } if (package != null) { uri = PackUriHelper.CreatePartUri(uri); if (package.PartExists(uri)) { var part = package.GetPart(uri); file = new PackageDocumentFile(part) { Type = type }; } } if (file == null) { // copy file to working folder and add to list var targetFullPath = ResolveRelativePath(relativePath); var assembly = typeof(AmlxDocument).Assembly; using (var stream = assembly.GetManifestResourceStream(resourceName)) { if (stream == null) { throw new AmlxException("Could not extract resource file"); } using (var writer = new FileStream(targetFullPath, FileMode.Create, FileAccess.Write)) { stream.CopyTo(writer); } } file = new IntermediateDocumentFile(_workingDirectory, relativePath) { Type = type }; } _files.Add(file); RaiseCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, file)); return(file); }
/// <summary> /// Adds a copy of the specified file to this collection. /// </summary> /// <param name="file">The file.</param> /// <param name="package"></param> /// <returns></returns> internal IDocumentFile AddCopy(IDocumentFile file, Package package) { var partUri = PackUriHelper.CreatePartUri(file.Location); var mimetype = XMLMimeTypeMapper.GetMimeType(partUri.ToString()); if (package.PartExists(partUri)) { package.DeletePart(partUri); } var part = package.CreatePart(partUri, mimetype, CompressionOption.Normal); if (part == null) { throw new AmlxCorruptedDocumentException("Cannot create part (null)."); } using (var source = file.GetStream()) { using (var target = part.GetStream(FileMode.Create, FileAccess.Write)) { source.CopyTo(target); } } var copy = new PackageDocumentFile(part); Add(copy); return(copy); //var tempPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); //using (var stream = file.GetStream()) //{ // using (var writer = new FileStream(tempPath, FileMode.Create, FileAccess.Write)) // { // stream.CopyTo(writer); // } //} //var copy = Add(tempPath, file.Location.ToString()); //return copy; }