using System; using System.IO; using AMWD.Common.Packing.Tar.Interfaces; using AMWD.Common.Packing.Tar.Utils; namespace AMWD.Common.Packing.Tar { /// /// Writes a tar (see GNU tar) archive to a stream. /// /// /// Copied from: DotnetMakeDeb /// public class TarWriter : LegacyTarWriter { /// /// Initilizes a new instance of the class. /// /// The stream to write the archive to. public TarWriter(Stream outStream) : base(outStream) { } /// /// Writes an entry header (file, dir, ...) to the archive. /// /// The name. /// The last modification time. /// The number of bytes. /// The user id. /// The group id. /// The access mode. /// The entry type. protected override void WriteHeader(string name, DateTime lastModificationTime, long count, int userId, int groupId, int mode, EntryType entryType) { var tarHeader = new UsTarHeader() { FileName = name, Mode = mode, UserId = userId, GroupId = groupId, SizeInBytes = count, LastModification = lastModificationTime, EntryType = entryType, UserName = Convert.ToString(userId, 8), GroupName = Convert.ToString(groupId, 8) }; OutStream.Write(tarHeader.GetHeaderValue(), 0, tarHeader.HeaderSize); } /// /// Writes an entry header (file, dir, ...) to the archive. /// Hashes the username and groupname down to a HashCode. /// /// The name. /// The last modification time. /// The number of bytes. /// The username. /// The group name. /// The access mode. /// The entry type. protected virtual void WriteHeader(string name, DateTime lastModificationTime, long count, string userName, string groupName, int mode, EntryType entryType) { WriteHeader( name: name, lastModificationTime: lastModificationTime, count: count, userId: userName.GetHashCode(), groupId: groupName.GetHashCode(), mode: mode, entryType: entryType); } /// /// Writes a file to the archive. /// /// The file name. /// The filesize in bytes. /// The username. /// The group name. /// The access mode. /// The last modification time. /// The write handle. public virtual void Write(string name, long dataSizeInBytes, string userName, string groupName, int mode, DateTime lastModificationTime, WriteDataDelegate writeDelegate) { var writer = new DataWriter(OutStream, dataSizeInBytes); WriteHeader(name, lastModificationTime, dataSizeInBytes, userName, groupName, mode, EntryType.File); while (writer.CanWrite) { writeDelegate(writer); } AlignTo512(dataSizeInBytes, false); } /// /// Writes a file to the archive. /// /// The file stream to add to the archive. /// The filesize in bytes. /// The file name. /// The user id. /// The group id. /// The access mode. /// The last modification time. public void Write(Stream data, long dataSizeInBytes, string fileName, string userId, string groupId, int mode, DateTime lastModificationTime) { WriteHeader(fileName, lastModificationTime, dataSizeInBytes, userId, groupId, mode, EntryType.File); WriteContent(dataSizeInBytes, data); AlignTo512(dataSizeInBytes, false); } } }