DeletePart() public méthode

public DeletePart ( Uri partUri ) : void
partUri System.Uri
Résultat void
Exemple #1
0
        /// <summary>
        /// Creates a new instance of the ExcelPackage class based on a existing file or creates a new file. 
        /// </summary>
        /// <param name="newFile">If newFile exists, it is opened.  Otherwise it is created from scratch.</param>
        private ExcelPackage(FileInfo newFile)
        {
            _outputFolderPath = newFile.DirectoryName;
            if (newFile.Exists)
            {
                // open the existing package
                _package = Package.Open(newFile.FullName, FileMode.Open, FileAccess.ReadWrite);
            }
            else
            {
                // create a new package and add the main workbook.xml part
                _package = Package.Open(newFile.FullName, FileMode.Create, FileAccess.ReadWrite);

                // save a temporary part to create the default application/xml content type
                var uriDefaultContentType = new Uri("/default.xml", UriKind.Relative);
                var partTemp = _package.CreatePart(uriDefaultContentType, "application/xml");

                var workbook = Workbook.WorkbookXml; // this will create the workbook xml in the package

                // create the relationship to the main part
                _package.CreateRelationship(Workbook.WorkbookUri,
                                            TargetMode.Internal,
                                            schemaRelationships + "/officeDocument");

                // remove the temporary part that created the default xml content type
                _package.DeletePart(uriDefaultContentType);
            }
        }
        /// <summary>
        ///By package because  ChangeDocumentType not working well
        /// </summary>
        /// <param name="documentStream"></param>
        private void ChangeDocmToDocxUsingPackage(Stream documentStream)
        {
            // Open the document in the stream and replace the custom XML part
            using (System.IO.Packaging.Package packageFile = System.IO.Packaging.Package.Open(documentStream, FileMode.Open, FileAccess.ReadWrite))
            {
                System.IO.Packaging.PackagePart packagePart = null;
                // Find part containing the correct namespace
                foreach (var part in packageFile.GetParts())
                {
                    if (part.ContentType.Equals("application/vnd.ms-word.document.macroEnabled.main+xml", StringComparison.OrdinalIgnoreCase))
                    {
                        packagePart = part;
                        break;
                    }
                }
                if (packagePart != null)
                {
                    using (MemoryStream source = new MemoryStream())
                    {
                        CopyStream(packagePart.GetStream(), source);

                        var saveRelationBeforeDelPart = new List <PackageRelationship>();
                        foreach (var item in packagePart.GetRelationships())
                        {
                            saveRelationBeforeDelPart.Add(item);
                        }

                        Uri uriData = packagePart.Uri;
                        // Delete the existing XML part
                        if (packageFile.PartExists(uriData))
                        {
                            packageFile.DeletePart(uriData);
                        }

                        // Load the custom XML data
                        var pkgprtData = packageFile.CreatePart(uriData, "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml", System.IO.Packaging.CompressionOption.SuperFast);

                        source.Position = 0;//reset position
                        CopyStream(source, pkgprtData.GetStream(FileMode.Create));

                        foreach (var copyRel in saveRelationBeforeDelPart)
                        {
                            pkgprtData.CreateRelationship(copyRel.TargetUri, copyRel.TargetMode, copyRel.RelationshipType, copyRel.Id);
                        }
                    }
                }
            }
        }
        void WriteRelationships()
        {
            bool exists = Package.PartExists(RelationshipsPartUri);

            if (exists && Relationships.Count == 0)
            {
                Package.DeletePart(RelationshipsPartUri);
                return;
            }

            if (!exists)
            {
                PackagePart part = Package.CreatePart(RelationshipsPartUri, Package.RelationshipContentType);
                part.IsRelationship = true;
            }
            using (Stream s = Package.GetPart(RelationshipsPartUri).GetStream())
                Package.WriteRelationships(Relationships, s);
        }
 public static void AddFileToPackage(Package package, string uri, string filePath, string contentType)
 {
     FileInfo info = new FileInfo(filePath);
     uri = MakeUriSafe(uri);
     if (!info.Exists)
     {
         throw new FileNotFoundException(Strings.Package_FileCouldNotBeAdded, filePath);
     }
     Uri partUri = new Uri(uri, UriKind.Relative);
     string extension = info.Extension;
     if (package.PartExists(partUri))
     {
         package.DeletePart(partUri);
     }
     PackagePart part = package.CreatePart(partUri, contentType, CompressionOption.Maximum);
     using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
     {
         using (Stream stream2 = part.GetStream())
         {
             CopyStream(stream, stream2);
         }
         stream.Close();
     }
 }
 public static void RenamePath(Package package, string oldUri, string newUri)
 {
     Uri partUri = new Uri(oldUri, UriKind.Relative);
     Uri uri2 = new Uri(newUri, UriKind.Relative);
     if (package.PartExists(partUri))
     {
         PackagePart part = package.GetPart(partUri);
         PackagePart part2 = package.CreatePart(uri2, part.ContentType, part.CompressionOption);
         using (Stream stream = part.GetStream())
         {
             using (Stream stream2 = part2.GetStream())
             {
                 CopyStream(stream, stream2);
             }
         }
         package.DeletePart(partUri);
     }
 }
 public static void RemoveFileFromPackage(Package package, string uri)
 {
     Uri partUri = new Uri(uri, UriKind.Relative);
     if (package.PartExists(partUri))
     {
         package.DeletePart(partUri);
     }
 }
Exemple #7
0
 static void AddPart(Package zip, string root, string path) {
     Uri uri = PackUriHelper.CreatePartUri(new Uri(path, UriKind.Relative));
     if (zip.PartExists(uri)) {
         zip.DeletePart(uri);
     }
     PackagePart part = zip.CreatePart(uri, "", CompressionOption.Normal);
     using (FileStream fileStream = new FileStream(Path.Combine(root, path), FileMode.Open, FileAccess.Read)) {
         using (Stream dest = part.GetStream()) {
             CopyStream(fileStream, dest);
         }
     }
 }
Exemple #8
0
        /* S T M  C R E A T E  P A R T */
        /*----------------------------------------------------------------------------
            %%Function: StmCreatePart
            %%Qualified: ArbWeb.OOXML.StmCreatePart
            %%Contact: rlittle

        ----------------------------------------------------------------------------*/
        public static Stream StmCreatePart(Package pkg, string sUri, string sContentType, out PackagePart prt)
        {
            Uri uriTeams = new System.Uri(sUri, UriKind.Relative);

            prt = pkg.GetPart(uriTeams);

            List<PackageRelationship> plrel = new List<PackageRelationship>();

            foreach (PackageRelationship rel in prt.GetRelationships())
                {
                plrel.Add(rel);
                }

            prt = null;

            pkg.DeletePart(uriTeams);
            prt = pkg.CreatePart(uriTeams, sContentType);

            foreach (PackageRelationship rel in plrel)
                {
                prt.CreateRelationship(rel.TargetUri, rel.TargetMode, rel.RelationshipType, rel.Id);
                }

            return prt.GetStream(FileMode.Create, FileAccess.Write);
        }
Exemple #9
0
 private static void AddToArchive(Package zip, string fileToAdd, string root)
 {
     try
     {
         string uriFileName = fileToAdd.Replace(" ", "_");
         FileAttributes attr = System.IO.File.GetAttributes(fileToAdd);
         if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
         {
             root = root + "/" + Path.GetFileName(uriFileName);
             string[] subfolders = Directory.GetDirectories(fileToAdd);
             for (int i = 0; i < subfolders.Length; i++)
             {
                 AddToArchive(zip, subfolders[i], root);
             }
             string[] subfiles = Directory.GetFiles(fileToAdd);
             for (int i = 0; i < subfiles.Length; i++)
             {
                 AddToArchive(zip, subfiles[i], root);
             }
         }
         else
         {
             string zipUri = string.Concat(root + "/", Path.GetFileName(uriFileName));
             Uri partUri = new Uri(zipUri, UriKind.Relative);
             string contentType = System.Net.Mime.MediaTypeNames.Application.Zip;
             if (zip.PartExists(partUri)) zip.DeletePart(partUri);
             PackagePart pkgPart = zip.CreatePart(partUri, contentType, CompressionOption.Normal);
             Byte[] bytes = System.IO.File.ReadAllBytes(fileToAdd);
             pkgPart.GetStream().Write(bytes, 0, bytes.Length);
         }
     }
     catch (Exception ex)
     {
         TextWindow.WriteLine(fileToAdd);
         Utilities.OnError(Utilities.GetCurrentMethod(), ex);
     }
 }
Exemple #10
0
        private bool AddToArchive(Package zip, string fileToAdd,Backup bck)
        {
            // Replace spaces with an underscore (_)
            string uriFileName = fileToAdd.Replace(" ", "_");

            // A Uri always starts with a forward slash "/"
            string zipUri = string.Concat("/", Path.GetFileName(uriFileName));

            Uri partUri = new Uri(zipUri, UriKind.Relative);
            string contentType = MediaTypeNames.Application.Zip;

            if (!zip.PartExists(partUri))
            {
                zip.DeletePart(partUri);
            }
            if (!zip.PartExists(partUri))
            {
                PackagePart pkgPart = zip.CreatePart(partUri, contentType, ConvertCompIntoCompressOption(bck.compressLevel));

                // Read all of the bytes from the file to add to the zip file
                Byte[] bites = null;

                if (bck.isVss)
                {
                    MessageBox.Show(sShadowPath + fileToAdd);
                    //bites = wApi.GetFileData(sShadowPath + Path.GetFileName(fileToAdd));
                }
                else
                {
                    try
                    {
                        bites = File.ReadAllBytes(fileToAdd);
                    }
                    catch (Exception e)
                    {
                        bites = null;
                    }
                }

                //Compress and write the bytes to the zip file

                if (bites != null)
                {

                    pkgPart.GetStream().Write(bites, 0, bites.Length);
                    return true;
                }
                else
                    return false;

            }
            else
                return false;
        }