internal static Stream GetPayloadStream(Stream stream) { using (ArFile archive = new ArFile(stream, leaveOpen: true)) { while (archive.Read()) { if (archive.FileName.StartsWith("data.tar.")) { var ext = Path.GetExtension(archive.FileName); if (ext == ".gz") { return(new GZipDecompressor(archive.Open(), false)); } if (ext == ".xz") { // For some reason it complains about corrupted data when we try to read using smaller chunks var payload = new MemoryStream(); using (var xz = new XZInputStream(archive.Open())) { xz.CopyTo(payload); payload.Seek(0, SeekOrigin.Begin); return(payload); } } throw new InvalidDataException("Don't know how to decompress " + archive.FileName); } } throw new InvalidDataException("data.tar.?? not found"); } }
public void ReadTarFileTest() { using (Stream stream = File.OpenRead("Deb/libplist3_1.12-3.1_amd64.deb")) using (ArFile arFile = new ArFile(stream, leaveOpen: true)) { // Skip the debian version arFile.Read(); // This is the tar file arFile.Read(); Collection <string> filenames = new Collection <string>(); Collection <string> contents = new Collection <string>(); Collection <TarHeader> headers = new Collection <TarHeader>(); using (Stream entryStream = arFile.Open()) using (GZipDecompressor decompressedStream = new GZipDecompressor(entryStream, leaveOpen: true)) using (TarFile tarFile = new TarFile(decompressedStream, leaveOpen: true)) { while (tarFile.Read()) { filenames.Add(tarFile.FileName); headers.Add((TarHeader)tarFile.FileHeader); using (Stream data = tarFile.Open()) using (StreamReader reader = new StreamReader(data)) { contents.Add(reader.ReadToEnd()); } } } } }
/// <summary> /// Reads and parses the <c>debian-binary</c> file in the Debian archive. /// </summary> /// <param name="archive"> /// The archive to update with the data read from the <c>debian-binary</c> file. /// </param> /// <param name="package"> /// The package to update. /// </param> private static void ReadDebianBinary(ArFile archive, DebPackage package) { using (Stream stream = archive.Open()) using (StreamReader reader = new StreamReader(stream)) { var content = reader.ReadToEnd(); package.PackageFormatVersion = new Version(content); } }
public void WriteTest() { using (Stream original = File.OpenRead("Deb/libplist3_1.12-3.1_amd64.deb")) using (Stream expected = File.OpenRead("Deb/libplist3_1.12-3.1_amd64.deb")) using (Stream actual = new MemoryStream()) using (Stream output = new ValidatingCompositeStream(null, actual, expected)) { ArFileCreator.WriteMagic(output); var input = new ArFile(original, true); while (input.Read()) { var header = (ArHeader)input.FileHeader; var clone = this.CloneHeader(header); this.AssertCompareClonedHeader(header, clone); using (var data = input.Open()) { ArFileCreator.WriteEntry(output, clone, data); } } } }
private static async Task ReadControlArchive(ArFile archive, DebPackage package) { package.ControlExtras = new Dictionary <string, DebPackageControlFileData>(); package.Md5Sums = new Dictionary <string, string>(); using (Stream stream = archive.Open()) using (GZipDecompressor decompressedStream = new GZipDecompressor(stream, leaveOpen: true)) using (TarFile tarFile = new TarFile(decompressedStream, leaveOpen: true)) { while (tarFile.Read()) { switch (tarFile.FileName) { case "./control": case "control": using (Stream controlFile = tarFile.Open()) { package.ControlFile = ControlFileParser.Read(controlFile); } break; case "./md5sums": using (var sums = new StreamReader(tarFile.Open())) { string line; while ((line = await sums.ReadLineAsync()) != null) { var s = line.Split(new[] { " " }, 2, StringSplitOptions.None); package.Md5Sums[s[1]] = s[0]; } } break; case "./preinst": package.PreInstallScript = tarFile.ReadAsUtf8String(); break; case "./postinst": package.PostInstallScript = tarFile.ReadAsUtf8String(); break; case "./prerm": package.PreRemoveScript = tarFile.ReadAsUtf8String(); break; case "./postrm": package.PostRemoveScript = tarFile.ReadAsUtf8String(); break; case "./": tarFile.Skip(); break; default: package.ControlExtras[tarFile.FileName] = new DebPackageControlFileData { Mode = tarFile.FileHeader.FileMode, Contents = tarFile.ReadAsUtf8String() }; break; } } } }