コード例 #1
0
        /// <summary>
        /// Move internal pointer to a next file in archive.
        /// </summary>
        /// <param name="skipData">Should be true if you want to read a header only, otherwise false</param>
        /// <returns>false on End Of File otherwise true</returns>
        ///
        /// Example:
        /// while(MoveNext())
        /// {
        ///     Read(dataDestStream);
        /// }
        /// <seealso cref="Read(Stream)"/>
        public bool MoveNext(bool skipData)
        {
            Debug.WriteLine("tar stream position MoveNext in: " + inStream.Position);
            if (remainingBytesInFile > 0)
            {
                if (!skipData)
                {
                    throw new TarException(
                              "You are trying to change file while not all the data from the previous one was read. If you do want to skip files use skipData parameter set to true.");
                }

                // Skip to the end of file.
                if (inStream.CanSeek)
                {
                    long remainer = (remainingBytesInFile % 512);
                    inStream.Seek(remainingBytesInFile + (512 - (remainer == 0 ? 512 : remainer)), SeekOrigin.Current);
                }
                else
                {
                    byte[] buffer;
                    while (Read(out buffer) > 0)
                    {
                    }
                }
            }

            byte[] bytes = header.GetBytes();
            int    headerRead;
            int    bytesRemaining = header.HeaderSize;

            do
            {
                headerRead      = inStream.Read(bytes, header.HeaderSize - bytesRemaining, bytesRemaining);
                bytesRemaining -= headerRead;
                if (headerRead <= 0 && bytesRemaining > 0)
                {
                    throw new TarException("Can not read header");
                }
            } while (bytesRemaining > 0);

            if (IsEmpty(bytes))
            {
                bytesRemaining = header.HeaderSize;
                do
                {
                    headerRead      = inStream.Read(bytes, header.HeaderSize - bytesRemaining, bytesRemaining);
                    bytesRemaining -= headerRead;
                    if (headerRead <= 0 && bytesRemaining > 0)
                    {
                        throw new TarException("Broken archive");
                    }
                } while (bytesRemaining > 0);

                if (bytesRemaining == 0 && IsEmpty(bytes))
                {
                    Debug.WriteLine("tar stream position MoveNext  out(false): " + inStream.Position);
                    return(false);
                }

                throw new TarException("Broken archive");
            }

            if (header.UpdateHeaderFromBytes())
            {
                throw new TarException("Checksum check failed");
            }

            remainingBytesInFile = header.SizeInBytes;

            Debug.WriteLine("tar stream position MoveNext  out(true): " + inStream.Position);
            return(true);
        }