private void WriteObjectToTar (TarOutputStream tar_out, FileSystemObject fso, EventTracker tracker) { MemoryStream memory = null; TarHeader header; header = new TarHeader (); StringBuilder name_builder; name_builder = new StringBuilder (fso.FullName); name_builder.Remove (0, this.FullName.Length+1); header.Name = name_builder.ToString (); header.ModTime = fso.Timestamp; if (fso is DirectoryObject) { header.Mode = 511; // 0777 header.TypeFlag = TarHeader.LF_DIR; header.Size = 0; } else { header.Mode = 438; // 0666 header.TypeFlag = TarHeader.LF_NORMAL; memory = new MemoryStream (); ((FileObject) fso).AddToStream (memory, tracker); header.Size = memory.Length; } TarEntry entry; entry = new TarEntry (header); tar_out.PutNextEntry (entry); if (memory != null) { tar_out.Write (memory.ToArray (), 0, (int) memory.Length); memory.Close (); } tar_out.CloseEntry (); // If this is a directory, write out the children if (fso is DirectoryObject) foreach (FileSystemObject child in fso.Children) WriteObjectToTar (tar_out, child, tracker); }
public void TestExtractArchiveTarGzCreateContainer() { CloudFilesProvider provider = (CloudFilesProvider)Bootstrapper.CreateObjectStorageProvider(); string containerName = TestContainerPrefix + Path.GetRandomFileName(); string sourceFileName = "DarkKnightRises.jpg"; byte[] content = File.ReadAllBytes("DarkKnightRises.jpg"); using (MemoryStream outputStream = new MemoryStream()) { using (GZipOutputStream gzoStream = new GZipOutputStream(outputStream)) { gzoStream.IsStreamOwner = false; gzoStream.SetLevel(9); using (TarOutputStream tarOutputStream = new TarOutputStream(gzoStream)) { tarOutputStream.IsStreamOwner = false; TarEntry entry = TarEntry.CreateTarEntry(containerName + '/' + sourceFileName); entry.Size = content.Length; tarOutputStream.PutNextEntry(entry); tarOutputStream.Write(content, 0, content.Length); tarOutputStream.CloseEntry(); tarOutputStream.Close(); } } outputStream.Flush(); outputStream.Position = 0; ExtractArchiveResponse response = provider.ExtractArchive(outputStream, "", ArchiveFormat.TarGz); Assert.IsNotNull(response); Assert.AreEqual(1, response.CreatedFiles); Assert.IsNotNull(response.Errors); Assert.AreEqual(0, response.Errors.Count); } using (MemoryStream downloadStream = new MemoryStream()) { provider.GetObject(containerName, sourceFileName, downloadStream, verifyEtag: true); Assert.AreEqual(content.Length, GetContainerObjectSize(provider, containerName, sourceFileName)); downloadStream.Position = 0; byte[] actualData = new byte[downloadStream.Length]; downloadStream.Read(actualData, 0, actualData.Length); Assert.AreEqual(content.Length, actualData.Length); using (MD5 md5 = MD5.Create()) { byte[] contentMd5 = md5.ComputeHash(content); byte[] actualMd5 = md5.ComputeHash(actualData); Assert.AreEqual(BitConverter.ToString(contentMd5), BitConverter.ToString(actualMd5)); } } /* Cleanup */ provider.DeleteContainer(containerName, deleteObjects: true); }
/// <summary> /// Write an entry to the archive. This method will call the putNextEntry /// and then write the contents of the entry, and finally call closeEntry() /// for entries that are files. For directories, it will call putNextEntry(), /// and then, if the recurse flag is true, process each entry that is a /// child of the directory. /// </summary> /// <param name="sourceEntry"> /// The TarEntry representing the entry to write to the archive. /// </param> /// <param name="recurse"> /// If true, process the children of directory entries. /// </param> void InternalWriteEntry(TarEntry sourceEntry, bool recurse) { var asciiTrans = false; string tempFileName = null; var entryFilename = sourceEntry.File; var entry = (TarEntry)sourceEntry.Clone(); if (applyUserInfoOverrides) { entry.GroupId = groupId; entry.GroupName = groupName; entry.UserId = userId; entry.UserName = userName; } OnProgressMessageEvent(entry, null); if (asciiTranslate && !entry.IsDirectory) { asciiTrans = !IsBinary(entryFilename); if (asciiTrans) { tempFileName = Path.GetTempFileName(); var inStream = File.OpenText(entryFilename); Stream outStream = File.Create(tempFileName); while (true) { var line = inStream.ReadLine(); if (line == null) { break; } var data = Encoding.ASCII.GetBytes(line); outStream.Write(data, 0, data.Length); outStream.WriteByte((byte)'\n'); } inStream.Close(); outStream.Flush(); outStream.Close(); entry.Size = new FileInfo(tempFileName).Length; entryFilename = tempFileName; } } string newName = null; if (rootPath != null) { if (entry.Name.StartsWith(rootPath)) { newName = entry.Name.Substring(rootPath.Length + 1); } } if (pathPrefix != null) { newName = (newName == null) ? pathPrefix + "/" + entry.Name : pathPrefix + "/" + newName; } if (newName != null) { entry.Name = newName; } tarOut.PutNextEntry(entry); if (entry.IsDirectory) { if (recurse) { var list = entry.GetDirectoryEntries(); for (var i = 0; i < list.Length; ++i) { InternalWriteEntry(list[i], recurse); } } } else { Stream inputStream = File.OpenRead(entryFilename); var numWritten = 0; var eBuf = new byte[32 * 1024]; while (true) { var numRead = inputStream.Read(eBuf, 0, eBuf.Length); if (numRead <= 0) { break; } tarOut.Write(eBuf, 0, numRead); numWritten += numRead; } inputStream.Close(); if (tempFileName != null && tempFileName.Length > 0) { File.Delete(tempFileName); } tarOut.CloseEntry(); } }
private void WriteEntryCore(TarEntry sourceEntry, bool recurse) { string text = null; string text2 = sourceEntry.File; TarEntry tarEntry = (TarEntry)sourceEntry.Clone(); if (applyUserInfoOverrides) { tarEntry.GroupId = groupId; tarEntry.GroupName = groupName; tarEntry.UserId = userId; tarEntry.UserName = userName; } OnProgressMessageEvent(tarEntry, null); if (asciiTranslate && !tarEntry.IsDirectory && !IsBinary(text2)) { text = Path.GetTempFileName(); using (StreamReader streamReader = File.OpenText(text2)) { using (Stream stream = File.Create(text)) { while (true) { string text3 = streamReader.ReadLine(); if (text3 == null) { break; } byte[] bytes = Encoding.ASCII.GetBytes(text3); stream.Write(bytes, 0, bytes.Length); stream.WriteByte(10); } stream.Flush(); } } tarEntry.Size = new FileInfo(text).Length; text2 = text; } string text4 = null; if (rootPath != null && tarEntry.Name.StartsWith(rootPath)) { text4 = tarEntry.Name.Substring(rootPath.Length + 1); } if (pathPrefix != null) { text4 = ((text4 == null) ? (pathPrefix + "/" + tarEntry.Name) : (pathPrefix + "/" + text4)); } if (text4 != null) { tarEntry.Name = text4; } tarOut.PutNextEntry(tarEntry); if (tarEntry.IsDirectory) { if (recurse) { TarEntry[] directoryEntries = tarEntry.GetDirectoryEntries(); for (int i = 0; i < directoryEntries.Length; i++) { WriteEntryCore(directoryEntries[i], recurse); } } } else { using (Stream stream2 = File.OpenRead(text2)) { byte[] array = new byte[32768]; while (true) { int num = stream2.Read(array, 0, array.Length); if (num <= 0) { break; } tarOut.Write(array, 0, num); } } if (text != null && text.Length > 0) { File.Delete(text); } tarOut.CloseEntry(); } }
protected override void _Partir(string fichero, string sal1, string dir, long kb) { FileInfo fi = null; DirectoryInfo din = null; DirectoryInfo dout = new DirectoryInfo (dir); if (File.Exists (fichero)) { fi = new FileInfo (fichero); } else if (Directory.Exists (fichero)) { din = new DirectoryInfo (fichero); } else { throw new Exception ("" + fichero + " not found"); } List<FileInfo> files = load (fichero); string baseName = ""; if (fi != null) { baseName = fi.Name; } else if (din != null) { baseName = din.Name; } if ((sal1 == null) || (sal1 == string.Empty)) { // if (din != null) { sal1 = din.Name; } if (fi != null) { sal1 = fi.Name; } } long totalSize = calculateTotalSize (files); long fragments = totalSize / (kb * 1024); string s = "" + fragments; JoinInfo info = new JoinInfo (); info.OriginalFile = baseName; info.InitialFragment = 0; info.Digits = Math.Max (s.Length, 3); info.BaseName = sal1 + ".tar.gz."; info.Directory = dout; info.Length = totalSize; Stream stream = new SplitStream (info, kb * 1024, info.Directory.FullName + Path.DirectorySeparatorChar + info.BaseName + "sha512sum.dalle", "SHA512"); stream = new GZipStream (stream, CompressionMode.Compress); TarOutputStream taros = new TarOutputStream (stream); foreach (FileInfo f in files) { TarEntry te = TarEntry.CreateEntryFromFile (f.FullName); te.UserId = 0; te.GroupId = 0; te.UserName = String.Empty; te.GroupName = String.Empty; taros.PutNextEntry (te); FileStream fs = f.OpenRead (); long leidosTotales = 0; byte[] buffer = new byte[Consts.BUFFER_LENGTH]; int leidos = 0; while ((leidos = fs.Read (buffer, 0, buffer.Length)) > 0) { taros.Write (buffer, 0, leidos); leidosTotales += leidos; OnProgress (leidosTotales, totalSize); } taros.CloseEntry (); fs.Close (); } taros.Close (); OnProgress (totalSize, totalSize); }
/// <summary> /// Write an entry to the archive. This method will call the putNextEntry /// and then write the contents of the entry, and finally call closeEntry() /// for entries that are files. For directories, it will call putNextEntry(), /// and then, if the recurse flag is true, process each entry that is a /// child of the directory. /// </summary> /// <param name="sourceEntry"> /// The TarEntry representing the entry to write to the archive. /// </param> /// <param name="recurse"> /// If true, process the children of directory entries. /// </param> void WriteEntryCore(TarEntry sourceEntry, bool recurse) { string tempFileName = null; string entryFilename = sourceEntry.File; TarEntry entry = (TarEntry)sourceEntry.Clone(); if (applyUserInfoOverrides) { entry.GroupId = groupId; entry.GroupName = groupName; entry.UserId = userId; entry.UserName = userName; } OnProgressMessageEvent(entry, null); if (asciiTranslate && !entry.IsDirectory) { if (!IsBinary(entryFilename)) { tempFileName = Path.GetTempFileName(); using (StreamReader inStream = File.OpenText(entryFilename)) { using (Stream outStream = File.Create(tempFileName)) { while (true) { string line = inStream.ReadLine(); if (line == null) { break; } byte[] data = Encoding.UTF8.GetBytes(line); outStream.Write(data, 0, data.Length); outStream.WriteByte((byte)'\n'); } outStream.Flush(); } } entry.Size = new FileInfo(tempFileName).Length; entryFilename = tempFileName; } } string newName = null; if (rootPath != null) { if (entry.Name.StartsWith(rootPath)) { newName = entry.Name.Substring(rootPath.Length + 1); } } if (pathPrefix != null) { newName = (newName == null) ? pathPrefix + "/" + entry.Name : pathPrefix + "/" + newName; } if (newName != null) { entry.Name = newName; } tarOut.PutNextEntry(entry); if (entry.IsDirectory) { if (recurse) { TarEntry[] list = entry.GetDirectoryEntries(); for (int i = 0; i < list.Length; ++i) { WriteEntryCore(list[i], recurse); } } } else { using (Stream inputStream = File.OpenRead(entryFilename)) { byte[] localBuffer = new byte[32 * 1024]; while (true) { int numRead = inputStream.Read(localBuffer, 0, localBuffer.Length); if (numRead <= 0) { break; } tarOut.Write(localBuffer, 0, numRead); } } if ((tempFileName != null) && (tempFileName.Length > 0)) { File.Delete(tempFileName); } tarOut.CloseEntry(); } }
private void TarFile(TarOutputStream stream, IPurePath baseDir, IPurePath sourcePath) { var tarName = baseDir.WithFilename(sourcePath.Filename); var entry = TarEntry.CreateTarEntry(tarName.ToString()); using (var file = File.OpenRead(sourcePath.ToString())) { entry.Size = file.Length; stream.PutNextEntry(entry); file.CopyTo(stream); } stream.CloseEntry(); }
private void WriteEntryCore(TarEntry sourceEntry, bool recurse) { //IL_00de: Unknown result type (might be due to invalid IL or missing references) string text = null; string text2 = sourceEntry.File; TarEntry tarEntry = (TarEntry)sourceEntry.Clone(); if (applyUserInfoOverrides) { tarEntry.GroupId = groupId; tarEntry.GroupName = groupName; tarEntry.UserId = userId; tarEntry.UserName = userName; } OnProgressMessageEvent(tarEntry, null); if (asciiTranslate && !tarEntry.IsDirectory && !IsBinary(text2)) { text = Path.GetTempFileName(); StreamReader val = File.OpenText(text2); try { Stream val2 = (Stream)(object)File.Create(text); try { while (true) { string text3 = ((TextReader)val).ReadLine(); if (text3 == null) { break; } byte[] bytes = Encoding.get_ASCII().GetBytes(text3); val2.Write(bytes, 0, bytes.Length); val2.WriteByte((byte)10); } val2.Flush(); } finally { ((global::System.IDisposable)val2)?.Dispose(); } } finally { ((global::System.IDisposable)val)?.Dispose(); } tarEntry.Size = new FileInfo(text).get_Length(); text2 = text; } string text4 = null; if (rootPath != null && tarEntry.Name.StartsWith(rootPath)) { text4 = tarEntry.Name.Substring(rootPath.get_Length() + 1); } if (pathPrefix != null) { text4 = ((text4 == null) ? (pathPrefix + "/" + tarEntry.Name) : (pathPrefix + "/" + text4)); } if (text4 != null) { tarEntry.Name = text4; } tarOut.PutNextEntry(tarEntry); if (tarEntry.IsDirectory) { if (recurse) { TarEntry[] directoryEntries = tarEntry.GetDirectoryEntries(); for (int i = 0; i < directoryEntries.Length; i++) { WriteEntryCore(directoryEntries[i], recurse); } } return; } Stream val3 = (Stream)(object)File.OpenRead(text2); try { byte[] array = new byte[32768]; while (true) { int num = val3.Read(array, 0, array.Length); if (num > 0) { ((Stream)tarOut).Write(array, 0, num); continue; } break; } } finally { ((global::System.IDisposable)val3)?.Dispose(); } if (text != null && text.get_Length() > 0) { File.Delete(text); } tarOut.CloseEntry(); }