/// <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 = Path.Join(destDirectory, fileNameFromArchive); if (UsTarHeader.IsPathSeparator(fileNameFromArchive[fileNameFromArchive.Length - 1]) || FileInfo.EntryType == EntryType.Directory) { // Record is a directory Directory.CreateDirectory(totalPath); logger.LogDebug("Created {directory}", totalPath); continue; } // If record is a file string fileName = Path.GetFileName(totalPath); string directory = Path.GetDirectoryName(totalPath); Directory.CreateDirectory(directory); using (FileStream file = File.Create(totalPath)) { Read(file); } logger.LogDebug("Extracted {fileName} to {directory}", fileName, directory); } }
/// <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, ILoggerFactory loggerFactory) { logger = loggerFactory.CreateLogger <TarReader>(); inStream = tarredData; header = new UsTarHeader(); }