Search code examples
c#.netzipstreamwriter

Value cannot be null. Parameter name: dest


I have a small app that writes some text data to zipped text file. It works fine for the first archive, but after some time I get the following Unhandled exception:

System.ArgumentNullException: Value cannot be null.
Parameter name: dest

I've added && line.Lenght >0 because I thought that it might be the problem, but nothing changed. I get 1 full archive in 3 hours and sometimes half of the second one, which is smaller, I suppose it is not finished, but is written to disk with .tmp extension. I can rename it to zip and open/unpack/read data. If I change "counter" to a smaller period e.g. 10 minutes, it writes a bunch of archives like everything is fine. Enough to make me think the bug is gone. So probably this error ecounters after some time aprox 4-5 hours. How to fix this?

Stacktrace:

[ERROR] FATAL UNHANDLED EXCEPTION: 
Unhandled Exception:
System.ArgumentNullException: Value cannot be null.
Parameter name: dest
  at System.IO.MemoryStream.set_Capacity (System.Int32 value)
  at System.IO.MemoryStream.EnsureCapacity (System.Int32 value)
  at System.IO.MemoryStream.Write (System.Byte[] buffer, System.Int32 offset, System.Int32 count)
  at SharpCompress.IO.NonDisposingStream.Write (System.Byte[] buffer, System.Int32 offset, System.Int32 count)
  at System.IO.Compression.ZipArchiveEntryStream.Write (System.Byte[] buffer, System.Int32 offset, System.Int32 count)
  at System.IO.StreamWriter.Flush (System.Boolean flushStream, System.Boolean flushEncoder)
  at System.IO.StreamWriter.Write (System.Char[] buffer, System.Int32 index, System.Int32 count)
  at System.IO.TextWriter.WriteLine (System.String value)
  at Program.DataWriter ()
  at System.Threading.ThreadHelper.ThreadStart_Context (System.Object state) 
  at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx)
  at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx)
  at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state)
  at System.Threading.ThreadHelper.ThreadStart () 

Here is the code:

static ConcurrentQueue<string> queue = new ConcurrentQueue<string>();

static void Main(string[] args)
{
    Thread thread = new Thread(DataWriter);
    thread.Start();
}

static void DataWriter()
{
    while (true)
    {
        string start = DateTime.Now.ToString("yy-MM-dd-HH-mm-ss");
        DateTime counter = DateTime.Now.AddHours(3);

        using (var archive = ZipFile.Open(HistoryPath + "history-" + start + ".tmp", ZipArchiveMode.Update))
        {
            ZipArchiveEntry data = archive.CreateEntry("data-" + start + ".txt");
            using (StreamWriter writer = new StreamWriter(data.Open()))
            {
                while (DateTime.Now < counter)
                {
                    if (queue.TryDequeue(out string line))
                    {
                        if (line != null && line.Length > 0)
                        {
                            writer.WriteLine(line);
                        }
                    }

                    System.Threading.Thread.Sleep(10);
                }
            }
        }

        File.Move(HistoryPath + "history-" + start + ".tmp", HistoryPath + "history-" + start + "_"+ DateTime.Now.ToString("yy-MM-dd-HH-mm-ss") + ".zip");
    }
}

Solution

  • Changing ZIP library solved my problem. I've changed Microsoft System.IO.Compression to ICSharpCode.SharpZipLib. The code is almost the same. Everything works as it should now. Seems there is an error in microsofts library.