CopyStream() приватный статический Метод

Copies the input stream to the output stream.
private static CopyStream ( Stream inputStream, Stream &outputStream ) : void
inputStream Stream The input stream.
outputStream Stream The output stream.
Результат void
Пример #1
0
        /// <summary>
        /// Copies the Package to the Outstream
        /// The package is closed after it has been saved
        /// </summary>
        /// <param name="outputStream">The stream to copy the package to</param>
        public void SaveAs(Stream outputStream)
        {
            this.File = null;
            this.Save();

            if (outputStream != _stream)
            {
                if (this.Encryption.IsEncrypted)
                {
#if !MONO
                    //Encrypt Workbook
                    Byte[] file = new byte[this.Stream.Length];
                    long   pos  = this.Stream.Position;
                    this.Stream.Seek(0, SeekOrigin.Begin);
                    this.Stream.Read(file, 0, (int)this.Stream.Length);
                    EncryptedPackageHandler eph = new EncryptedPackageHandler();
                    var ms = eph.EncryptPackage(file, this.Encryption);
                    ExcelPackage.CopyStream(ms, ref outputStream);
#endif
#if MONO
                    throw new NotSupportedException("Encryption is not supported under Mono.");
#endif
                }
                else
                {
                    ExcelPackage.CopyStream(_stream, ref outputStream);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Saves all the components back into the package.
        /// This method recursively calls the Save method on all sub-components.
        /// We close the package after the save is done.
        /// </summary>
        public void Save()
        {
            try
            {
                if (this.Stream is MemoryStream && this.Stream.Length > 0)
                {
                    //Close any open memorystream and "renew" then. This can occure if the package is saved twice.
                    //The stream is left open on save to enable the user to read the stream-property.
                    //Non-memorystream streams will leave the closing to the user before saving a second time.
                    this.CloseStream();
                }

                this.Workbook.Save();
                if (this.File == null)
                {
                    if (this.Encryption.IsEncrypted)
                    {
#if !MONO
                        var ms = new MemoryStream();
                        this.Package.Save(ms);
                        byte[] file = ms.ToArray();
                        EncryptedPackageHandler eph = new EncryptedPackageHandler();
                        var msEnc = eph.EncryptPackage(file, Encryption);
                        ExcelPackage.CopyStream(msEnc, ref _stream);
#endif
#if MONO
                        throw new NotSupportedException("Encryption is not supported under Mono.");
#endif
                    }
                    else
                    {
                        this.Package.Save(this.Stream);
                    }
                    this.Stream.Flush();
                    this.Package.Close();
                }
                else
                {
                    if (System.IO.File.Exists(File.FullName))
                    {
                        try
                        {
                            System.IO.File.Delete(File.FullName);
                        }
                        catch (Exception ex)
                        {
                            throw (new Exception(string.Format("Error overwriting file {0}", File.FullName), ex));
                        }
                    }

                    this.Package.Save(this.Stream);
                    this.Package.Close();
                    if (this.Stream is MemoryStream)
                    {
                        var fi = new FileStream(this.File.FullName, FileMode.Create);
                        //EncryptPackage
                        if (this.Encryption.IsEncrypted)
                        {
#if !MONO
                            byte[] file = ((MemoryStream)this.Stream).ToArray();
                            EncryptedPackageHandler eph = new EncryptedPackageHandler();
                            var ms = eph.EncryptPackage(file, Encryption);

                            fi.Write(ms.GetBuffer(), 0, (int)ms.Length);
#endif
#if MONO
                            throw new NotSupportedException("Encryption is not supported under Mono.");
#endif
                        }
                        else
                        {
                            fi.Write(((MemoryStream)this.Stream).GetBuffer(), 0, (int)this.Stream.Length);
                        }
                        fi.Close();
                    }
                    else
                    {
                        System.IO.File.WriteAllBytes(this.File.FullName, this.GetAsByteArray(false));
                    }
                }
            }
            catch (Exception ex)
            {
                if (this.File == null)
                {
                    throw;
                }
                else
                {
                    throw (new InvalidOperationException(string.Format("Error saving file {0}", this.File.FullName), ex));
                }
            }
        }
Пример #3
0
        private void Load(Stream input, Stream output, string password)
        {
            //Release some resources:
            if (this._package != null)
            {
                this._package.Close();
                this._package = null;
            }
            if (this._stream != null)
            {
                this._stream.Close();
                this._stream.Dispose();
                this._stream = null;
            }
            this._isExternalStream = true;
            if (input.Length == 0)             // Template is blank, Construct new
            {
                this._stream = output;
                this.ConstructNewFile(password);
            }
            else
            {
                Stream ms;
                this._stream = output;
                if (password != null)
                {
#if !MONO
                    Stream encrStream = new MemoryStream();
                    ExcelPackage.CopyStream(input, ref encrStream);
                    EncryptedPackageHandler eph = new EncryptedPackageHandler();
                    this.Encryption.Password = password;
                    ms = eph.DecryptPackage((MemoryStream)encrStream, this.Encryption);
#endif
#if MONO
                    throw new NotSupportedException("Encryption is not supported under Mono.");
#endif
                }
                else
                {
                    ms = new MemoryStream();
                    ExcelPackage.CopyStream(input, ref ms);
                }

                try
                {
                    //this._package = Package.Open(this._stream, FileMode.Open, FileAccess.ReadWrite);
                    this._package = new Packaging.ZipPackage(ms);
                }
                catch (Exception ex)
                {
#if !MONO
                    EncryptedPackageHandler eph = new EncryptedPackageHandler();
                    if (password == null && CompoundDocument.IsStorageILockBytes(CompoundDocument.GetLockbyte((MemoryStream)_stream)) == 0)
                    {
                        throw new Exception("Can not open the package. Package is an OLE compound document. If this is an encrypted package, please supply the password", ex);
                    }
                    else
                    {
                        throw;
                    }
#endif
#if MONO
                    throw;
#endif
                }
            }
            //Clear the workbook so that it gets reinitialized next time
            this._workbook = null;
        }