Tar MemoryStream only works with no compression #548

Closed
opened 2026-01-29 22:13:36 +00:00 by claunia · 3 comments
Owner

Originally created by @DR9885 on GitHub (Nov 24, 2022).

Example Test (similar to existing), only none passes

[Theory]
        [InlineData(CompressionType.None)]
        [InlineData(CompressionType.GZip)]
        [InlineData(CompressionType.LZip)]
        [InlineData(CompressionType.BZip2)]
        public void TestCompressions(CompressionType compressionType)
        {
            const string filename = "file.txt";
            const string input = "dummy filecontent";

            // Step 1: create a tar in memory
            using var stream = new MemoryStream();
            using var writer = WriterFactory.Open(stream, ArchiveType.Tar, compressionType);
            using var inputStream = new MemoryStream();
            inputStream.Write(Encoding.UTF8.GetBytes(input));
            inputStream.Position = 0;
            writer.Write(filename, inputStream, null);
            stream.Position = 0;

            // Step 2: check if the written tar file can be read correctly
            using var archive2 = TarArchive.Open(stream);
            Assert.Equal(1, archive2.Entries.Count);
            Assert.Contains(filename, archive2.Entries.Select(entry => entry.Key));

            foreach (var entry in archive2.Entries)
                Assert.Equal("dummy filecontent", new StreamReader(entry.OpenEntryStream()).ReadLine());
        }
Originally created by @DR9885 on GitHub (Nov 24, 2022). Example Test (similar to existing), only none passes ``` [Theory] [InlineData(CompressionType.None)] [InlineData(CompressionType.GZip)] [InlineData(CompressionType.LZip)] [InlineData(CompressionType.BZip2)] public void TestCompressions(CompressionType compressionType) { const string filename = "file.txt"; const string input = "dummy filecontent"; // Step 1: create a tar in memory using var stream = new MemoryStream(); using var writer = WriterFactory.Open(stream, ArchiveType.Tar, compressionType); using var inputStream = new MemoryStream(); inputStream.Write(Encoding.UTF8.GetBytes(input)); inputStream.Position = 0; writer.Write(filename, inputStream, null); stream.Position = 0; // Step 2: check if the written tar file can be read correctly using var archive2 = TarArchive.Open(stream); Assert.Equal(1, archive2.Entries.Count); Assert.Contains(filename, archive2.Entries.Select(entry => entry.Key)); foreach (var entry in archive2.Entries) Assert.Equal("dummy filecontent", new StreamReader(entry.OpenEntryStream()).ReadLine()); } ```
Author
Owner

@adamhathcock commented on GitHub (Nov 24, 2022):

stream doesn't get closed correctly. You need to explicitly dispose it in the scope to finish off compression/trailers for any archive.

@adamhathcock commented on GitHub (Nov 24, 2022): `stream` doesn't get closed correctly. You need to explicitly dispose it in the scope to finish off compression/trailers for any archive.
Author
Owner

@DR9885 commented on GitHub (May 10, 2023):

wont the "using" statement properly dispose the object?

@DR9885 commented on GitHub (May 10, 2023): wont the "using" statement properly dispose the object?
Author
Owner

@adamhathcock commented on GitHub (May 11, 2023):

wont the "using" statement properly dispose the object?

you have two scopes writing then reading....nothing gets disposed until the end of the function which is the problem.

@adamhathcock commented on GitHub (May 11, 2023): > wont the "using" statement properly dispose the object? you have two scopes writing then reading....nothing gets disposed until the end of the function which is the problem.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#548