Ejemplo n.º 1
0
        /// <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>
        private void InternalWriteEntry(TarEntry sourceEntry, bool recurse)
        {
            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)
            {
                var asciiTrans = !IsBinary(entryFilename);

                if (asciiTrans)
                {
                    tempFileName = Path.GetTempFileName();

                    using (var inStream = File.OpenText(entryFilename))
                    {
                        using (Stream outStream = File.Create(tempFileName))
                        {
                            while (true)
                            {
                                var line = inStream.ReadLine();
                                if (line == null)
                                {
                                    break;
                                }
                                var 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) ? string.Format("{0}/{1}", 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
            {
                using (Stream inputStream = File.OpenRead(entryFilename))
                {
                    var localBuffer = new byte[32 * 1024];
                    while (true)
                    {
                        var numRead = inputStream.Read(localBuffer, 0, localBuffer.Length);

                        if (numRead <= 0)
                        {
                            break;
                        }

                        tarOut.Write(localBuffer, 0, numRead);
                    }
                }

                if (!string.IsNullOrEmpty(tempFileName))
                {
                    File.Delete(tempFileName);
                }

                tarOut.CloseEntry();
            }
        }
Ejemplo n.º 2
0
        /// <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>
        private void InternalWriteEntry(TarEntry sourceEntry, bool recurse)
        {
            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)
            {
                var asciiTrans = !IsBinary(entryFilename);

                if (asciiTrans)
                {
                    tempFileName = Path.GetTempFileName();

                    using (var inStream = File.OpenText(entryFilename))
                    {
                        using (Stream outStream = File.Create(tempFileName))
                        {
                            while (true)
                            {
                                var line = inStream.ReadLine();
                                if (line == null)
                                {
                                    break;
                                }
                                var 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) ? string.Format("{0}/{1}", 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
            {
                using (Stream inputStream = File.OpenRead(entryFilename))
                {
                    var localBuffer = new byte[32*1024];
                    while (true)
                    {
                        var numRead = inputStream.Read(localBuffer, 0, localBuffer.Length);

                        if (numRead <= 0)
                        {
                            break;
                        }

                        tarOut.Write(localBuffer, 0, numRead);
                    }
                }

                if (!string.IsNullOrEmpty(tempFileName))
                {
                    File.Delete(tempFileName);
                }

                tarOut.CloseEntry();
            }
        }