Arun Madathil M
Arun Madathil M

Reputation: 11

How to create a .tar.gz file from text string

I have a dlm file and I want to create a .tar.gz file from the content in dlm file. When I am trying to create the file, it is created but when I manually unzip that it is failed. My code is below for creating .tar.gz file, targetFileName is like C:\Folder\xxx.tar.gz:

 using (StreamWriter write = new StreamWriter(targetFileName, false, Encoding.Default))
 {
        write.Write(text.ToString());
        write.Close();
 }

In the above code text is content from dlm file. Is there anything that I am missing? please help.

Upvotes: 1

Views: 1558

Answers (2)

Abhinav Pandey
Abhinav Pandey

Reputation: 183

This is a quick example to create a .tar.gz and .gz file that will include the file that you might be creating using the stream.

Note that I'm using SharpZipLib which you can find in Nuget Package Manager for you project. Then make sure to add reference in your code:

Making tar.gz

using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Tar;
using System.IO;
using System.Text;

 static void Main(string[] args)
 {
        string text = ".Net is Awesome";
        string filename = "D:\\text.txt";
        string tarfilename = "D:\\text.tar.gz";
        using (StreamWriter write = new StreamWriter(filename, false, Encoding.Default))
        {
            //Writing a text file
            write.Write(text.ToString());
            write.Close();

            //Creating a tar.gz Stream
            Stream TarFileStream = File.Create(tarfilename);
            Stream GZStream = new GZipOutputStream(TarFileStream);
            TarArchive tarArchive = TarArchive.CreateOutputTarArchive(GZStream);
            tarArchive.RootPath = "D:/"; //Setting the Root Path for the archive

            //Creating a file entry for the tar archive
            TarEntry tarEntry = TarEntry.CreateEntryFromFile(filename);

            //Writing the entry in the archive.
            tarArchive.WriteEntry(tarEntry, false); //set false to only add the concerned file in the archive.
            tarArchive.Close();
        }
}

Making only .gz

You can create a method to make it more reusable like:

private static void MakeGz(string targetFile)
{
      string TargetGz = targetFile + ".gz";
      using (Stream GzStream = new GZipOutputStream(File.Create(TargetGz)))
      {
           using (FileStream fs = File.OpenRead(targetFile))
           {
               byte[] FileBuffer = new byte[fs.Length];
               fs.Read(FileBuffer, 0, (int)fs.Length);
               GzStream.Write(FileBuffer, 0, FileBuffer.Length);
               fs.Close();
               GzStream.Close();
            }
      }
 }

Then you can call this method whenever you are creating a file to make an archive for the same at the same time like:

 MakeGz(filename);

Upvotes: 2

Victor E.
Victor E.

Reputation: 21

try use SharpZipLib from Nuget

using System;
using System.IO;
using System.Text;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Tar;

add method:

private static void CreateTarGZ(string tgzFilename, string innerFilename, string text)
    {
        var uncompressed = Encoding.UTF8.GetBytes(text);

        using (Stream outStream = File.Create(tgzFilename))
        {
            using (GZipOutputStream gzoStream = new GZipOutputStream(outStream))
            {
                gzoStream.SetLevel(9);
                using (TarOutputStream taroStream = new TarOutputStream(gzoStream, Encoding.UTF8))
                {
                    taroStream.IsStreamOwner = false;
                    TarEntry entry = TarEntry.CreateTarEntry(innerFilename);
                    entry.Size = uncompressed.Length;
                    taroStream.PutNextEntry(entry);
                    taroStream.Write(uncompressed, 0, uncompressed.Length);
                    taroStream.CloseEntry();
                    taroStream.Close();
                }
            }
        }
    }

then call:

CreateTarGZ("test.tar.gz", "FileName.txt", "my text");
CreateTarGZ("c:\\temp\\test.tar.gz", "foo-folder\\FileName.txt", "my text");

Upvotes: 2

Related Questions