コード例 #1
0
        public TarArchiveEntryStream(TarArchive tar, TarArchiveEntry entry)
        {
            if (entry is null)
            {
                throw new ArgumentNullException(nameof(entry));
            }

            this.tar    = tar ?? throw new ArgumentNullException(nameof(tar));
            entryStream = entry.Open();
        }
コード例 #2
0
        public TarArchive(Stream stream)
        {
            if (stream is null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            entries = new List <TarArchiveEntry>();

            var header = new byte[512];

            var zeroBlockCount = 0;
            var position       = 0L;

            while (zeroBlockCount < 2)
            {
                var length = stream.Read(header, 0, header.Length);
                if (length < 512)
                {
                    throw new InvalidDataException($"Invalid header block size < 512");
                }

                position += length;

                if (IsAllZeros(header))
                {
                    ++zeroBlockCount;
                }
                else
                {
                    zeroBlockCount = 0;

                    var fileName = GetString(header, 0, 100);
                    ValidateChecksum(header, fileName);
                    var fileLength = ReadFileSize(header, fileName);

                    if (IsFileType(header))
                    {
                        var lastModifiedTime = ReadLastModifiedTime(header, fileName);
                        fileName = AddFilePrefix(header, fileName);
                        var entry = new TarArchiveEntry(stream, fileName, lastModifiedTime.DateTime.ToLocalTime(), fileLength);
                        position += fileLength;
                        position  = SeekToNextEntry(header, stream, position, fileName);
                        entries.Add(entry);
                    }
                }
            }
        }