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();
            }
        }
Exemple #2
0
        private void createTARAndTxLog(int partNumber)
        {
            FileInfo fioi   = new FileInfo(fileLogFileDialog.FileName);
            String   slogId = fioi.Name.Replace(".csv", "");

            DateTime dt = new DateTime();

            dt = DateTime.Now;
            String dtString = dt.ToString();

            dtString = dtString.Replace("/", "_").Replace(":", "").Replace(" ", "");

            tarLogName = "GlacierTARBall_" + slogId + "_Part" + Convert.ToString(partNumber) + "_" + dtString + "_LOG.csv";
            tarLog     = new StreamWriter(tarLogName);
            tarLog.WriteLine("Zip FileLoc" + "," + "SHA256 Compressed" + "," + "FileName" + "," + "SHA 256 Decompressed");

            archiveName = "GlacierTARBall_" + slogId + "_Part" + Convert.ToString(partNumber) + "_" + dtString + ".tar";

            log.Info("Making Tar Log : " + "GlacierTarLog_" + slogId + "_Part" + Convert.ToString(partNumber) + "_" + dtString + ".csv");

            log.Info("Tar FileName : " + "GlacierTARBall_" + slogId + "_Part" + Convert.ToString(partNumber) + "_" + dtString + ".tar");

            FileStream fs = File.Create(archiveName);

            TarOutputStream stream = new TarOutputStream((Stream)fs);

            archive = TarArchive.CreateOutputTarArchive(stream);
        }
Exemple #3
0
 /// <summary>
 /// 向包流中追加文件
 /// </summary>
 /// <param name="tar">zip流</param>
 /// <param name="file">文件名</param>
 /// <param name="path_len">根目录长度</param>
 /// <param name="fcp">进度事件</param>
 public static void AddFile(this TarOutputStream tar, string file, int path_len, FileCompressProgress fcp = null)
 {
     if (File.Exists(file))
     {
         var head = new TarHeader();
         head.Name    = file.Substring(path_len);            //设置文件名
         head.ModTime = new FileInfo(file).LastWriteTimeUtc; //设置添加时间
         using (var fs = File.OpenRead(file))
         {
             head.Size = fs.Length;                //设置文件大小
             tar.PutNextEntry(new TarEntry(head)); //开始处理
             var buffer = new byte[_buffer];       //缓存
             int i;
             while ((i = fs.Read(buffer, 0, buffer.Length)) > 0)
             {
                 if (i < _buffer)//处理剩余字节
                 {
                     var b = new byte[i];
                     Buffer.BlockCopy(buffer, 0, b, 0, i);
                     buffer = b;
                 }
                 tar.Write(buffer, 0, buffer.Length);                        //写入流
                 fcp?.BeginInvoke(file, fs.Length, fs.Position, null, null); //通知进度
             }
             tar.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);
                }
            }
        }
Exemple #5
0
        /// <inheritdoc />
        public async Task CopyFileAsync(string id, string filePath, byte[] fileContent, int accessMode, int userId, int groupId, CancellationToken ct = default)
        {
            using (var memStream = new MemoryStream())
            {
                using (var tarOutputStream = new TarOutputStream(memStream, Encoding.Default))
                {
                    tarOutputStream.IsStreamOwner = false;
                    tarOutputStream.PutNextEntry(
                        new TarEntry(
                            new TarHeader
                    {
                        Name    = filePath,
                        UserId  = userId,
                        GroupId = groupId,
                        Mode    = accessMode,
                        Size    = fileContent.Length,
                    }));

#if NETSTANDARD2_1_OR_GREATER
                    await tarOutputStream.WriteAsync(fileContent.AsMemory(0, fileContent.Length), ct)
                    .ConfigureAwait(false);
#else
                    await tarOutputStream.WriteAsync(fileContent, 0, fileContent.Length, ct)
                    .ConfigureAwait(false);
#endif

                    tarOutputStream.CloseEntry();
                }

                memStream.Seek(0, SeekOrigin.Begin);

                await this.containers.ExtractArchiveToContainerAsync(id, "/", memStream, ct)
                .ConfigureAwait(false);
            }
        }
        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();
        }
Exemple #7
0
        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}");
            }
        }
        public void OutputStreamOwnership()
        {
            var memStream = new TrackedMemoryStream();
            var s         = new TarOutputStream(memStream);

            Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially");
            Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially");

            s.Close();

            Assert.IsTrue(memStream.IsClosed, "Should be closed after parent owner close");
            Assert.IsTrue(memStream.IsDisposed, "Should be disposed after parent owner close");

            memStream = new TrackedMemoryStream();
            s         = new TarOutputStream(memStream);

            Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially");
            Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially");

            s.IsStreamOwner = false;
            s.Close();

            Assert.IsFalse(memStream.IsClosed, "Should not be closed after parent owner close");
            Assert.IsFalse(memStream.IsDisposed, "Should not be disposed after parent owner close");
        }
Exemple #9
0
        internal static void RecurseDir(string path, TarOutputStream stream, int offset, string outputpath)
        {
            string[] files;
            try
            {
                files = Directory.GetFiles(path);
            }
            catch
            {
                return;
            }

            foreach (var f in files)
            {
                if (Path.GetFullPath(f).Equals(outputpath))
                {
                    continue;
                }

                var temp = f.Replace("\\", "/");

                TarFile(temp, stream, offset);
            }

            foreach (var f in Directory.GetDirectories(path))
            {
                RecurseDir(f, stream, offset, outputpath);
            }
        }
Exemple #10
0
        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();
            }
        }
Exemple #11
0
        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();
        }
Exemple #12
0
        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);
        }
Exemple #13
0
        /// <summary>
        /// SharpZipLib doesn't account for illegal file names or characters in
        /// file names (e.g. ':' in Windows), so first we stream the tar to a
        /// new tar, sanitizing any of the contained bad file names as we go.
        /// We also need to record the modification times of all the files, so
        /// that we can restore them into the final zip.
        /// </summary>
        public static void SanitizeTarForWindows(string inputTar, string outputTar, Action cancellingDelegate)
        {
            using (var fsIn = File.OpenRead(inputTar))
                using (var inputStream = new TarInputStream(fsIn))
                    using (var fsOut = File.OpenWrite(outputTar))
                        using (var outputStream = new TarOutputStream(fsOut))
                        {
                            TarEntry entry;
                            byte[]   buf       = new byte[8 * 1024];
                            var      usedNames = new Dictionary <string, string>();

                            while ((entry = inputStream.GetNextEntry()) != null)
                            {
                                cancellingDelegate?.Invoke();

                                entry.Name = SanitizeTarName(entry.Name, usedNames);
                                outputStream.PutNextEntry(entry);

                                long bytesToCopy = entry.Size;
                                while (bytesToCopy > 0)
                                {
                                    int bytesRead = inputStream.Read(buf, 0, Math.Min(bytesToCopy > int.MaxValue ? int.MaxValue : (int)bytesToCopy, buf.Length));
                                    outputStream.Write(buf, 0, bytesRead);
                                    bytesToCopy -= bytesRead;
                                }

                                outputStream.CloseEntry();
                            }
                        }
        }
Exemple #14
0
        private async Task AddDirectoryFilesToTar(TarOutputStream tarArchive, IDirectoryInfo directory)
        {
            if (!string.IsNullOrWhiteSpace(GetRelativePath(directory)))
            {
                var tarEntry = TarEntry.CreateEntryFromFile(directory.FullName);
                tarEntry.Name = GetRelativePath(directory);
                tarArchive.PutNextEntry(tarEntry);
            }

            // Write each file to the tar.

            foreach (var file in directory.GetFiles())
            {
                var tarEntry = TarEntry.CreateEntryFromFile(file.FullName);
                tarEntry.Name = GetRelativePath(file);
                tarEntry.Size = file.Length;

                tarArchive.PutNextEntry(tarEntry);

                using var fileStream = file.OpenRead();
                await fileStream.CopyToAsync(tarArchive);

                tarArchive.CloseEntry();
            }

            foreach (var childDirectory in directory.GetDirectories())
            {
                await AddDirectoryFilesToTar(tarArchive, childDirectory);
            }
        }
Exemple #15
0
        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());
            }
        }
Exemple #16
0
 public ZipWriteOperator(TempStream tempStream, string targetFile)
 {
     file             = new FileStream(targetFile, FileMode.Create);
     gZipOutputStream = new GZipOutputStream(file);
     tarOutputStream  = new TarOutputStream(gZipOutputStream, Encoding.UTF8);
     TempStream       = tempStream;
 }
Exemple #17
0
        /// <summary>
        /// When overridden in a derived class, executes the task.
        /// </summary>
        /// <returns>
        /// true if the task successfully executed; otherwise, false.
        /// </returns>
        public override bool Execute()
        {
            var buffer   = new byte[32 * 1024];
            var rootName = string.IsNullOrWhiteSpace(RootName) ? "" : RootName + "/";

            using (var gz = new GZipOutputStream(File.Create(Out)))
                using (var outStream = new TarOutputStream(gz))
                {
                    gz.SetLevel(3);

                    foreach (var file in Include.Select(x => x.ItemSpec.ToNPath()).Where(x => x.FileExists()))
                    {
                        AddToArchive(rootName, file, outStream, ref buffer);
                    }

                    foreach (var dir in Include.Select(x => x.ItemSpec.ToNPath()).Where(x => x.DirectoryExists()))
                    {
                        foreach (var file in dir.Contents(true))
                        {
                            var relativePath = file.DirectoryExists() ? file.RelativeTo(dir) : file.Parent.RelativeTo(dir);
                            var baseName     = rootName;
                            if (!relativePath.IsEmpty)
                            {
                                baseName += relativePath.ToString(SlashMode.Forward) + "/";
                            }
                            AddToArchive(baseName, file, outStream, ref buffer);
                        }
                    }
                }
            return(true);
        }
Exemple #18
0
        public void SingleLargeEntry()
        {
            const string       EntryName = "LargeTarEntry";
            const TestDataSize dataSize  = TestDataSize.Large;

            PerformanceTesting.TestReadWrite(
                size: dataSize,
                input: bs =>
            {
                var tis   = new TarInputStream(bs, null);
                var entry = tis.GetNextEntry();

                Assert.AreEqual(EntryName, entry.Name);
                return(tis);
            },
                output: bs =>
            {
                var tos = new TarOutputStream(bs, null);
                tos.PutNextEntry(new TarEntry(new TarHeader()
                {
                    Name = EntryName,
                    Size = (int)dataSize,
                }));
                return(tos);
            },
                outputClose: stream =>
            {
                ((TarOutputStream)stream).CloseEntry();
            }
                );
        }
        /// <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();
            }
        }
Exemple #20
0
        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();
                }
            }
        }
Exemple #21
0
            /// <summary>
            /// Initializes a new instance of the TarStreamHelper class
            /// </summary>
            /// <param name="create">indicates whether to create or open an archive</param>
            /// <param name="useGz">indicates whether to use gzip compression</param>
            /// <param name="sourceStream">provides the stream where to save data or to read data from</param>
            /// <param name="ownsStream">indicates whether the sourceStream is owned by this object</param>
            private TarStreamHelper(bool create, bool useGz, Stream sourceStream, bool ownsStream)
                : this()
            {
                if (ownsStream)
                {
                    subStreams.Push(sourceStream);
                }

                Stream parentStream = sourceStream;

                if (useGz)
                {
                    parentStream = create
                                       ? (Stream) new GZipOutputStream(sourceStream)
                                       : new GZipInputStream(sourceStream);
                    subStreams.Push(parentStream);
                }

                if (create)
                {
                    OutputStream = new TarOutputStream(parentStream);
                    subStreams.Push(OutputStream);
                }
                else
                {
                    InputStream = new TarInputStream(parentStream);
                    subStreams.Push(InputStream);
                }
            }
Exemple #22
0
        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();
        }
Exemple #23
0
 public void Dispose()
 {
     if (_stream != null)
     {
         _stream.Close();
         _stream = null;
     }
 }
Exemple #24
0
            private static ArchiveOutputStream CreateTgzStream(System.IO.Stream stream)
            {
                GZipOutputStream gzipStream = new GZipOutputStream(stream);
                TarOutputStream  tarStream  = new TarOutputStream(gzipStream);

                gzipStream.IsStreamOwner = false;
                return(new ArchiveOutputStream(tarStream));
            }
Exemple #25
0
        public TarCore(CloudBlobClient blobClient, Stream outputStream) : base(blobClient, outputStream)
        {
            var archiveStream = new TarOutputStream(outputStream);

            archiveStream.IsStreamOwner = false;

            _archiveStream = archiveStream;
        }
 /// <summary></summary>
 /// <param name="stream">Accepts a new stream, it will contain an archive upon completion of work</param>
 public void SetStream(Stream stream)
 {
     gzoStream = new GZipOutputStream(stream)
     {
         IsStreamOwner = false
     };
     gzip = new TarOutputStream(gzoStream, Encoding.UTF8);
     gzoStream.IsStreamOwner = false;
 }
Exemple #27
0
        async Task AddTarNodeAsync(Cid cid, string name, TarOutputStream tar, CancellationToken cancel)
        {
            var block = await ipfs.Block.GetAsync(cid, cancel).ConfigureAwait(false);

            var dm = new DataMessage {
                Type = DataType.Raw
            };
            DagNode dag = null;

            if (cid.ContentType == "dag-pb")
            {
                dag = new DagNode(block.DataStream);
                dm  = Serializer.Deserialize <DataMessage>(dag.DataStream);
            }
            var entry  = new TarEntry(new TarHeader());
            var header = entry.TarHeader;

            header.Mode      = 0x1ff; // 777 in octal
            header.LinkName  = String.Empty;
            header.UserName  = String.Empty;
            header.GroupName = String.Empty;
            header.Version   = "00";
            header.Name      = name;
            header.DevMajor  = 0;
            header.DevMinor  = 0;
            header.UserId    = 0;
            header.GroupId   = 0;
            header.ModTime   = DateTime.Now;

            if (dm.Type == DataType.Directory)
            {
                header.TypeFlag = TarHeader.LF_DIR;
                header.Size     = 0;
                tar.PutNextEntry(entry);
                tar.CloseEntry();
            }
            else // Must be a file
            {
                var content = await ReadFileAsync(cid, cancel).ConfigureAwait(false);

                header.TypeFlag = TarHeader.LF_NORMAL;
                header.Size     = content.Length;
                tar.PutNextEntry(entry);
                await content.CopyToAsync(tar);

                tar.CloseEntry();
            }

            // Recurse over files and subdirectories
            if (dm.Type == DataType.Directory)
            {
                foreach (var link in dag.Links)
                {
                    await AddTarNodeAsync(link.Id, $"{name}/{link.Name}", tar, cancel).ConfigureAwait(false);
                }
            }
        }
Exemple #28
0
        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;
                    }
                }
            }
        }
        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();
        }
Exemple #30
0
        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;
                }
            }
        }
Exemple #31
0
        /// <summary>
        /// Initialise a TarArchive for output.
        /// </summary>
        /// <param name="stream">The <see cref="TarOutputStream"/> to use for output.</param> 
        protected TarArchive(TarOutputStream stream)
        {
            if ( stream == null ) {
                throw new ArgumentNullException("stream");
            }

            tarOut = stream;
        }