Exemple #1
0
        /// <summary>
        /// Reads a single file from an archive stream.
        /// </summary>
        /// <param name="stream">A stream for reading the archive.</param>
        /// <param name="path">The path of the file within the archive
        /// (not the external file path).</param>
        /// <returns>A stream for reading the extracted file, or null
        /// if the file does not exist in the archive.</returns>
        /// <exception cref="ArchiveException">The stream is not a valid
        /// archive.</exception>
        /// <remarks>The entire extracted file is cached in memory, so this
        /// method requires enough free memory to hold the file.</remarks>
        public Stream Unpack(Stream stream, string path)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            BasicUnpackStreamContext streamContext =
                new BasicUnpackStreamContext(stream);

            this.Unpack(
                streamContext,
                delegate(string match)
            {
                return(String.Compare(
                           match, path, StringComparison.OrdinalIgnoreCase) == 0);
            });

            Stream extractStream = streamContext.FileStream;

            if (extractStream != null)
            {
                extractStream.Position = 0;
            }

            return(extractStream);
        }
        /// <summary>
        /// Reads a single file from an archive stream.
        /// </summary>
        /// <param name="stream">A stream for reading the archive.</param>
        /// <param name="path">The path of the file within the archive
        /// (not the external file path).</param>
        /// <returns>A stream for reading the extracted file, or null
        /// if the file does not exist in the archive.</returns>
        /// <exception cref="ArchiveException">The stream is not a valid
        /// archive.</exception>
        /// <remarks>The entire extracted file is cached in memory, so this
        /// method requires enough free memory to hold the file.</remarks>
        public Stream Unpack(Stream stream, string path)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            BasicUnpackStreamContext streamContext =
                new BasicUnpackStreamContext(stream);
            this.Unpack(
                streamContext,
                delegate(string match)
                {
                    return String.Compare(
                        match, path, StringComparison.OrdinalIgnoreCase) == 0;
                });

            Stream extractStream = streamContext.FileStream;
            if (extractStream != null)
            {
                extractStream.Position = 0;
            }

            return extractStream;
        }