123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695 |
- using System;
- using System.IO;
- using System.Text;
- namespace CommonMPQ.SharpZipLib.Tar
- {
-
-
-
-
-
-
-
- public class TarInputStream : Stream
- {
- #region Constructors
-
-
-
-
- public TarInputStream(Stream inputStream)
- : this(inputStream, TarBuffer.DefaultBlockFactor)
- {
- }
-
-
-
-
-
- public TarInputStream(Stream inputStream, int blockFactor)
- {
- this.inputStream = inputStream;
- tarBuffer = TarBuffer.CreateInputTarBuffer(inputStream, blockFactor);
- }
- #endregion
-
-
-
-
- public bool IsStreamOwner
- {
- get { return tarBuffer.IsStreamOwner; }
- set { tarBuffer.IsStreamOwner = value; }
- }
- #region Stream Overrides
-
-
-
- public override bool CanRead
- {
- get {
- return inputStream.CanRead;
- }
- }
-
-
-
-
-
- public override bool CanSeek {
- get {
- return false;
- }
- }
-
-
-
-
-
- public override bool CanWrite {
- get {
- return false;
- }
- }
-
-
-
-
- public override long Length {
- get {
- return inputStream.Length;
- }
- }
-
-
-
-
-
-
- public override long Position {
- get {
- return inputStream.Position;
- }
- set {
- throw new NotSupportedException("TarInputStream Seek not supported");
- }
- }
-
-
-
-
- public override void Flush()
- {
- inputStream.Flush();
- }
-
-
-
-
-
-
-
-
- public override long Seek(long offset, SeekOrigin origin)
- {
- throw new NotSupportedException("TarInputStream Seek not supported");
- }
-
-
-
-
-
-
-
- public override void SetLength(long value)
- {
- throw new NotSupportedException("TarInputStream SetLength not supported");
- }
-
-
-
-
-
-
-
-
-
- public override void Write(byte[] buffer, int offset, int count)
- {
- throw new NotSupportedException("TarInputStream Write not supported");
- }
-
-
-
-
-
-
-
- public override void WriteByte(byte value)
- {
- throw new NotSupportedException("TarInputStream WriteByte not supported");
- }
-
-
-
-
- public override int ReadByte()
- {
- byte[] oneByteBuffer = new byte[1];
- int num = Read(oneByteBuffer, 0, 1);
- if (num <= 0)
- {
-
- return -1;
- }
- return oneByteBuffer[0];
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public override int Read(byte[] buffer, int offset, int count)
- {
- if ( buffer == null )
- {
- throw new ArgumentNullException("buffer");
- }
- int totalRead = 0;
-
- if (entryOffset >= entrySize)
- {
- return 0;
- }
-
- long numToRead = count;
-
- if ((numToRead + entryOffset) > entrySize)
- {
- numToRead = entrySize - entryOffset;
- }
-
- if (readBuffer != null)
- {
- int sz = (numToRead > readBuffer.Length) ? readBuffer.Length : (int)numToRead;
-
- Array.Copy(readBuffer, 0, buffer, offset, sz);
-
- if (sz >= readBuffer.Length)
- {
- readBuffer = null;
- }
- else
- {
- int newLen = readBuffer.Length - sz;
- byte[] newBuf = new byte[newLen];
- Array.Copy(readBuffer, sz, newBuf, 0, newLen);
- readBuffer = newBuf;
- }
-
- totalRead += sz;
- numToRead -= sz;
- offset += sz;
- }
-
- while (numToRead > 0)
- {
- byte[] rec = tarBuffer.ReadBlock();
- if (rec == null)
- {
-
- throw new TarException("unexpected EOF with " + numToRead + " bytes unread");
- }
-
- int sz = (int)numToRead;
- int recLen = rec.Length;
-
- if (recLen > sz)
- {
- Array.Copy(rec, 0, buffer, offset, sz);
- readBuffer = new byte[recLen - sz];
- Array.Copy(rec, sz, readBuffer, 0, recLen - sz);
- }
- else
- {
- sz = recLen;
- Array.Copy(rec, 0, buffer, offset, recLen);
- }
-
- totalRead += sz;
- numToRead -= sz;
- offset += sz;
- }
-
- entryOffset += totalRead;
-
- return totalRead;
- }
-
-
-
-
-
- public override void Close()
- {
- tarBuffer.Close();
- }
-
- #endregion
-
-
-
-
- public void SetEntryFactory(IEntryFactory factory)
- {
- entryFactory = factory;
- }
-
-
-
-
- public int RecordSize
- {
- get { return tarBuffer.RecordSize; }
- }
-
-
-
-
-
-
- [Obsolete("Use RecordSize property instead")]
- public int GetRecordSize()
- {
- return tarBuffer.RecordSize;
- }
-
-
-
-
-
-
-
-
-
-
-
- public long Available {
- get {
- return entrySize - entryOffset;
- }
- }
-
-
-
-
-
-
-
-
-
-
- public void Skip(long skipCount)
- {
-
-
-
-
- byte[] skipBuf = new byte[8 * 1024];
-
- for (long num = skipCount; num > 0;) {
- int toRead = num > skipBuf.Length ? skipBuf.Length : (int)num;
- int numRead = Read(skipBuf, 0, toRead);
-
- if (numRead == -1) {
- break;
- }
-
- num -= numRead;
- }
- }
-
-
-
-
-
- public bool IsMarkSupported {
- get {
- return false;
- }
- }
-
-
-
-
-
-
-
- public void Mark(int markLimit)
- {
- }
-
-
-
-
- public void Reset()
- {
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public TarEntry GetNextEntry()
- {
- if (hasHitEOF) {
- return null;
- }
-
- if (currentEntry != null) {
- SkipToNextEntry();
- }
-
- byte[] headerBuf = tarBuffer.ReadBlock();
-
- if (headerBuf == null) {
- hasHitEOF = true;
- } else if (TarBuffer.IsEndOfArchiveBlock(headerBuf)) {
- hasHitEOF = true;
- }
-
- if (hasHitEOF) {
- currentEntry = null;
- } else {
- try {
- TarHeader header = new TarHeader();
- header.ParseBuffer(headerBuf);
- if ( !header.IsChecksumValid )
- {
- throw new TarException("Header checksum is invalid");
- }
- this.entryOffset = 0;
- this.entrySize = header.Size;
-
- StringBuilder longName = null;
-
- if (header.TypeFlag == TarHeader.LF_GNU_LONGNAME) {
-
- byte[] nameBuffer = new byte[TarBuffer.BlockSize];
- long numToRead = this.entrySize;
-
- longName = new StringBuilder();
-
- while (numToRead > 0) {
- int numRead = this.Read(nameBuffer, 0, (numToRead > nameBuffer.Length ? nameBuffer.Length : (int)numToRead));
-
- if (numRead == -1) {
- throw new InvalidHeaderException("Failed to read long name entry");
- }
-
- longName.Append(TarHeader.ParseName(nameBuffer, 0, numRead).ToString());
- numToRead -= numRead;
- }
-
- SkipToNextEntry();
- headerBuf = this.tarBuffer.ReadBlock();
- } else if (header.TypeFlag == TarHeader.LF_GHDR) {
-
- SkipToNextEntry();
- headerBuf = this.tarBuffer.ReadBlock();
- } else if (header.TypeFlag == TarHeader.LF_XHDR) {
-
- SkipToNextEntry();
- headerBuf = this.tarBuffer.ReadBlock();
- } else if (header.TypeFlag == TarHeader.LF_GNU_VOLHDR) {
-
- SkipToNextEntry();
- headerBuf = this.tarBuffer.ReadBlock();
- } else if (header.TypeFlag != TarHeader.LF_NORMAL &&
- header.TypeFlag != TarHeader.LF_OLDNORM &&
- header.TypeFlag != TarHeader.LF_DIR) {
-
- SkipToNextEntry();
- headerBuf = tarBuffer.ReadBlock();
- }
-
- if (entryFactory == null) {
- currentEntry = new TarEntry(headerBuf);
- if (longName != null) {
- currentEntry.Name = longName.ToString();
- }
- } else {
- currentEntry = entryFactory.CreateEntry(headerBuf);
- }
-
-
-
-
- entryOffset = 0;
-
-
- entrySize = this.currentEntry.Size;
- } catch (InvalidHeaderException ex) {
- entrySize = 0;
- entryOffset = 0;
- currentEntry = null;
- string errorText = string.Format("Bad header in record {0} block {1} {2}",
- tarBuffer.CurrentRecord, tarBuffer.CurrentBlock, ex.Message);
- throw new InvalidHeaderException(errorText);
- }
- }
- return currentEntry;
- }
-
-
-
-
-
-
-
-
- public void CopyEntryContents(Stream outputStream)
- {
- byte[] tempBuffer = new byte[32 * 1024];
-
- while (true) {
- int numRead = Read(tempBuffer, 0, tempBuffer.Length);
- if (numRead <= 0) {
- break;
- }
- outputStream.Write(tempBuffer, 0, numRead);
- }
- }
- void SkipToNextEntry()
- {
- long numToSkip = entrySize - entryOffset;
-
- if (numToSkip > 0)
- {
- Skip(numToSkip);
- }
-
- readBuffer = null;
- }
-
-
-
-
-
-
- public interface IEntryFactory
- {
-
-
-
-
-
-
-
- TarEntry CreateEntry(string name);
-
-
-
-
-
-
-
-
-
-
- TarEntry CreateEntryFromFile(string fileName);
-
-
-
-
-
-
-
-
-
-
- TarEntry CreateEntry(byte[] headerBuffer);
- }
-
-
-
- public class EntryFactoryAdapter : IEntryFactory
- {
-
-
-
-
-
- public TarEntry CreateEntry(string name)
- {
- return TarEntry.CreateTarEntry(name);
- }
-
-
-
-
-
-
- public TarEntry CreateEntryFromFile(string fileName)
- {
- return TarEntry.CreateEntryFromFile(fileName);
- }
-
-
-
-
-
- public TarEntry CreateEntry(byte[] headerBuffer)
- {
- return new TarEntry(headerBuffer);
- }
- }
- #region Instance Fields
-
-
-
- protected bool hasHitEOF;
-
-
-
-
- protected long entrySize;
-
-
-
-
- protected long entryOffset;
-
-
-
- protected byte[] readBuffer;
-
-
-
-
- protected TarBuffer tarBuffer;
-
-
-
-
- TarEntry currentEntry;
-
-
-
-
- protected IEntryFactory entryFactory;
-
-
-
-
- readonly Stream inputStream;
- #endregion
- }
- }
-
|