Flush() public method

public Flush ( ) : void
return void
Ejemplo n.º 1
0
        void testingabpckg()
        {
            //try
            //{

            string InputFolder = @"C:\Users\inbakad\Desktop\input";

            //------------

            using (System.IO.Packaging.Package ABpackage = System.IO.Packaging.Package.Open(@"C:\Users\inbakad\Desktop\output\zenon.abpkg", FileMode.Create))
            {
                //get all files to be included in package in corresponding folder
                string[] filenames = Directory.GetFiles(InputFolder, "*.*", SearchOption.AllDirectories);
                foreach (string file in filenames)
                {
                    //create packagePart for each file in folder
                    //uri shall be relative --> remove absolute path, uri shall start with "/", uri doesn't accept blanks --> replace with %20
                    if (!InputFolder.EndsWith(Path.DirectorySeparatorChar.ToString()))
                    {
                        InputFolder = InputFolder + Path.DirectorySeparatorChar;
                    }
                    string fileRelativePath = file.ToString().Replace(InputFolder, "/");
                    //prepare path for uri:
                    //fileRelativePath = fileRelativePath.Replace(" ", "%20");
                    //fileRelativePath = fileRelativePath.Replace("{", "%7b");
                    //fileRelativePath = fileRelativePath.Replace("}", "%7d");
                    //convert path info into uri format
                    fileRelativePath = fileRelativePath.Replace("\\", "/");
                    var    packagePartUri = PackUriHelper.CreatePartUri(new Uri(fileRelativePath, UriKind.Relative));
                    string extension      = Path.GetExtension(@file);
                    if (string.IsNullOrEmpty(extension))
                    {
                        extension = ".%00";
                    }
                    var packagePart = ABpackage.CreatePart(packagePartUri, "part/" + extension);
                    //get file content and write to package part
                    using (var packageStream = packagePart.GetStream(FileMode.Create, FileAccess.ReadWrite))
                    {
                        try
                        {
                            using (FileStream currentFileStream = new FileStream(file, FileMode.Open, FileAccess.Read))
                            {
                                currentFileStream.CopyTo(packageStream);
                                Console.WriteLine("Added file: " + file);
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                        ABpackage.CreateRelationship(packagePart.Uri, TargetMode.Internal, "rel");
                    }
                }
                // Flush package to ensure all data is written to it
                ABpackage.Flush();
            }


            //----------------------
            //using (System.IO.Packaging.Package ABpackage = System.IO.Packaging.Package.Open(@"C:\Users\inbakad\Desktop\output\test.abpkg", FileMode.Create)) ;
            //{

            //   // get all files to be included in package in corresponding folder
            //    string[] filenames = Directory.GetFiles(InputFolder, "*.*", SearchOption.AllDirectories);
            //   // string[] encodefiles = new string[filenames.Length];
            //    int I = 0;
            //    foreach (string file in filenames)
            //    {
            //      //  create packagePart for each file in folder

            //      //  uri shall be relative-- > remove absolute path, uri shall start with "/", uri doesn't accept blanks --> replace with %20
            //        if (!InputFolder.EndsWith(Path.DirectorySeparatorChar.ToString()))
            //                InputFolder = InputFolder + Path.DirectorySeparatorChar;
            //        string fileRelativePath = file.ToString().Replace(InputFolder, "/");


            //        var packagePartUri = PackUriHelper.CreatePartUri(new Uri(fileRelativePath, UriKind.Relative));
            //        var pckg = PackUriHelper.CreatePartUri(new Uri(fileRelativePath));
            //       // encodefiles[I] = Uri.EscapeDataString(fileRelativePath);
            //       // I++;
            //        string escaprstr = Uri.EscapeDataString(filePath);
            //        //prepare path for uri:
            //        //remove blanks
            //        fileRelativePath = fileRelativePath.Replace(" ", "%20");
            //        fileRelativePath = fileRelativePath.Replace("{", "%7b");
            //        fileRelativePath = fileRelativePath.Replace("}", "%7d");
            //        convert path info into uri format
            //          fileRelativePath = fileRelativePath.Replace("\\", "/");
            //        var packagePartUri = PackUriHelper.CreatePartUri(new Uri(fileRelativePath, UriKind.Relative));
            //        // string extension = Path.GetExtension(@file);
            //        if (string.IsNullOrEmpty(extension))
            //            extension = ".%00";
            //        var packagePart = ABpackage.CreatePart(packagePartUri, "part/" + extension);
            //        get file content and write to package part
            //        using (var packageStream = packagePart.GetStream(FileMode.Create, FileAccess.ReadWrite))
            //        {
            //            try
            //            {
            //                using (FileStream currentFileStream = new FileStream(file, FileMode.Open, FileAccess.Read))
            //                {
            //                    currentFileStream.CopyTo(packageStream);
            //                    Console.WriteLine("Added file: " + file);
            //                }
            //            }
            //            catch (Exception ex)
            //            {
            //                Console.WriteLine(ex.Message);
            //            }
            //            // ABpackage.CreateRelationship(packagePart.Uri, TargetMode.Internal, "rel");
            //        }
            //    }
            // Flush package to ensure all data is written to it

            //}
            //ABpackage.Flush();
            // return encodefiles ;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Save the package to a file.
        /// </summary>
        /// <param name="path">The file path of the package. Any existing file will be overwritten.</param>
        /// <param name="rsaParamsXml">RSA key serialized to XML. If null, save an unencrypted package.</param>
        public void SaveAs(string path, string rsaParamsXml)
        {
            MemoryStream ms;
            bool needContentHack = IsNew;
            //
            CondCreateStream();
            SaveManifest();
            _package.Flush();
            if (needContentHack) // Flush() doesn't flush the [Content_Types].xml file! Arghh!
            {
                _package.Close();
                _stream.Position = 0;
                _package = Package.Open(_stream, FileMode.Open, FileAccess.ReadWrite);
                _package.Flush();
            }
            _stream.Flush();

            ms = Encrypt(_stream, rsaParamsXml);

            using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
            {
                ms.Position = 0;
                CopyStream(fs, ms);
            }
            _path = path;
        }