/// <exception cref="System.IO.IOException"/> /// <exception cref="Sharpen.URISyntaxException"/> internal static LocalResource CreateTgzFile(FileContext files, Path p, int len, Random r, LocalResourceVisibility vis) { byte[] bytes = new byte[len]; r.NextBytes(bytes); FilePath gzipFile = new FilePath(p.ToUri().GetPath() + ".tar.gz"); gzipFile.CreateNewFile(); TarArchiveOutputStream @out = new TarArchiveOutputStream(new GZIPOutputStream(new FileOutputStream(gzipFile))); TarArchiveEntry entry = new TarArchiveEntry(p.GetName()); entry.SetSize(bytes.Length); @out.PutArchiveEntry(entry); @out.Write(bytes); @out.CloseArchiveEntry(); @out.Close(); LocalResource ret = recordFactory.NewRecordInstance <LocalResource>(); ret.SetResource(ConverterUtils.GetYarnUrlFromPath(new Path(p.ToString() + ".tar.gz" ))); ret.SetSize(len); ret.SetType(LocalResourceType.Archive); ret.SetVisibility(vis); ret.SetTimestamp(files.GetFileStatus(new Path(p.ToString() + ".tar.gz")).GetModificationTime ()); return(ret); }
public TarEntryViewModel(TarFileInfo parent, TarArchiveEntry e) { this.parent = parent; Name = e.Key; getStream = new Lazy <Stream>(delegate { using (var srcStream = e.OpenEntryStream()) { if (Name.EndsWith(".img.lz4")) { return(Lz4FileInfo.DecodeLz4(srcStream)); } return(TempFileStream.CreateFrom(srcStream, done => FileInfoBase.ReportProgress(done, e.Size))); } }); }
/// <inheritdoc /> public override ICompressedArchiveEntry CreateEntry(string name) { if (IsReadOnly) { throw new InvalidOperationException(Resources.Strings.TarArchiveAccess_InvalidModeForCreateEntryError); } var root = TarArchive.RootPath; var isRooted = Path.IsPathRooted(name); var tarEntry = isRooted ? TarEntry.CreateEntryFromFile(name) : TarEntry.CreateTarEntry(name); if (isRooted) { if (!string.IsNullOrEmpty(RootLocation)) { var relativeName = PathUtils.GetRelativePath(tarEntry.File, Path.GetDirectoryName(RootLocation)); tarEntry.Name = relativeName; } } var newEntry = new TarArchiveEntry(tarEntry); _entries.Add(newEntry); return(newEntry); }
/// <summary> /// Initializes a new instance of <see cref="OutputStream"/>. /// </summary> /// <param name="tarAccess">The TAR archive to which the contents of <paramref name="tarArchiveEntry"/> is to be written.</param> /// <param name="tarArchiveEntry">A previously created <see cref="TarEntry"/> that will be written to.</param> public OutputStream(TarAccessSharpZipLib tarAccess, TarArchiveEntry tarArchiveEntry) { _tarAccess = tarAccess; _tarArchiveEntry = tarArchiveEntry; }
/// <summary> /// Write a set of input files to a tape archive (assuming the same output archive name) /// </summary> /// <param name="inputFiles">Input files to be moved</param> /// <param name="outDir">Output directory to build to</param> /// <param name="rom">DatItem representing the new information</param> /// <returns>True if the archive was written properly, false otherwise</returns> public override bool Write(List <string> inputFiles, string outDir, List <Rom> roms) { bool success = false; string tempFile = Path.Combine(outDir, $"tmp{Guid.NewGuid()}"); // If either list of roms is null or empty, return if (inputFiles == null || roms == null || inputFiles.Count == 0 || roms.Count == 0) { return(success); } // If the number of inputs is less than the number of available roms, return if (inputFiles.Count < roms.Count) { return(success); } // If one of the files doesn't exist, return foreach (string file in inputFiles) { if (!File.Exists(file)) { return(success); } } // Get the output archive name from the first rebuild rom string archiveFileName = Path.Combine(outDir, Sanitizer.RemovePathUnsafeCharacters(roms[0].Machine.Name) + (roms[0].Machine.Name.EndsWith(".tar") ? string.Empty : ".tar")); // Set internal variables TarArchive oldTarFile = TarArchive.Create(); TarArchive tarFile = TarArchive.Create(); try { // If the full output path doesn't exist, create it if (!Directory.Exists(Path.GetDirectoryName(archiveFileName))) { Directory.CreateDirectory(Path.GetDirectoryName(archiveFileName)); } // If the archive doesn't exist, create it and put the single file if (!File.Exists(archiveFileName)) { // Map all inputs to index Dictionary <string, int> inputIndexMap = new Dictionary <string, int>(); for (int i = 0; i < inputFiles.Count; i++) { inputIndexMap.Add(roms[i].Name.Replace('\\', '/'), i); } // Sort the keys in TZIP order List <string> keys = inputIndexMap.Keys.ToList(); keys.Sort(ZipFile.TrrntZipStringCompare); // Now add all of the files in order foreach (string key in keys) { // Get the index mapped to the key int index = inputIndexMap[key]; // Get temporary date-time if possible DateTime?usableDate = null; if (UseDates && !string.IsNullOrWhiteSpace(roms[index].Date) && DateTime.TryParse(roms[index].Date.Replace('\\', '/'), out DateTime dt)) { usableDate = dt; } // Copy the input stream to the output tarFile.AddEntry(roms[index].Name, FileExtensions.TryOpenRead(inputFiles[index]), size: roms[index].Size ?? 0, modified: usableDate); } } // Otherwise, sort the input files and write out in the correct order else { // Open the old archive for reading oldTarFile = TarArchive.Open(archiveFileName); // Get a list of all current entries List <string> entries = oldTarFile.Entries.Select(i => i.Key).ToList(); // Map all inputs to index Dictionary <string, int> inputIndexMap = new Dictionary <string, int>(); for (int i = 0; i < inputFiles.Count; i++) { // If the old one contains the new file, then just skip out if (entries.Contains(roms[i].Name.Replace('\\', '/'))) { continue; } inputIndexMap.Add(roms[i].Name.Replace('\\', '/'), -(i + 1)); } // Then add all of the old entries to it too for (int i = 0; i < entries.Count; i++) { inputIndexMap.Add(entries[i], i); } // If the number of entries is the same as the old archive, skip out if (inputIndexMap.Keys.Count <= entries.Count) { success = true; return(success); } // Get the order for the entries with the new file List <string> keys = inputIndexMap.Keys.ToList(); keys.Sort(ZipFile.TrrntZipStringCompare); // Copy over all files to the new archive foreach (string key in keys) { // Get the index mapped to the key int index = inputIndexMap[key]; // If we have the input file, add it now if (index < 0) { // Get temporary date-time if possible DateTime?usableDate = null; if (UseDates && !string.IsNullOrWhiteSpace(roms[-index - 1].Date) && DateTime.TryParse(roms[-index - 1].Date.Replace('\\', '/'), out DateTime dt)) { usableDate = dt; } // Copy the input file to the output tarFile.AddEntry(roms[-index - 1].Name, FileExtensions.TryOpenRead(inputFiles[-index - 1]), size: roms[-index - 1].Size ?? 0, modified: usableDate); } // Otherwise, copy the file from the old archive else { // Get the stream from the original archive TarArchiveEntry tae = oldTarFile.Entries.ElementAt(index); MemoryStream entry = new MemoryStream(); tae.OpenEntryStream().CopyTo(entry); // Copy the input stream to the output tarFile.AddEntry(key, entry, size: tae.Size, modified: tae.LastModifiedTime); } } } // Close the output tar file tarFile.SaveTo(tempFile, new WriterOptions(CompressionType.None)); success = true; } catch (Exception ex) { logger.Error(ex); success = false; } finally { tarFile.Dispose(); oldTarFile.Dispose(); } // If the old file exists, delete it and replace if (File.Exists(archiveFileName)) { FileExtensions.TryDelete(archiveFileName); } File.Move(tempFile, archiveFileName); return(true); }
/// <summary> /// Write an input stream to a tape archive /// </summary> /// <param name="inputStream">Input stream to be moved</param> /// <param name="outDir">Output directory to build to</param> /// <param name="rom">DatItem representing the new information</param> /// <param name="date">True if the date from the DAT should be used if available, false otherwise (default)</param> /// <param name="romba">True if files should be output in Romba depot folders, false otherwise</param> /// <returns>True if the archive was written properly, false otherwise</returns> public override bool Write(Stream inputStream, string outDir, Rom rom, bool date = false, bool romba = false) { bool success = false; string tempFile = Path.Combine(outDir, "tmp" + Guid.NewGuid().ToString()); // If either input is null or empty, return if (inputStream == null || rom == null || rom.Name == null) { return(success); } // If the stream is not readable, return if (!inputStream.CanRead) { return(success); } // Get the output archive name from the first rebuild rom string archiveFileName = Path.Combine(outDir, Utilities.RemovePathUnsafeCharacters(rom.MachineName) + (rom.MachineName.EndsWith(".tar") ? "" : ".tar")); // Set internal variables TarArchive oldTarFile = TarArchive.Create(); TarArchive tarFile = TarArchive.Create(); try { // If the full output path doesn't exist, create it if (!Directory.Exists(Path.GetDirectoryName(archiveFileName))) { Directory.CreateDirectory(Path.GetDirectoryName(archiveFileName)); } // If the archive doesn't exist, create it and put the single file if (!File.Exists(archiveFileName)) { // Get temporary date-time if possible DateTime?usableDate = null; if (date && !String.IsNullOrWhiteSpace(rom.Date) && DateTime.TryParse(rom.Date.Replace('\\', '/'), out DateTime dt)) { usableDate = dt; } // Copy the input stream to the output inputStream.Seek(0, SeekOrigin.Begin); tarFile.AddEntry(rom.Name, inputStream, size: rom.Size, modified: usableDate); } // Otherwise, sort the input files and write out in the correct order else { // Open the old archive for reading oldTarFile = TarArchive.Open(archiveFileName); // Get a list of all current entries List <string> entries = oldTarFile.Entries.Select(i => i.Key).ToList(); // Map all inputs to index Dictionary <string, int> inputIndexMap = new Dictionary <string, int>(); // If the old one doesn't contain the new file, then add it if (!entries.Contains(rom.Name.Replace('\\', '/'))) { inputIndexMap.Add(rom.Name.Replace('\\', '/'), -1); } // Then add all of the old entries to it too for (int i = 0; i < entries.Count; i++) { inputIndexMap.Add(entries[i], i); } // If the number of entries is the same as the old archive, skip out if (inputIndexMap.Keys.Count <= entries.Count) { success = true; return(success); } // Get the order for the entries with the new file List <string> keys = inputIndexMap.Keys.ToList(); keys.Sort(ZipFile.TorrentZipStringCompare); // Copy over all files to the new archive foreach (string key in keys) { // Get the index mapped to the key int index = inputIndexMap[key]; // Get temporary date-time if possible DateTime?usableDate = null; if (date && !String.IsNullOrWhiteSpace(rom.Date) && DateTime.TryParse(rom.Date.Replace('\\', '/'), out DateTime dt)) { usableDate = dt; } // If we have the input file, add it now if (index < 0) { // Copy the input file to the output inputStream.Seek(0, SeekOrigin.Begin); tarFile.AddEntry(rom.Name, inputStream, size: rom.Size, modified: usableDate); } // Otherwise, copy the file from the old archive else { // Get the stream from the original archive TarArchiveEntry tae = oldTarFile.Entries.ElementAt(index); MemoryStream entry = new MemoryStream(); tae.OpenEntryStream().CopyTo(entry); // Copy the input stream to the output tarFile.AddEntry(key, entry, size: tae.Size, modified: tae.LastModifiedTime); } } } // Close the output tar file tarFile.SaveTo(tempFile, new WriterOptions(CompressionType.None)); success = true; } catch (Exception ex) { Console.WriteLine(ex); success = false; } finally { inputStream.Dispose(); tarFile.Dispose(); oldTarFile.Dispose(); } // If the old file exists, delete it and replace if (File.Exists(archiveFileName)) { Utilities.TryDeleteFile(archiveFileName); } File.Move(tempFile, archiveFileName); return(success); }
public TarEntryBlob(string key, TarArchiveEntry entry) { Key = key ?? throw new ArgumentNullException(nameof(key)); this.entry = entry ?? throw new ArgumentNullException(nameof(entry)); }
public CompressedFileInfo(TarArchiveEntry entry) : this(entry?.FullName ?? throw new ArgumentNullException(nameof(entry)), true, entry.Length) { }