/// <summary> /// </summary> /// <param name="stream">Stream on which the package is created</param> /// <param name="packageMode">FileMode in which the package is to be opened</param> /// <param name="packageAccess">FileAccess on the package that is opened</param> /// <returns>Package</returns> /// <exception cref="ArgumentNullException">If stream parameter is null</exception> /// <exception cref="ArgumentOutOfRangeException">If FileMode enumeration [packageMode] does not have one of the valid values</exception> /// <exception cref="ArgumentOutOfRangeException">If FileAccess enumeration [packageAccess] does not have one of the valid values</exception> /// <exception cref="IOException">If package to be created should have readwrite/read access and underlying stream is write only</exception> /// <exception cref="IOException">If package to be created should have readwrite/write access and underlying stream is read only</exception> public static Package Open(Stream stream, FileMode packageMode, FileAccess packageAccess) { Package package = null; if (stream == null) throw new ArgumentNullException(nameof(stream)); try { // Today the Open(Stream) method is purely used for streams of Zip file format as // that is the default underlying file format mapper implemented. package = new ZipPackage(stream, packageMode, packageAccess); //We need to get all the parts if any exists from the underlying file //so that we have the names in the Normalized form in our in-memory //data structures. //Note: If ever this call is removed, each individual call to GetPartCore, //may result in undefined behavior as the underlying ZipArchive, maintains the //files list as being case-sensitive. if (package.FileOpenAccess == FileAccess.ReadWrite || package.FileOpenAccess == FileAccess.Read) package.GetParts(); } catch { if (package != null) { package.Close(); } throw; } return package; }
/// <summary> /// This method gives the possibility of opening a package in streaming mode. /// When 'streaming' is true, the only allowed file modes are Create and CreateNew, /// the only allowed file access is Write and the only allowed FileShare values are /// Null and Read. /// </summary> /// <param name="path">Path to the package.</param> /// <param name="packageMode">FileMode in which the package should be opened.</param> /// <param name="packageAccess">FileAccess with which the package should be opened.</param> /// <param name="packageShare">FileShare with which the package is opened.</param> /// <param name="streaming">Whether to allow the creation of part pieces while enforcing write-once access.</param> /// <returns>Package</returns> /// <exception cref="ArgumentNullException">If path parameter is null</exception> /// <exception cref="ArgumentOutOfRangeException">If FileAccess enumeration [packageAccess] does not have one of the valid values</exception> /// <exception cref="ArgumentOutOfRangeException">If FileMode enumeration [packageMode] does not have one of the valid values</exception> internal static Package Open( string path, FileMode packageMode, FileAccess packageAccess, FileShare packageShare, bool streaming) { EventTrace.EasyTraceEvent(EventTrace.Keyword.KeywordXPS, EventTrace.Event.WClientDRXOpenPackageBegin); Package package = null; try { if (path == null) throw new ArgumentNullException("path"); ThrowIfFileModeInvalid(packageMode); ThrowIfFileAccessInvalid(packageAccess); ValidateStreamingAccess(packageMode, packageAccess, packageShare, streaming); //Note: FileShare enum is not being verfied at this stage, as we do not interpret the flag in this //code at all and just pass it on to the next layer, where the necessary validation can be //performed. Also, there is no meaningful way to check this parameter at this layer, as the //FileShare enumeration is a set of flags and flags/Bit-fields can be combined using a //bitwise OR operation to create different values, and validity of these values is specific to //the actual physical implementation. //Verify if this is valid for filenames FileInfo packageFileInfo = new FileInfo(path); try { package = new ZipPackage(packageFileInfo.FullName, packageMode, packageAccess, packageShare, streaming); if (!package._inStreamingCreation) // No read operation in streaming production. { //We need to get all the parts if any exists from the underlying file //so that we have the names in the Normalized form in our in-memory //data structures. //Note: If ever this call is removed, each individual call to GetPartCore, //may result in undefined behavior as the underlying ZipArchive, maintains the //files list as being case-sensitive. if (package.FileOpenAccess == FileAccess.ReadWrite || package.FileOpenAccess == FileAccess.Read) package.GetParts(); } } catch { if (package != null) { package.Close(); } throw; } } finally { EventTrace.EasyTraceEvent(EventTrace.Keyword.KeywordXPS, EventTrace.Event.WClientDRXOpenPackageEnd); } return package; }
/// <summary> /// </summary> /// <param name="path">Path to the package.</param> /// <param name="packageMode">FileMode in which the package should be opened.</param> /// <param name="packageAccess">FileAccess with which the package should be opened.</param> /// <param name="packageShare">FileShare with which the package is opened.</param> /// <returns>Package</returns> /// <exception cref="ArgumentNullException">If path parameter is null</exception> /// <exception cref="ArgumentOutOfRangeException">If FileAccess enumeration [packageAccess] does not have one of the valid values</exception> /// <exception cref="ArgumentOutOfRangeException">If FileMode enumeration [packageMode] does not have one of the valid values</exception> public static Package Open( string path, FileMode packageMode, FileAccess packageAccess, FileShare packageShare) { Package package = null; if (path == null) throw new ArgumentNullException(nameof(path)); ThrowIfFileModeInvalid(packageMode); ThrowIfFileAccessInvalid(packageAccess); if (packageMode == FileMode.OpenOrCreate && packageAccess != FileAccess.ReadWrite) throw new ArgumentException(SR.UnsupportedCombinationOfModeAccess); if (packageMode == FileMode.Create && packageAccess != FileAccess.ReadWrite) throw new ArgumentException(SR.UnsupportedCombinationOfModeAccess); if (packageMode == FileMode.CreateNew && packageAccess != FileAccess.ReadWrite) throw new ArgumentException(SR.UnsupportedCombinationOfModeAccess); if (packageMode == FileMode.Open && packageAccess == FileAccess.Write) throw new ArgumentException(SR.UnsupportedCombinationOfModeAccess); if (packageMode == FileMode.Truncate && packageAccess == FileAccess.Read) throw new ArgumentException(SR.UnsupportedCombinationOfModeAccess); if (packageMode == FileMode.Truncate) throw new NotSupportedException(SR.UnsupportedCombinationOfModeAccess); //Note: FileShare enum is not being verified at this stage, as we do not interpret the flag in this //code at all and just pass it on to the next layer, where the necessary validation can be //performed. Also, there is no meaningful way to check this parameter at this layer, as the //FileShare enumeration is a set of flags and flags/Bit-fields can be combined using a //bitwise OR operation to create different values, and validity of these values is specific to //the actual physical implementation. //Verify if this is valid for filenames FileInfo packageFileInfo = new FileInfo(path); try { package = new ZipPackage(packageFileInfo.FullName, packageMode, packageAccess, packageShare); package._openFileMode = packageMode; //We need to get all the parts if any exists from the underlying file //so that we have the names in the Normalized form in our in-memory //data structures. //Note: If ever this call is removed, each individual call to GetPartCore, //may result in undefined behavior as the underlying ZipArchive, maintains the //files list as being case-sensitive. if (package.FileOpenAccess == FileAccess.ReadWrite || package.FileOpenAccess == FileAccess.Read) package.GetParts(); } catch { if (package != null) { package.Close(); } throw; } return package; }
internal static Package Open(Stream stream, FileMode packageMode, FileAccess packageAccess, bool streaming) { EventTrace.EasyTraceEvent(EventTrace.Keyword.KeywordXPS, EventTrace.Event.WClientDRXOpenPackageBegin); Package package = null; try { if (stream == null) throw new ArgumentNullException("stream"); ValidateStreamingAccess(packageMode, packageAccess, null /* no FileShare info */, streaming); //FileMode and FileAccess Enums are validated in the following call Stream ensuredStream = ValidateModeAndAccess(stream, packageMode, packageAccess); try { // Today the Open(Stream) method is purely used for streams of Zip file format as // that is the default underlying file format mapper implemented. package = new ZipPackage(ensuredStream, packageMode, packageAccess, streaming); if (!package._inStreamingCreation) // No read operation in streaming production. { //We need to get all the parts if any exists from the underlying file //so that we have the names in the Normalized form in our in-memory //data structures. //Note: If ever this call is removed, each individual call to GetPartCore, //may result in undefined behavior as the underlying ZipArchive, maintains the //files list as being case-sensitive. if (package.FileOpenAccess == FileAccess.ReadWrite || package.FileOpenAccess == FileAccess.Read) package.GetParts(); } } catch { if (package != null) { package.Close(); } throw; } } finally { EventTrace.EasyTraceEvent(EventTrace.Keyword.KeywordXPS, EventTrace.Event.WClientDRXOpenPackageEnd); } return package; }