protected virtual void WriteHeader(string name, DateTime lastModificationTime, long count, string userName, string groupName, int mode) { var tarHeader = new UsTarHeader() { FileName = name, LastModification = lastModificationTime, SizeInBytes = count, UserId = userName.GetHashCode(), UserName = userName, GroupId = groupName.GetHashCode(), GroupName = groupName, Mode = mode }; OutStream.Write(tarHeader.GetHeaderValue(), 0, tarHeader.HeaderSize); }
protected override void WriteHeader(string name, DateTime lastModificationTime, long count, int userId, int groupId, int mode, EntryType entryType) { var tarHeader = new UsTarHeader() { FileName = name, LastModification = lastModificationTime, SizeInBytes = count, UserId = userId, UserName = Convert.ToString(userId, 8), GroupId = groupId, GroupName = Convert.ToString(groupId, 8), Mode = mode, EntryType = entryType }; OutStream.Write(tarHeader.GetHeaderValue(), 0, tarHeader.HeaderSize); }
/// <summary> /// Read all files from an archive to a directory. It creates some child directories to /// reproduce a file structure from the archive. /// </summary> /// <param name="destDirectory">The out directory.</param> /// /// CAUTION! This method is not safe. It's not tar-bomb proof. /// {see http://en.wikipedia.org/wiki/Tar_(file_format) } /// If you are not sure about the source of an archive you extracting, /// then use MoveNext and Read and handle paths like ".." and "../.." according /// to your business logic. public void ReadToEnd(string destDirectory) { while (MoveNext(false)) { string fileNameFromArchive = FileInfo.FileName; string totalPath = destDirectory + Path.DirectorySeparatorChar + fileNameFromArchive; if (UsTarHeader.IsPathSeparator(fileNameFromArchive[fileNameFromArchive.Length - 1]) || FileInfo.EntryType == EntryType.Directory) { // Record is a directory Directory.CreateDirectory(totalPath); continue; } // If record is a file string fileName = Path.GetFileName(totalPath); string directory = totalPath.Remove(totalPath.Length - fileName.Length); Directory.CreateDirectory(directory); using (FileStream file = File.Create(totalPath)) { Read(file); } } }
/// <summary> /// Constructs TarReader object to read data from `tarredData` stream /// </summary> /// <param name="tarredData">A stream to read tar archive from</param> public TarReader(Stream tarredData) { inStream = tarredData; header = new UsTarHeader(); }