public void ReadToEnd(string destDirectory) { while (this.MoveNext(false)) { string fileName1 = this.FileInfo.FileName; string path = destDirectory + (object)Path.DirectorySeparatorChar + fileName1; if (UsTarHeader.IsPathSeparator(fileName1[fileName1.Length - 1]) || this.FileInfo.EntryType == EntryType.Directory) { Directory.CreateDirectory(path); } else { string fileName2 = Path.GetFileName(path); Directory.CreateDirectory(path.Remove(path.Length - fileName2.Length)); using (FileStream fileStream = File.Create(path)) this.Read((Stream)fileStream); } } }
/// <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 async Task ReadToEndAsync(string destDirectory) { while (await MoveNextAsync(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)) { await ReadAsync(file); } } }
public void ReadToEnd(string destDirectory) { while (MoveNext(false)) { var fileName1 = FileInfo.FileName; var path = destDirectory + Path.DirectorySeparatorChar + fileName1; if (UsTarHeader.IsPathSeparator(fileName1[fileName1.Length - 1]) || (FileInfo.EntryType == EntryType.Directory)) { Directory.CreateDirectory(path); } else { var fileName2 = Path.GetFileName(path); Directory.CreateDirectory(path.Remove(path.Length - fileName2.Length)); using (var fileStream = File.Create(path)) { Read(fileStream); } } } }