/// <summary> /// /// </summary> /// <param name="input"></param> /// <param name="output"></param> /// <param name="Password"></param> /// <param name="cancellationToken"></param> private async Task LoadAsync(Stream input, Stream output, string Password, CancellationToken cancellationToken) { ReleaseResources(); if (input.CanSeek && input.Length == 0) // Template is blank, Construct new { _stream = output; await ConstructNewFileAsync(Password, cancellationToken).ConfigureAwait(false); } else { Stream ms; _stream = output; if (Password != null) { using (var encrStream = RecyclableMemory.GetStream()) { await CopyStreamAsync(input, encrStream, cancellationToken).ConfigureAwait(false); var eph = new EncryptedPackageHandler(); Encryption.Password = Password; ms = eph.DecryptPackage(encrStream, Encryption); } } else { ms = RecyclableMemory.GetStream(); await CopyStreamAsync(input, ms, cancellationToken).ConfigureAwait(false); } try { _zipPackage = new Packaging.ZipPackage(ms); } catch (Exception ex) { if (Password == null && await CompoundDocumentFile.IsCompoundDocumentAsync((MemoryStream)_stream, cancellationToken).ConfigureAwait(false)) { 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); } throw; } finally { ms.Dispose(); } } //Clear the workbook so that it gets reinitialized next time this._workbook = null; }
private async Task ConstructNewFileAsync(string password) { var ms = new MemoryStream(); if (_stream == null) { _stream = new MemoryStream(); } if (File != null) { File.Refresh(); } if (File != null && File.Exists) { if (password != null) { var encrHandler = new EncryptedPackageHandler(); Encryption.IsEncrypted = true; Encryption.Password = password; ms = encrHandler.DecryptPackage(File, Encryption); encrHandler = null; } else { await WriteFileToStreamAsync(File.FullName, ms); } try { _package = new Packaging.ZipPackage(ms); } catch (Exception ex) { if (password == null && await CompoundDocumentFile.IsCompoundDocumentAsync(File).ConfigureAwait(false)) { 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; } } } else { _package = new Packaging.ZipPackage(ms); CreateBlankWb(); } }