Home>

I need to create a tar archive, which will contain csv files, from data of type string (it is also possible to use byte []) at the moment I'm trying to do something like this

public void ExportTAR (string tgzFilename, params string [] files)
    {
        using (var outStream= File.Create (tgzFilename))
        using (var gzoStream= new GZipOutputStream (outStream))
        using (var tarArchive= TarArchive.CreateOutputTarArchive (gzoStream))
        {
            int i= 0;
            foreach (var file in files)
            {
                var tarEntry= TarEntry.CreateTarEntry ("name" + i);
                //what are the actions to write to the tarentry of the line
                tarArchive.WriteEntry (tarEntry, true);
                i ++;
            }
        }
    }

How can I do this?

Doesn't this code work? As I understand it, you want to create a tgz (.tar.gz) archive, not a tar archive?

aepot2021-11-15 09:37:46

roughly speaking, I need to compile an archive from an array of lines, each line is a separate file (or, in other words, the content of this file, if it is clearer)

MRsandman2021-11-15 09:41:42

The file path is clearer this way.

aepot2021-11-15 09:42:16
  • Answer # 1

    As I understand it, you are this exampledo with the library SharpZipLib... But you need to shove several files into the archive at once.

    Let's take an example for one file

    private void CreateTarGZ (string tgzFilename, string fileName)
    {
        using (var outStream= File.Create (tgzFilename))
        using (var gzoStream= new GZipOutputStream (outStream))
        using (var tarArchive= TarArchive.CreateOutputTarArchive (gzoStream))
        {
            tarArchive.RootPath= Path.GetDirectoryName (fileName);
            var tarEntry= TarEntry.CreateEntryFromFile (fileName);
            tarEntry.Name= Path.GetFileName (fileName);
            tarArchive.WriteEntry (tarEntry, true);
        }
    }
    

    And rewrite under several

    private void CreateTarGZ (string tgzFilename, params string [] fileNames)
    {
        using (var outStream= File.Create (tgzFilename))
        using (var gzoStream= new GZipOutputStream (outStream))
        using (var tarArchive= TarArchive.CreateOutputTarArchive (gzoStream))
        {
            foreach (string fileName in fileNames)
            {
                tarArchive.RootPath= Path.GetDirectoryName (fileName);
                var tarEntry= TarEntry.CreateEntryFromFile (fileName);
                tarEntry.Name= Path.GetFileName (fileName);
                tarArchive.WriteEntry (tarEntry, true);
            }
        }
    }
    

    perhaps I did not put the question correctly or you were misled by the name of the variables, I will reformulate the question. I need to create an archive of strings (array string [], where each element of the array is the content of a separate file (eg, new string [] {"sometextsometextsometextsometextsometext", "sometext2sometext2sometext2sometext2"}) located in the application RAM

    Okay, no question, but the library is strange, or I haven't found how to feed it a data stream, I'll have to use crutches. That is, we first write to disk in a temporary file.

    private void CreateTarGZ (string tgzFilename, params string [] data)
    {
        using (var outStream= File.Create (tgzFilename))
        using (var gzoStream= new GZipOutputStream (outStream))
        using (var tarArchive= TarArchive.CreateOutputTarArchive (gzoStream))
        {
            int i= 0;
            foreach (string text in data)
            {
                string name= $ "{++ i} .txt";
                string path= Path.GetTempFileName ();
                File.WriteAllText (path, text);
                var tarEntry= TarEntry.CreateEntryFromFile (path);
                tarEntry.Name= name;
                tarArchive.WriteEntry (tarEntry, true);
                File.Delete (path);
            }
        }
    }
    

    Perhaps I did not put the question correctly or you were misled by the naming of the variables, I will reformulate the question. I need to create an archive of strings (array string [], where each element of the array is the content of a separate file (eg, new string [] {"sometextsometextsometextsometextsometext", "sometext2sometext2sometext2sometext2"}) located in the application RAM

    MRsandman2021-11-15 10:02:53

    @MRsandman updated answer

    aepot2021-11-15 10:31:12