/// <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(); }