I cannot add . txt files in . tar(C#) . net framework 4.6.1

Asked

Viewed 43 times

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?

  • No. actually the same problem is that it is not adding the . txt files in . RAR

  • Dude, if the destination folder is the same as the source. in your code you try to add the file .tar inside himself, while he’s open...

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.