Пример #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) != -1)
                    {
                    }
                }
            }

            byte[] bytes = header.GetBytes();

            int headerRead = inStream.Read(bytes, 0, header.HeaderSize);

            if (headerRead < 512)
            {
                throw new TarException("Can not read header");
            }

            if (IsEmpty(bytes))
            {
                headerRead = inStream.Read(bytes, 0, header.HeaderSize);
                if (headerRead == 512 && 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);
        }
Пример #2
0
        public bool MoveNext(bool skipData)
        {
            if (remainingBytesInFile > 0L)
            {
                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.");
                }
                if (inStream.CanSeek)
                {
                    var num = remainingBytesInFile % 512L;
                    inStream.Seek(remainingBytesInFile + (512L - (num == 0L ? 512L : num)), SeekOrigin.Current);
                }
                else
                {
                    byte[] buffer;
                    while (Read(out buffer) != -1)
                    {
                        ;
                    }
                }
            }
            var bytes = header.GetBytes();

            if (inStream.Read(bytes, 0, header.HeaderSize) < 512)
            {
                throw new TarException("Can not read header");
            }
            if (IsEmpty(bytes))
            {
                if ((inStream.Read(bytes, 0, header.HeaderSize) == 512) && IsEmpty(bytes))
                {
                    return(false);
                }
                throw new TarException("Broken archive");
            }
            if (header.UpdateHeaderFromBytes())
            {
                throw new TarException("Checksum check failed");
            }
            remainingBytesInFile = header.SizeInBytes;
            return(true);
        }