/** * Adds a blob to the archive. Note that this should be used with non-file {@link Blob}s; for * adding files to the archive, use {@link #addTarArchiveEntry}. * * @param blob the {@link Blob} to add to the tarball * @param size the size (in bytes) of {@code blob} * @param name the name of the entry (i.e. filename) */ public void AddBlobEntry(IBlob blob, long size, string name) { TarEntry entry = TarEntry.CreateTarEntry(name); entry.TarHeader.Size = size; archiveMap[entry] = blob; }
private void CreateTarManually(TarOutputStream tarOutputStream, string sourceFile) { // You might replace these 3 lines with your own stream code using (Stream inputStream = File.OpenRead(sourceFile)) { string tarName = Path.GetFileName(sourceFile);//.Substring(3); // strip off "C:\" long fileSize = inputStream.Length; // Create a tar entry named as appropriate. You can set the name to anything, // but avoid names starting with drive or UNC. TarEntry entry = TarEntry.CreateTarEntry(tarName); // Must set size, otherwise TarOutputStream will fail when output exceeds. entry.Size = fileSize; // Add the entry to the tar stream, before writing the data. tarOutputStream.PutNextEntry(entry); // this is copied from TarArchive.WriteEntryCore byte[] localBuffer = new byte[32 * 1024]; while (true) { int numRead = inputStream.Read(localBuffer, 0, localBuffer.Length); if (numRead <= 0) { break; } tarOutputStream.Write(localBuffer, 0, numRead); } } tarOutputStream.CloseEntry(); }
// Source: https://github.com/icsharpcode/SharpZipLib/wiki/GZip-and-Tar-Samples#-create-a-tar-or-tgz-with-control-over-filenames-and-data-source private static void CreateTarRecursive(TarOutputStream tarOutputStream, byte[] fileCopyBuffer, DirectoryInfo directory, string relativePath, List <Regex> ignoredPathsRegex) { FileInfo[] files = directory.GetFiles(); for (int i = 0; i < files.Length; i++) { string fileRelativePath = relativePath + files[i].Name; if (!ignoredPathsRegex.PathMatchesPattern(fileRelativePath)) { using (Stream inputStream = File.OpenRead(files[i].FullName)) { TarEntry tarEntry = TarEntry.CreateTarEntry(fileRelativePath.Replace('\\', '/')); tarEntry.Size = inputStream.Length; tarOutputStream.PutNextEntry(tarEntry); int numRead; while ((numRead = inputStream.Read(fileCopyBuffer, 0, fileCopyBuffer.Length)) > 0) { tarOutputStream.Write(fileCopyBuffer, 0, numRead); } } tarOutputStream.CloseEntry(); } } DirectoryInfo[] subDirectories = directory.GetDirectories(); for (int i = 0; i < subDirectories.Length; i++) { string directoryRelativePath = relativePath + subDirectories[i].Name + Path.DirectorySeparatorChar; if (!ignoredPathsRegex.PathMatchesPattern(directoryRelativePath)) { CreateTarRecursive(tarOutputStream, fileCopyBuffer, subDirectories[i], directoryRelativePath, ignoredPathsRegex); } } }
private void WriteBufferToTar(TarOutputStream tarOut, string filePath, byte[] buffer) { if (tarOut == null) { throw new ArgumentNullException(nameof(tarOut)); } if (filePath == null) { throw new ArgumentNullException(nameof(filePath)); } if (buffer == null) { throw new ArgumentNullException(nameof(buffer)); } filePath = filePath.Replace(@"\", "/"); filePath = filePath.TrimStart('/'); var tarEntry = TarEntry.CreateTarEntry("package/" + filePath); tarEntry.Size = buffer.Length; tarOut.PutNextEntry(tarEntry); tarOut.Write(buffer, 0, buffer.Length); tarOut.CloseEntry(); }
private async Task <Result> AddEntry([NotNull] TarOutputStream archive, [NotNull] string filePathInArchive, [NotNull] Stream contentStream) { if (archive == null) { throw new ArgumentNullException(nameof(archive)); } if (filePathInArchive == null) { throw new ArgumentNullException(nameof(filePathInArchive)); } if (contentStream == null) { throw new ArgumentNullException(nameof(contentStream)); } var entry = TarEntry.CreateTarEntry(filePathInArchive); entry.Size = contentStream.Length; entry.TarHeader.Mode = Chmod; //chmod 755 archive.PutNextEntry(entry); var result = await _streamService.Copy(contentStream, archive, $"Adding {filePathInArchive}"); archive.CloseEntry(); return(result); }
public void StreamWithJapaneseName(int length, string encodingName) { // U+3042 is Japanese Hiragana // https://unicode.org/charts/PDF/U3040.pdf var entryName = new string((char)0x3042, length); var data = new byte[32]; var encoding = Encoding.GetEncoding(encodingName); using (var memoryStream = new MemoryStream()) { using (var tarOutput = new TarOutputStream(memoryStream, encoding)) { var entry = TarEntry.CreateTarEntry(entryName); entry.Size = 32; tarOutput.PutNextEntry(entry); tarOutput.Write(data, 0, data.Length); } using (var memInput = new MemoryStream(memoryStream.ToArray())) using (var inputStream = new TarInputStream(memInput, encoding)) { var buf = new byte[64]; var entry = inputStream.GetNextEntry(); Assert.AreEqual(entryName, entry.Name); var bytesread = inputStream.Read(buf, 0, buf.Length); Assert.AreEqual(data.Length, bytesread); } File.WriteAllBytes(Path.Combine(Path.GetTempPath(), $"jpnametest_{length}_{encodingName}.tar"), memoryStream.ToArray()); } }
private void AddToArchive(string rootName, NPath file, TarOutputStream outStream, ref byte[] buffer) { rootName += file.FileName; var entry = TarEntry.CreateTarEntry(rootName); if (file.FileExists()) { using (var data = File.OpenRead(file)) { entry.Size = data.Length; outStream.PutNextEntry(entry); while (true) { var read = data.Read(buffer, 0, buffer.Length); if (read <= 0) { break; } outStream.Write(buffer, 0, read); } outStream.CloseEntry(); } } }
internal static void TarFile(string filename, TarOutputStream stream, int offset) { try { using (var sReader = File.Open(filename, FileMode.Open, FileAccess.Read)) { var tName = filename.Substring(offset); var fSize = sReader.Length; var tEntry = TarEntry.CreateTarEntry(tName); tEntry.Size = fSize; stream.PutNextEntry(tEntry); var localBuffer = new byte[32 * 1024]; while (true) { var numRead = sReader.Read(localBuffer, 0, localBuffer.Length); if (numRead <= 0) { break; } stream.Write(localBuffer, 0, numRead); } stream.CloseEntry(); } } catch { Console.WriteLine($"Access denied on {filename}"); } }
static void _AddFileToTarRaw(TarOutputStream tarOutputStream, FileStream fin, FileInfo info, string name) { long fileSize = fin.Length; // Create a tar entry named as appropriate. You can set the name to anything, // but avoid names starting with drive or UNC. TarEntry entry = TarEntry.CreateTarEntry(name); if (info.IsReadOnly) { entry.TarHeader.Mode &= Convert.ToInt32("0444", 8); Console.WriteLine($"chmod [{name}] -> 0{Convert.ToString(entry.TarHeader.Mode, 8)}"); } // Must set size, otherwise TarOutputStream will fail when output exceeds. entry.Size = fileSize; // Add the entry to the tar stream, before writing the data. tarOutputStream.PutNextEntry(entry); // this is copied from TarArchive.WriteEntryCore byte[] localBuffer = new byte[32 * 1024]; while (true) { int numRead = fin.Read(localBuffer, 0, localBuffer.Length); if (numRead <= 0) { break; } tarOutputStream.Write(localBuffer, 0, numRead); } tarOutputStream.CloseEntry(); }
protected override void OpenEntry(TarOutputStream outputStream, string path) { Command.WriteVerbose("Open Tar Entry: " + path); TarEntry entry = TarEntry.CreateTarEntry(path); if (EndsWithDirectorySeparator(path)) { Command.WriteVerbose("Directory Entry."); entry.TarHeader.Mode = 511; // 0777 entry.TarHeader.TypeFlag = TarHeader.LF_DIR; entry.TarHeader.Size = 0; } else { Command.WriteVerbose("File Entry."); entry.TarHeader.Mode = 438; // 0666 entry.TarHeader.TypeFlag = TarHeader.LF_NORMAL; entry.TarHeader.Size = _currentInputFileInfo.Length; entry.TarHeader.ModTime = _currentInputFileInfo.LastWriteTime; } outputStream.PutNextEntry(entry); }
public static void WriteFile(this TarOutputStream stream, string source, string dest) { using (Stream inputStream = File.OpenRead(source)) { long fileSize = inputStream.Length; // Create a tar entry named as appropriate. You can set the name to anything, // but avoid names starting with drive or UNC. TarEntry entry = TarEntry.CreateTarEntry(dest); // Must set size, otherwise TarOutputStream will fail when output exceeds. entry.Size = fileSize; // Add the entry to the tar stream, before writing the data. stream.PutNextEntry(entry); // this is copied from TarArchive.WriteEntryCore byte[] localBuffer = new byte[32 * 1024]; while (true) { int numRead = inputStream.Read(localBuffer, 0, localBuffer.Length); if (numRead <= 0) { break; } stream.Write(localBuffer, 0, numRead); } //Close the entry stream.CloseEntry(); } }
static void EnsureDirectories(string name, ICollection <string> dirs, TarOutputStream tar) { var root = Path.GetDirectoryName(name); var parts = root.Split(Path.DirectorySeparatorChar); var path = new StringBuilder(); for (var i = 0; i < parts.Length; i++) { var part = parts[i]; if (string.IsNullOrWhiteSpace(part)) { continue; } if (i != 0) { path.Append(Path.DirectorySeparatorChar); } path.Append(part); var current = path.ToString() + Path.DirectorySeparatorChar; if (dirs.Contains(current)) { continue; } dirs.Add(current); var entry = TarEntry.CreateTarEntry(FixSlash(current)); var header = entry.TarHeader; TarEntry.NameTarHeader(header, FixSlash(current)); header.GroupName = "root"; header.UserName = "******"; header.Mode = GetPermissions(entry.Name, false); tar.PutNextEntry(entry); tar.CloseEntry(); } }
/** * Adds a {@link TarArchiveEntry} if its extraction path does not exist yet. Also adds all of * the parent directories on the extraction path, if the parent does not exist. Parent will have * modified time to set to {@link LayerConfiguration#DEFAULT_MODIFIED_TIME}. * * @param tarArchiveEntry the {@link TarArchiveEntry} */ public void Add(TarEntry tarArchiveEntry) { if (names.Contains(tarArchiveEntry.Name)) { return; } // Adds all directories along extraction paths to explicitly set permissions for those // directories. SystemPath namePath = Paths.Get(tarArchiveEntry.Name); if (namePath.GetParent() != namePath.GetRoot()) { TarEntry dir = TarEntry.CreateTarEntry(namePath.GetParent().ToString().Replace(Path.DirectorySeparatorChar, '/')); dir.Name += "/"; dir.ModTime = DateTimeOffset.FromUnixTimeMilliseconds(LayerConfiguration.DefaultModifiedTime.ToUnixTimeMilliseconds()).DateTime; dir.TarHeader.Mode &= ~(int)PosixFilePermissions.All; dir.TarHeader.Mode |= (int)( PosixFilePermissions.OwnerAll | PosixFilePermissions.GroupReadExecute | PosixFilePermissions.OthersReadExecute); dir.TarHeader.TypeFlag = TarHeader.LF_DIR; Add(dir); } entries.Add(tarArchiveEntry); names.Add(tarArchiveEntry.Name); }
/// <summary> /// 打包单个文件 /// </summary> /// <param name="SourFile"></param> /// <param name="TarFile"></param> public void SerFileTar(string SourFile, string TarFile) { FileStream fs = System.IO.File.Create(TarFile); TarOutputStream taroutputstream = new TarOutputStream(fs); try { FileStream ins = ins = System.IO.File.OpenRead(SourFile); byte[] buffer = new byte[ins.Length]; ins.Read(buffer, 0, buffer.Length); ins.Close(); string tempfile = SourFile.Substring(3, SourFile.Length - 3); TarEntry tarEntry = TarEntry.CreateTarEntry(tempfile); tarEntry.Size = buffer.Length; taroutputstream.PutNextEntry(tarEntry); taroutputstream.Write(buffer, 0, buffer.Length); taroutputstream.CloseEntry(); } catch (Exception ex) { throw ex; } finally { taroutputstream.Close(); } }
/** * Adds a blob to the archive. Note that this should be used with raw bytes and not file contents; * for adding files to the archive, use {@link #addTarArchiveEntry}. * * @param contents the bytes to add to the tarball * @param name the name of the entry (i.e. filename) */ public void AddByteEntry(byte[] contents, string name) { contents = contents ?? throw new ArgumentNullException(nameof(contents)); TarEntry entry = TarEntry.CreateTarEntry(name); entry.TarHeader.Size = contents.Length; archiveMap[entry] = Blobs.From(contents); }
//[ExpectedException(typeof(ArgumentOutOfRangeException))] public void InvalidSize() { TarEntry e = TarEntry.CreateTarEntry("test"); //e.Size = -6; Assert.That(() => e.Size = -6, Throws.TypeOf <ArgumentOutOfRangeException>()); }
private void AddToTarArchive(CompressContext ctx, FileDetails fileDetails) { var tarEntry = TarEntry.CreateTarEntry(fileDetails.FullName); tarEntry.Size = fileDetails.Length; tarEntry.ModTime = fileDetails.Modified.UtcDateTime; ctx.TarStream.PutNextEntry(tarEntry); }
//[ExpectedException(typeof(ArgumentOutOfRangeException))] public void InvalidModTime() { TarEntry e = TarEntry.CreateTarEntry("test"); //e.ModTime = DateTime.MinValue; Assert.That(() => e.ModTime = DateTime.MinValue, Throws.TypeOf <ArgumentOutOfRangeException>()); }
/// <summary> /// The record name is created (the name of a separate file in the archive) /// </summary> /// <param name="title">File name with extension, this name will have the file in the archive</param> /// <param name="lastModification">Set the datetime of last modification of the entry.</param> public void CreateEntry(string title, DateTime?lastModification) { tarEntry = TarEntry.CreateTarEntry(title); if (lastModification.HasValue) { tarEntry.ModTime = lastModification.Value; } }
protected override void PutNextEntry(CloudBlob blob) { var entry = TarEntry.CreateTarEntry(GetEntryName(blob)); entry.Size = blob.Properties.Length; entry.ModTime = (blob.Properties.LastModified ?? new DateTimeOffset(2000, 01, 01, 00, 00, 00, TimeSpan.Zero)).UtcDateTime; _archiveStream.PutNextEntry(entry); }
public void BlockFactorHandling() { const int MinimumBlockFactor = 1; const int MaximumBlockFactor = 64; const int FillFactor = 2; for (int factor = MinimumBlockFactor; factor < MaximumBlockFactor; ++factor) { var ms = new MemoryStream(); using (TarOutputStream tarOut = new TarOutputStream(ms, factor, null)) { TarEntry entry = TarEntry.CreateTarEntry("TestEntry"); entry.Size = (TarBuffer.BlockSize * factor * FillFactor); tarOut.PutNextEntry(entry); byte[] buffer = new byte[TarBuffer.BlockSize]; var r = new Random(); r.NextBytes(buffer); // Last block is a partial one for (int i = 0; i < factor * FillFactor; ++i) { tarOut.Write(buffer, 0, buffer.Length); } } byte[] tarData = ms.ToArray(); Assert.IsNotNull(tarData, "Data written is null"); // Blocks = Header + Data Blocks + Zero block + Record trailer int usedBlocks = 1 + (factor * FillFactor) + 2; int totalBlocks = usedBlocks + (factor - 1); totalBlocks /= factor; totalBlocks *= factor; Assert.AreEqual(TarBuffer.BlockSize * totalBlocks, tarData.Length, "Tar file should contain {0} blocks in length", totalBlocks); if (usedBlocks < totalBlocks) { // Start at first byte after header. int byteIndex = TarBuffer.BlockSize * ((factor * FillFactor) + 1); while (byteIndex < tarData.Length) { int blockNumber = byteIndex / TarBuffer.BlockSize; int offset = blockNumber % TarBuffer.BlockSize; Assert.AreEqual(0, tarData[byteIndex], string.Format("Trailing block data should be null iteration {0} block {1} offset {2} index {3}", factor, blockNumber, offset, byteIndex)); byteIndex += 1; } } } }
//[ExpectedException(typeof(ArgumentNullException))] public void InvalidLinkName() { var e = TarEntry.CreateTarEntry("test"); //e.TarHeader.LinkName = null; Assert.That(() => e.TarHeader.LinkName = null, Throws.TypeOf <ArgumentNullException>()); }
//[ExpectedException(typeof(ArgumentNullException))] public void InvalidVersionName() { TarEntry e = TarEntry.CreateTarEntry("test"); //e.TarHeader.Version = null; Assert.That(() => e.TarHeader.Version = null, Throws.TypeOf <ArgumentNullException>()); }
private static void ZipFile(TarOutputStream zip, string filename, Stream fileStream = null) { filename = filename ?? "file"; var entry = TarEntry.CreateTarEntry(filename); entry.Size = fileStream.Length; zip.PutNextEntry(entry); fileStream.CopyTo(zip); zip.CloseEntry(); }
public void BlockFactorHandling() { const int minimumBlockFactor = 1; const int maximumBlockFactor = 64; const int fillFactor = 2; for (var factor = minimumBlockFactor; factor < maximumBlockFactor; ++factor) { var ms = new MemoryStream(); using (var tarOut = new TarOutputStream(ms, factor, nameEncoding: null)) { var entry = TarEntry.CreateTarEntry("TestEntry"); entry.Size = TarBuffer.BlockSize * factor * fillFactor; tarOut.PutNextEntry(entry); var buffer = Utils.GetDummyBytes(TarBuffer.BlockSize); // Last block is a partial one for (var i = 0; i < factor * fillFactor; ++i) { tarOut.Write(buffer, 0, buffer.Length); } } byte[] tarData = ms.ToArray(); Assert.IsNotNull(tarData, "Data written is null"); // Blocks = Header + Data Blocks + Zero block + Record trailer int usedBlocks = 1 + (factor * fillFactor) + 2; int totalBlocks = usedBlocks + (factor - 1); totalBlocks /= factor; totalBlocks *= factor; Assert.AreEqual(TarBuffer.BlockSize * totalBlocks, tarData.Length, "Tar file should contain {0} blocks in length", totalBlocks); if (usedBlocks >= totalBlocks) { continue; } // Start at first byte after header. var byteIndex = TarBuffer.BlockSize * ((factor * fillFactor) + 1); while (byteIndex < tarData.Length) { var blockNumber = byteIndex / TarBuffer.BlockSize; var offset = blockNumber % TarBuffer.BlockSize; Assert.AreEqual(0, tarData[byteIndex], "Trailing block data should be null iteration {0} block {1} offset {2} index {3}", factor, blockNumber, offset, byteIndex); byteIndex += 1; } } }
public void UserAndGroupNames() { TarEntry e = TarEntry.CreateTarEntry("test"); e.UserName = null; Assert.IsNotNull(e.UserName, "Name set to OS default"); e.UserName = ""; Assert.AreEqual(0, e.UserName.Length, "Empty name allowed"); e.GroupName = null; Assert.AreEqual("None", e.GroupName, "default group name is None"); }
public bool AddWithMetaGenerated(string sourcePath, string targetPath) { var targetPathNormalized = targetPath.Replace("\\", "/").Trim('/'); if (_addedPathSet.Contains(targetPathNormalized)) { return(false); } // guid/asset : #Assets/Folder/File.dll // /asset.meta : #Assets/Folder/File.dll.meta // /pathname : Assets/Folder/File.dll var metaData = GenerateMeta(sourcePath, targetPath); var guid = metaData.Item1; // asset if (File.Exists(sourcePath)) { var asset = TarEntry.CreateTarEntry(guid + "/asset"); var assetFile = File.ReadAllBytes(sourcePath); asset.Size = assetFile.Length; asset.ModTime = File.GetLastWriteTimeUtc(sourcePath); _stream.PutNextEntry(asset); _stream.Write(assetFile, 0, assetFile.Length); _stream.CloseEntry(); } // asset.meta { var meta = TarEntry.CreateTarEntry(guid + "/asset.meta"); var metaFile = metaData.Item2; meta.Size = metaFile.Length; meta.ModTime = File.GetLastWriteTimeUtc(sourcePath); _stream.PutNextEntry(meta); _stream.Write(metaFile, 0, metaFile.Length); _stream.CloseEntry(); } // pathname { var pathname = TarEntry.CreateTarEntry(guid + "/pathname"); var pathnameFile = Encoding.UTF8.GetBytes(targetPathNormalized); pathname.Size = pathnameFile.Length; pathname.ModTime = File.GetLastWriteTimeUtc(sourcePath); _stream.PutNextEntry(pathname); _stream.Write(pathnameFile, 0, pathnameFile.Length); _stream.CloseEntry(); } _addedPathSet.Add(targetPathNormalized); return(true); }
public static async Task AddStringToTar(TarOutputStream tarStream, string path, string data) { var buffer = Encoding.UTF8.GetBytes(data); var entry = TarEntry.CreateTarEntry(path); entry.Size = buffer.Length; tarStream.PutNextEntry(entry); await tarStream.WriteAsync(buffer); tarStream.CloseEntry(); }
public void WriteEntry(string key, Stream stream) { using (var buffered = TempStream.GetBuffered(stream)) { var entry = TarEntry.CreateTarEntry(key); entry.Size = buffered.Length; tarOutputStream.PutNextEntry(entry); buffered.Position = 0; buffered.CopyTo(tarOutputStream); tarOutputStream.CloseEntry(); } }
public void PutNextEntry(string name, long size) { if (Stream is ZipOutputStream) { ((ZipOutputStream)Stream).PutNextEntry(new ZipEntry(name)); } else if (Stream is TarOutputStream) { var tarEntry = TarEntry.CreateTarEntry(name); tarEntry.Size = size; ((TarOutputStream)Stream).PutNextEntry(tarEntry); } }