1
Good morning!
I want to add the . txt files (as shown below) in . TAR
[![insert image description here][1][1]
but I’m not getting it. the file . TAR is created, but the files are not added. Follow picture:
[![insert image description here][2][2]
Follows code.
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Tar;
using System.IO;
namespace Milhous.Recorder.Homolog.CompressInTar
{
    public class TarCreateFromStream
    {
        public void CreateTar(string outputTarFilename, string sourceDirectory)
        {
            using (FileStream fs = new FileStream(outputTarFilename, FileMode.Create, FileAccess.Write, FileShare.None))
            using (Stream gzipStream = new GZipOutputStream(fs))
            using (TarArchive tarArchive = TarArchive.CreateOutputTarArchive(gzipStream))
            {
                AddDirectoryFilesToTar(tarArchive, sourceDirectory, true);
            }
        }
        private void AddDirectoryFilesToTar(TarArchive tarArchive, string sourceDirectory, bool recurse)
        {
            if (recurse)
            {
                string[] directories = Directory.GetDirectories(sourceDirectory);
                foreach (string directory in directories)
                    AddDirectoryFilesToTar(tarArchive, directory, recurse);
            }
            string[] filenames = Directory.GetFiles(sourceDirectory);
            foreach (string filename in filenames)
            {
                TarEntry tarEntry = TarEntry.CreateEntryFromFile(filename);
                tarArchive.WriteEntry(tarEntry, true);
            }
        }
    }
}
						
Look... the error message is very clear... you tried to run the code without having these files opened anywhere?
– Leandro Angelo
No. actually the same problem is that it is not adding the . txt files in . RAR
– Thiago Simão
Dude, if the destination folder is the same as the source. in your code you try to add the file
.tarinside himself, while he’s open...– Leandro Angelo