/**
         * Builds and returns the layer {@link Blob}.
         *
         * @return the new layer
         */
        public IBlob Build()
        {
            UniqueTarArchiveEntries uniqueTarArchiveEntries = new UniqueTarArchiveEntries();

            // Adds all the layer entries as tar entries.
            foreach (LayerEntry layerEntry in layerEntries)
            {
                // Adds the entries to uniqueTarArchiveEntries, which makes sure all entries are unique and
                // adds parent directories for each extraction path.
                TarEntry entry = TarEntry.CreateEntryFromFile(layerEntry.SourceFile);
                entry.Name = layerEntry.ExtractionPath.ToString().TrimStart('/');

                if (Directory.Exists(layerEntry.SourceFile))
                {
                    entry.Name += '/';
                    entry.TarHeader.TypeFlag = TarHeader.LF_DIR;
                }

                // Sets the entry's permissions by masking out the permission bits from the entry's mode (the
                // lowest 9 bits) then using a bitwise OR to set them to the layerEntry's permissions.
                entry.SetMode((entry.GetMode() & ~PosixFilePermissions.All) | layerEntry.Permissions.GetPermissionBits());
                entry.ModTime = DateTimeOffset.FromUnixTimeMilliseconds(layerEntry.LastModifiedTime.ToUnixTimeMilliseconds()).DateTime;

                uniqueTarArchiveEntries.Add(entry);
            }

            // Gets the entries sorted by extraction path.
            IList <TarEntry> sortedFilesystemEntries = uniqueTarArchiveEntries.GetSortedEntries();

            HashSet <string> names = new HashSet <string>();

            // Adds all the files to a tar stream.
            TarStreamBuilder tarStreamBuilder = new TarStreamBuilder();

            foreach (TarEntry entry in sortedFilesystemEntries)
            {
                // Strips out all non-reproducible elements from tar archive entries.
                // Modified time is configured per entry
                entry.TarHeader.GroupId   = 0;
                entry.TarHeader.UserId    = 0;
                entry.TarHeader.UserName  = "";
                entry.TarHeader.GroupName = "";

                Debug.Assert(!names.Contains(entry.Name));
                names.Add(entry.Name);

                tarStreamBuilder.AddTarArchiveEntry(entry);
            }

            return(Blobs.From(tarStreamBuilder.WriteAsTarArchiveToAsync, -1));
        }