icsharpcode / SharpZipLib

#ziplib is a Zip, GZip, Tar and BZip2 library written entirely in C# for the .NET platform.
http://icsharpcode.github.io/SharpZipLib/
MIT License
3.73k stars 976 forks source link

TAR archive has only 20kb when application high load #847

Open bail16 opened 1 year ago

bail16 commented 1 year ago

Describe the bug

I have .net framework 4.8 application. I'm building TAR archive from multiple memory streams, everything goes ok when application has low load. After executing performance tests, TAR building crashes. Every package after high load has only 20kb.

Reproduction Code

    public class TarArchiveWriter : IArchiveWriter
    {
        private readonly MemoryStream _memoryStream;
        private readonly TarOutputStream _tarOutputStream;
        private readonly List<IDisposable> _toDispose = new List<IDisposable>();
        public string Extension => "tar";

        public TarArchiveWriter()
        {
            _memoryStream = new MemoryStream();
            _tarOutputStream = new TarOutputStream(_memoryStream, Encoding.UTF8);
        }

        public void Write(string path, string xml)
        {
            var bytes = Encoding.UTF8.GetBytes(xml);
            Write(path, bytes);
        }

        public void Write(string path, byte[] content)
        {
            var entry = TarEntry.CreateTarEntry(path);
            entry.Size = content.Length;
            _tarOutputStream.PutNextEntry(entry);
            var binaryWriter = new BinaryWriter(_tarOutputStream);
            _toDispose.Add(binaryWriter);
            binaryWriter.Write(content);
            binaryWriter.Flush();
            _tarOutputStream.CloseEntry();
        }

        public void Write(string path)
        {
            var entry = TarEntry.CreateTarEntry(path);
            _tarOutputStream.PutNextEntry(entry);
        }

        public byte[] GetResult()
        {
            _tarOutputStream.Close();
            return _memoryStream?.ToArray();
        }

        public void Dispose()
        {
            foreach (var disposable in _toDispose)
            {
                disposable?.Dispose();
            }
            _tarOutputStream?.Dispose();
            _memoryStream?.Dispose();        
            _toDispose.Clear();
        }
    }

Steps to reproduce

Expected behavior

Package should be build properly

Operating System

Windows

Framework Version

.NET Framework 4.x

Tags

Tar

Additional context

No response