public void PutNextEntry(TarEntry entry) { if (entry == null) { throw new ArgumentNullException("entry"); } if (entry.TarHeader.Name.Length >= 100) { TarHeader header; new TarHeader { TypeFlag = 0x4c, Name = header.Name + "././@LongLink", UserId = 0, GroupId = 0, GroupName = "", UserName = "", LinkName = "", Size = entry.TarHeader.Name.Length }.WriteHeader(this.blockBuffer); this.buffer.WriteBlock(this.blockBuffer); int nameOffset = 0; while (nameOffset < entry.TarHeader.Name.Length) { Array.Clear(this.blockBuffer, 0, this.blockBuffer.Length); TarHeader.GetAsciiBytes(entry.TarHeader.Name, nameOffset, this.blockBuffer, 0, 0x200); nameOffset += 0x200; this.buffer.WriteBlock(this.blockBuffer); } } entry.WriteEntryHeader(this.blockBuffer); this.buffer.WriteBlock(this.blockBuffer); this.currBytes = 0L; this.currSize = entry.IsDirectory ? 0L : entry.Size; }
public TarEntry[] GetDirectoryEntries() { if (!((this.file != null) && Directory.Exists(this.file))) { return new TarEntry[0]; } string[] fileSystemEntries = Directory.GetFileSystemEntries(this.file); TarEntry[] entryArray = new TarEntry[fileSystemEntries.Length]; for (int i = 0; i < fileSystemEntries.Length; i++) { entryArray[i] = CreateEntryFromFile(fileSystemEntries[i]); } return entryArray; }
public TarEntry GetNextEntry() { if (this.hasHitEOF) { return null; } if (this.currentEntry != null) { this.SkipToNextEntry(); } byte[] block = this.tarBuffer.ReadBlock(); if (block == null) { this.hasHitEOF = true; } else if (TarBuffer.IsEndOfArchiveBlock(block)) { this.hasHitEOF = true; } if (this.hasHitEOF) { this.currentEntry = null; } else { try { TarHeader header = new TarHeader(); header.ParseBuffer(block); if (!header.IsChecksumValid) { throw new TarException("Header checksum is invalid"); } this.entryOffset = 0L; this.entrySize = header.Size; StringBuilder builder = null; if (header.TypeFlag == 0x4c) { byte[] buffer = new byte[0x200]; long entrySize = this.entrySize; builder = new StringBuilder(); while (entrySize > 0L) { int length = this.Read(buffer, 0, (entrySize > buffer.Length) ? buffer.Length : ((int) entrySize)); if (length == -1) { throw new InvalidHeaderException("Failed to read long name entry"); } builder.Append(TarHeader.ParseName(buffer, 0, length).ToString()); entrySize -= length; } this.SkipToNextEntry(); block = this.tarBuffer.ReadBlock(); } else if (header.TypeFlag == 0x67) { this.SkipToNextEntry(); block = this.tarBuffer.ReadBlock(); } else if (header.TypeFlag == 120) { this.SkipToNextEntry(); block = this.tarBuffer.ReadBlock(); } else if (header.TypeFlag == 0x56) { this.SkipToNextEntry(); block = this.tarBuffer.ReadBlock(); } else if (((header.TypeFlag != 0x30) && (header.TypeFlag != 0)) && (header.TypeFlag != 0x35)) { this.SkipToNextEntry(); block = this.tarBuffer.ReadBlock(); } if (this.entryFactory == null) { this.currentEntry = new TarEntry(block); if (builder != null) { this.currentEntry.Name = builder.ToString(); } } else { this.currentEntry = this.entryFactory.CreateEntry(block); } this.entryOffset = 0L; this.entrySize = this.currentEntry.Size; } catch (InvalidHeaderException exception) { this.entrySize = 0L; this.entryOffset = 0L; this.currentEntry = null; throw new InvalidHeaderException(string.Format("Bad header in record {0} block {1} {2}", this.tarBuffer.CurrentRecord, this.tarBuffer.CurrentBlock, exception.Message)); } } return this.currentEntry; }
private void WriteEntryCore(TarEntry sourceEntry, bool recurse) { string path = null; bool flag; string file = sourceEntry.File; TarEntry entry = (TarEntry) sourceEntry.Clone(); if (this.applyUserInfoOverrides) { entry.GroupId = this.groupId; entry.GroupName = this.groupName; entry.UserId = this.userId; entry.UserName = this.userName; } this.OnProgressMessageEvent(entry, null); if ((this.asciiTranslate && !entry.IsDirectory) && !IsBinary(file)) { path = Path.GetTempFileName(); using (StreamReader reader = File.OpenText(file)) { using (Stream stream = File.Create(path)) { string str3; goto Label_00EA; Label_00A8: str3 = reader.ReadLine(); if (str3 == null) { goto Label_00EF; } byte[] bytes = Encoding.ASCII.GetBytes(str3); stream.Write(bytes, 0, bytes.Length); stream.WriteByte(10); Label_00EA: flag = true; goto Label_00A8; Label_00EF: stream.Flush(); } } entry.Size = new FileInfo(path).Length; file = path; } string str4 = null; if ((this.rootPath != null) && entry.Name.StartsWith(this.rootPath)) { str4 = entry.Name.Substring(this.rootPath.Length + 1); } if (this.pathPrefix != null) { str4 = (str4 == null) ? (this.pathPrefix + "/" + entry.Name) : (this.pathPrefix + "/" + str4); } if (str4 != null) { entry.Name = str4; } this.tarOut.PutNextEntry(entry); if (entry.IsDirectory) { if (recurse) { TarEntry[] directoryEntries = entry.GetDirectoryEntries(); for (int i = 0; i < directoryEntries.Length; i++) { this.WriteEntryCore(directoryEntries[i], recurse); } } return; } using (Stream stream2 = File.OpenRead(file)) { int num2; byte[] buffer = new byte[0x8000]; goto Label_0284; Label_0253: num2 = stream2.Read(buffer, 0, buffer.Length); if (num2 <= 0) { goto Label_02A0; } this.tarOut.Write(buffer, 0, num2); Label_0284: flag = true; goto Label_0253; } Label_02A0: if ((path != null) && (path.Length > 0)) { File.Delete(path); } this.tarOut.CloseEntry(); }
public void WriteEntry(TarEntry sourceEntry, bool recurse) { if (sourceEntry == null) { throw new ArgumentNullException("sourceEntry"); } if (this.isDisposed) { throw new ObjectDisposedException("TarArchive"); } try { if (recurse) { TarHeader.SetValueDefaults(sourceEntry.UserId, sourceEntry.UserName, sourceEntry.GroupId, sourceEntry.GroupName); } this.WriteEntryCore(sourceEntry, recurse); } finally { if (recurse) { TarHeader.RestoreSetValues(); } } }
protected virtual void OnProgressMessageEvent(TarEntry entry, string message) { ProgressMessageHandler progressMessageEvent = this.ProgressMessageEvent; if (progressMessageEvent != null) { progressMessageEvent(this, entry, message); } }
private void ExtractEntry(string destDir, TarEntry entry) { this.OnProgressMessageEvent(entry, null); string name = entry.Name; if (Path.IsPathRooted(name)) { name = name.Substring(Path.GetPathRoot(name).Length); } name = name.Replace('/', Path.DirectorySeparatorChar); string directoryName = Path.Combine(destDir, name); if (entry.IsDirectory) { EnsureDirectoryExists(directoryName); } else { EnsureDirectoryExists(Path.GetDirectoryName(directoryName)); bool flag = true; FileInfo info = new FileInfo(directoryName); if (info.Exists) { if (this.keepOldFiles) { this.OnProgressMessageEvent(entry, "Destination file already exists"); flag = false; } else if ((info.Attributes & FileAttributes.ReadOnly) != 0) { this.OnProgressMessageEvent(entry, "Destination file already exists, and is read-only"); flag = false; } } if (flag) { bool flag2 = false; Stream stream = File.Create(directoryName); if (this.asciiTranslate) { flag2 = !IsBinary(directoryName); } StreamWriter writer = null; if (flag2) { writer = new StreamWriter(stream); } byte[] buffer = new byte[0x8000]; while (true) { int count = this.tarIn.Read(buffer, 0, buffer.Length); if (count <= 0) { if (flag2) { writer.Close(); } else { stream.Close(); } return; } if (flag2) { int index = 0; for (int i = 0; i < count; i++) { if (buffer[i] == 10) { string str4 = Encoding.ASCII.GetString(buffer, index, i - index); writer.WriteLine(str4); index = i + 1; } } } else { stream.Write(buffer, 0, count); } } } } }
public static TarEntry CreateTarEntry(string name) { TarEntry entry = new TarEntry(); NameTarHeader(entry.header, name); return entry; }
public static TarEntry CreateEntryFromFile(string fileName) { TarEntry entry = new TarEntry(); entry.GetFileTarHeader(entry.header, fileName); return entry; }
public bool IsDescendent(TarEntry toTest) { if (toTest == null) { throw new ArgumentNullException("toTest"); } return toTest.Name.StartsWith(this.Name); }