Ejemplo n.º 1
0
        /// <summary>
        /// Reads a file structure from an open stream.
        /// </summary>
        /// <param name="stream">Stream to read from.</param>
        /// <returns>File structure populated from the stream.</returns>
        public static FileStructure Read(Stream stream)
        {
            FileStructure fs = new FileStructure();

            using (NonClosingStreamWrapper wrapper = new NonClosingStreamWrapper(stream))
                using (BinaryReader reader = new BinaryReader(wrapper))
                {
                    fs.FileFormat = FileStructure.ReadFileFormat(reader);

                    if (FileFormat.Unknown != fs.FileFormat)
                    {
                        fs.embeddedFileSizes = FileStructure.ReadEmbeddedFileSizes(reader);

                        // Remember the data stream offset, which is right after the embedded files have been written.
                        fs.dataStreamOffset = stream.Position;
                        foreach (long size in fs.embeddedFileSizes)
                        {
                            fs.dataStreamOffset += size;
                        }
                    }
                }

            fs.stream = stream;

            return(fs);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Reads a file structure from an open stream.
        /// </summary>
        /// <param name="stream">Stream to read from.</param>
        /// <returns>File structure populated from the stream.</returns>
        public static FileStructure Read(Stream stream)
        {
            var fs = new FileStructure();

            using (var reader = new BinaryReader(stream, Encoding.UTF8, true))
            {
                fs.FileFormat = FileStructure.ReadFileFormat(reader);

                if (fs.FileFormat != FileFormat.Unknown)
                {
                    fs.embeddedFileSizes = FileStructure.ReadEmbeddedFileSizes(reader);

                    // Remember the data stream offset, which is right after the embedded files have been written.
                    fs.dataStreamOffset = stream.Position;
                    foreach (long size in fs.embeddedFileSizes)
                    {
                        fs.dataStreamOffset += size;
                    }
                }
            }

            fs.stream = stream;

            return(fs);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Probes a stream to determine the file format.
        /// </summary>
        /// <param name="stream">Stream to test.</param>
        /// <returns>The file format.</returns>
        public static FileFormat TestFileFormat(Stream stream)
        {
            FileFormat format = FileFormat.Unknown;

            long position = stream.Position;

            try
            {
                using (var reader = new BinaryReader(stream, Encoding.UTF8, true))
                {
                    format = FileStructure.ReadFileFormat(reader);
                }
            }
            finally
            {
                stream.Seek(position, SeekOrigin.Begin);
            }

            return(format);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Probes a stream to determine the file format.
        /// </summary>
        /// <param name="stream">Stream to test.</param>
        /// <returns>The file format.</returns>
        public static FileFormat TestFileFormat(Stream stream)
        {
            FileFormat format = FileFormat.Unknown;

            long position = stream.Position;

            try
            {
                using (NonClosingStreamWrapper wrapper = new NonClosingStreamWrapper(stream))
                    using (BinaryReader reader = new BinaryReader(wrapper))
                    {
                        format = FileStructure.ReadFileFormat(reader);
                    }
            }
            finally
            {
                stream.Seek(position, SeekOrigin.Begin);
            }

            return(format);
        }