Exemple #1
0
        static Package OpenCore(Stream stream, FileMode packageMode, FileAccess packageAccess, bool ownsStream)
        {
            if ((packageAccess & FileAccess.Read) == FileAccess.Read && !stream.CanRead)
            {
                throw new IOException("Stream does not support reading");
            }

            if ((packageAccess & FileAccess.Write) == FileAccess.Write && !stream.CanWrite)
            {
                throw new IOException("Stream does not support reading");
            }

            if (!stream.CanSeek)
            {
                throw new ArgumentException("stream", "Stream must support seeking");
            }

            if (packageMode == FileMode.Open && stream.Length == 0)
            {
                throw new FileFormatException("Stream length cannot be zero with FileMode.Open");
            }

            if (packageMode == FileMode.CreateNew && stream.Length > 0)
            {
                throw new IOException("Cannot use CreateNew when stream contains data");
            }

            if (packageMode == FileMode.Append || packageMode == FileMode.Truncate)
            {
                if (stream.CanWrite)
                {
                    throw new NotSupportedException(string.Format("PackageMode.{0} is not supported", packageMode));
                }
                else
                {
                    throw new IOException(string.Format("PackageMode.{0} is not supported", packageMode));
                }
            }

            // Test to see if archive is valid
            if (stream.Length > 0 && packageAccess == FileAccess.Read)
            {
                try
                {
                    using (zipsharp.UnzipArchive a = new zipsharp.UnzipArchive(stream))
                    {
                    }
                }
                catch
                {
                    throw new FileFormatException("The specified archive is invalid.");
                }
            }

            return(new ZipPackage(packageAccess, ownsStream, stream));
        }
		static Package OpenCore (Stream stream, FileMode packageMode, FileAccess packageAccess)
		{
			if ((packageAccess & FileAccess.Read) == FileAccess.Read && !stream.CanRead)
				throw new IOException ("Stream does not support reading");

			if ((packageAccess & FileAccess.Write) == FileAccess.Write && !stream.CanWrite)
				throw new IOException ("Stream does not support reading");
			
			if (!stream.CanSeek)
				throw new ArgumentException ("stream", "Stream must support seeking");
			
			if (packageMode == FileMode.Open && stream.Length == 0)
				throw new FileFormatException("Stream length cannot be zero with FileMode.Open");

			if (packageMode == FileMode.CreateNew && stream.Length > 0)
				throw new IOException ("Cannot use CreateNew when stream contains data");

			if (packageMode == FileMode.Append || packageMode == FileMode.Truncate)
			{
				if (stream.CanWrite)
					throw new NotSupportedException (string.Format("PackageMode.{0} is not supported", packageMode));
				else
					throw new IOException (string.Format("PackageMode.{0} is not supported", packageMode));
			}

			// Test to see if archive is valid
			if (stream.Length > 0 && packageAccess == FileAccess.Read) {
				try {
					using (zipsharp.UnzipArchive a = new zipsharp.UnzipArchive (stream)) {
					}
				} catch {
					throw new FileFormatException ("The specified archive is invalid.");
				}
			}
			
			return new ZipPackage (packageAccess, stream);
		}