[Help] [Question] How to create a .tar.gz file? #584

Open
opened 2026-01-29 22:14:09 +00:00 by claunia · 6 comments
Owner

Originally created by @SuperJMN on GitHub (Sep 12, 2023).

Hi!

I want to create a tar.gz, but seeing the API I can't figure out how.

Thanks :)

BTW, I'm looking to create .deb packages from .NET (without external tools) and I saw that deb files are just tar.gz archives. That's ultimately what I want to do.

Originally created by @SuperJMN on GitHub (Sep 12, 2023). Hi! I want to create a tar.gz, but seeing the API I can't figure out how. Thanks :) BTW, I'm looking to create .deb packages from .NET (without external tools) and I saw that deb files are just tar.gz archives. That's ultimately what I want to do.
claunia added the questionup for grabs labels 2026-01-29 22:14:09 +00:00
Author
Owner

@btomblinson commented on GitHub (Sep 18, 2023):

Check GZipWriterTests, that has an example of writing a tar to a .gz, I don’t think there are any examples of creating the .tar first though

@btomblinson commented on GitHub (Sep 18, 2023): Check GZipWriterTests, that has an example of writing a tar to a .gz, I don’t think there are any examples of creating the .tar first though
Author
Owner

@adamhathcock commented on GitHub (Sep 18, 2023):

it should be just matter of selecting compression type with the TarWriter or TarArchive https://github.com/adamhathcock/sharpcompress/blob/master/tests/SharpCompress.Test/Tar/TarWriterTests.cs

@adamhathcock commented on GitHub (Sep 18, 2023): it should be just matter of selecting compression type with the TarWriter or TarArchive https://github.com/adamhathcock/sharpcompress/blob/master/tests/SharpCompress.Test/Tar/TarWriterTests.cs
Author
Owner

@SuperJMN commented on GitHub (Oct 23, 2023):

it should be just matter of selecting compression type with the TarWriter or TarArchive https://github.com/adamhathcock/sharpcompress/blob/master/tests/SharpCompress.Test/Tar/TarWriterTests.cs

I've tried with this, but I didn't get the desired results.

 [Fact]
 public void Test1()
 {
     var tarfile = SharpCompress.Archives.Tar.TarArchive.Create();
     using (var memoryStream = new MemoryStream("Some content"u8.ToArray()))
     {
         var entry = tarfile.AddEntry("something.txt", memoryStream);
         using (FileStream fileStream = File.OpenWrite("Myfile.tar.gz"))
         {
             tarfile.SaveTo(fileStream, new TarWriterOptions(CompressionType.GZip, true));
         }
     }
 }
  1. Since I'm making a tar.gz for a Debian package, I would like the unique entry of the tar.gz file to be one named "." (dot). I don't know how to do that :(
  2. The entry I've created is empty.

I hope anyone can shed a big of light. Thanks a lot!

@SuperJMN commented on GitHub (Oct 23, 2023): > it should be just matter of selecting compression type with the TarWriter or TarArchive https://github.com/adamhathcock/sharpcompress/blob/master/tests/SharpCompress.Test/Tar/TarWriterTests.cs I've tried with this, but I didn't get the desired results. ```csharp [Fact] public void Test1() { var tarfile = SharpCompress.Archives.Tar.TarArchive.Create(); using (var memoryStream = new MemoryStream("Some content"u8.ToArray())) { var entry = tarfile.AddEntry("something.txt", memoryStream); using (FileStream fileStream = File.OpenWrite("Myfile.tar.gz")) { tarfile.SaveTo(fileStream, new TarWriterOptions(CompressionType.GZip, true)); } } } ``` 1. Since I'm making a tar.gz for a Debian package, I would like the unique entry of the tar.gz file to be one named "." (dot). I don't know how to do that :( 2. The entry I've created is empty. I hope anyone can shed a big of light. Thanks a lot!
Author
Owner

@adamhathcock commented on GitHub (Oct 24, 2023):

make sure you're disposing everything....that Test1 isn't

@adamhathcock commented on GitHub (Oct 24, 2023): make sure you're disposing everything....that Test1 isn't
Author
Owner

@SuperJMN commented on GitHub (Oct 25, 2023):

OK, the problem doesn't seem to be related with the disposal, but with the way I added the entry:

	tarfile.AddEntry("something.txt", memoryStream);

as opposed to

	tarfile.AddEntry("something.txt", memoryStream, memoryStream.Length);

Now it works.

@SuperJMN commented on GitHub (Oct 25, 2023): OK, the problem doesn't seem to be related with the disposal, but with the way I added the entry: ``` tarfile.AddEntry("something.txt", memoryStream); ``` as opposed to ``` tarfile.AddEntry("something.txt", memoryStream, memoryStream.Length); ``` Now it works.
Author
Owner

@DannyBoyk commented on GitHub (Jan 26, 2024):

If you are working with a set of files in a directory, we found this works really well:

public static void TarGzDirectory(string inputDirectory, string outputFilePath)
{
    var writerOptions = new WriterOptions(SharpCompress.Common.CompressionType.GZip)
    {
        LeaveStreamOpen = true,
    };

    using (var stream = File.OpenWrite(outputFilePath))
    using (var writer = WriterFactory.Open(stream, SharpCompress.Common.ArchiveType.Tar, writerOptions))
    {
        writer.WriteAll(inputDirectory, "*", SearchOption.AllDirectories);
    }
}
@DannyBoyk commented on GitHub (Jan 26, 2024): If you are working with a set of files in a directory, we found this works really well: ``` public static void TarGzDirectory(string inputDirectory, string outputFilePath) { var writerOptions = new WriterOptions(SharpCompress.Common.CompressionType.GZip) { LeaveStreamOpen = true, }; using (var stream = File.OpenWrite(outputFilePath)) using (var writer = WriterFactory.Open(stream, SharpCompress.Common.ArchiveType.Tar, writerOptions)) { writer.WriteAll(inputDirectory, "*", SearchOption.AllDirectories); } } ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#584