[
  {
    "Id": "277597",
    "ThreadId": "80955",
    "Html": "<p>Is it possible to have different threads calling ZipEntry.OpenReader and reading the data without synchronization?</p>\r\n<p>My fear is that the stream for accessing the ZipFile would&nbsp;be lost with&nbsp;interleaved Seek/Read from different threads.</p>\r\n<p>My question&nbsp;is also&nbsp;about the Extract operations.</p>",
    "PostedDate": "2010-01-15T07:53:14.587-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "277953",
    "ThreadId": "80955",
    "Html": "<p>ZipFile and DotNetZip is not multi-thread safe.&nbsp; If you want to have multiple threads using a zip file, open multiple instances of ZipFile.</p>",
    "PostedDate": "2010-01-16T09:22:46.26-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "278435",
    "ThreadId": "80955",
    "Html": "<p>could there be a way to avoid reparsing the Zip and rebuilding the ZipFile object?</p>\r\n<p>The assumption would be that a file is opened read-only, so no changes could be done to it, but with a share option with other readers.</p>\r\n<p>Somting like ZipEntry.OpenReaderOnOwnStream?</p>",
    "PostedDate": "2010-01-18T02:14:15.103-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "278495",
    "ThreadId": "80955",
    "Html": "<p>What problem are we trying to solve?</p>\r\n<p>I think it's reasonable to request that DotNetZip classes be modified to be thread safe. It's reasonable to ask, but unreasonable to expect that I will do the work.&nbsp; Most use of DNZ is single-threaded, and changes to support multiple threads would be far-reaching and not performance neutral.&nbsp;&nbsp;I don't feel like there's a good payback there, so I wouldn't do that work.&nbsp; Also, the workaround is simple: open multiple instances of the ZipFile.&nbsp;</p>\r\n<p>On the other hand I'm not sure what you're asking for here.&nbsp; What problem are you trying to solve? &nbsp;</p>\r\n<p>based on my understanding, ZipEntry.OpenReaderOnOwnStream() would be a half-way step to multi-thread support.&nbsp; IT would be multi-thread support, but only for reading entries.&nbsp; The special-case nature of it seems sort of inelegant, from the start.</p>\r\n<p>Secondly, It's not so easy to build this.&nbsp; Opening a reader is simple.&nbsp;&nbsp;But once such a reading stream is dispensed, the zipfile could not be updated at all, if there were any outstanding &quot;other thread&quot; readers.&nbsp; The library would need a mechanism to release that lock.&nbsp; This makes the API considerably more complicated to support the scenario of multiple threads. The internal implementation is much more complicated. All of this is hard to get right, hard to maintain, and hard to explain to the large majority of users who just don't care about 2nd-thread readers.&nbsp;</p>\r\n<p>To me, you identified the base issue in your original question: The filestream pointer. In fact the situation with a ZipFile is exactly&nbsp;analogous to the situation with the FileStream type.&nbsp; It's a fairly simple object.&nbsp; and it is not thread safe.&nbsp; To read or write a file from multiple threads, you must open multiple FileStream instances. The same applies to a zip file.</p>\r\n<p>&nbsp;</p>",
    "PostedDate": "2010-01-18T05:38:00.427-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "278877",
    "ThreadId": "80955",
    "Html": "<p>You are true, FileStream is mostly&nbsp;unusable with multiple threads, except perhaps to log data in a trace file.<br>But the OS supplies options for sharing files, e.g. by opening a file for read only and allowing share with readers.<br>I am evaluating a scenario for extracting data from a zip file&nbsp;on the fly, when a thread need access to a given entry, without extracting all entries in advance (most entries being not accessed by the application, and each thread responsible&nbsp;of the CPU time&nbsp;for deflating). It would be like access to a database where you make read requests with where clauses, accessing only a small part of the whole data. For sure, it has to be &quot;constant data&quot; while the application is running (e.g. it would first&nbsp;download the zip in a temp file from a server).</p>\r\n<p>The ZipFile would be hidden in a specific&nbsp;class, the only allowed operations being read only operations, and&nbsp;getting a (deflated)&nbsp;Stream&nbsp;for an entry.<br>I would have to create a new FileStream for the zip file, and find a way to hack the reader from OpenReader for using the new FileStream instead of the one owned by ZipFile (and dispose the FileStream&nbsp;when the reader is close/disposed). The ZipFile would be instanciated by the new class constructor, so no multi threading problems could occur during this time.</p>\r\n<p>I am using&nbsp;files that&nbsp;have an index.xml entry; I could&nbsp;expose specific methods to access&nbsp;their content.</p>\r\n<p>&nbsp;</p>",
    "PostedDate": "2010-01-19T03:41:18.633-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "278939",
    "ThreadId": "80955",
    "Html": "<p>It sounds to me like you have a special case; you might want to hack up a special class, or a custom modification of DotNetZip&nbsp;to handle this.</p>\r\n<p>I don't feel like your case is a mainstream on, as I explained above.</p>\r\n<p>&nbsp;</p>",
    "PostedDate": "2010-01-19T06:15:45.537-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "278963",
    "ThreadId": "80955",
    "Html": "<p>I agree. But parrallelism in ExtractAll would be nice (perhaps)</p>",
    "PostedDate": "2010-01-19T07:04:57.48-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "278972",
    "ThreadId": "80955",
    "Html": "<p>I just found that in .Net 4.0, there will be a ThreadLocal&lt;T&gt; class.<br>so it will be possible to have something like a field = new ThreadLocal(()=&gt; New FileStream(filename,...));<br>and a property returning the field Value property.</p>\r\n<p>The file would be automatically reopened for each thread!</p>",
    "PostedDate": "2010-01-19T07:19:57.197-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "279004",
    "ThreadId": "80955",
    "Html": "<p>You can already do threadlocal storage in .NET.&nbsp;</p>\r\n<p><a href=\"http://msdn.microsoft.com/en-us/library/system.threading.thread.allocatenameddataslot.aspx\">http://msdn.microsoft.com/en-us/library/system.threading.thread.allocatenameddataslot.aspx</a></p>\r\n<p>The new class will make it easier, but you can already do this today.</p>",
    "PostedDate": "2010-01-19T08:10:49.137-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "279344",
    "ThreadId": "80955",
    "Html": "<p>I missed this information about the FileStream class (since version 2.0, it is not present in the 1.1&nbsp;version of this page: <a href=\"http://msdn.microsoft.com/en-us/library/system.io.filestream.aspx\">http://msdn.microsoft.com/en-us/library/system.io.filestream.aspx</a></p>\r\n<h3>Detection of stream position changes</h3>\r\n<div>\r\n<p>When a <span><span>FileStream</span></span> object does not have an exclusive hold on its handle, another thread could access the file handle concurrently and change the position of the operating system's file pointer that is associated with the file handle. In this case, the cached position in the <span><span>FileStream</span></span> object and the cached data in the buffer could be compromised. The <span><span>FileStream</span></span> object routinely performs checks on methods that access the cached buffer to assure that the operating system's handle position is the same as the cached position used by the <span><span>FileStream</span></span> object.</p>\r\n<p>If an unexpected change in the handle position is detected in a call to the <span><a id=\"ctl00_MTCS_main_ctl73_ctl00_ctl20\" href=\"http://msdn.microsoft.com/en-us/library/system.io.filestream.read.aspx\">Read</a></span> method, the .NET Framework discards the contents of the buffer and reads the stream from the file again. This can affect performance, depending on the size of the file and any other processes that could affect the position of the file stream.</p>\r\n<p>If an unexpected change in the handle position is detected in a call to the <span><a id=\"ctl00_MTCS_main_ctl73_ctl00_ctl21\" href=\"http://msdn.microsoft.com/en-us/library/system.io.filestream.write.aspx\">Write</a></span> method, the contents of the buffer are discarded and an <span><a id=\"ctl00_MTCS_main_ctl73_ctl00_ctl22\" href=\"http://msdn.microsoft.com/en-us/library/system.io.ioexception.aspx\">IOException</a></span> is thrown.</p>\r\n<p>A <span><span>FileStream</span></span> object will not have an exclusive hold on its handle when either the <span><a id=\"ctl00_MTCS_main_ctl73_ctl00_ctl23\" href=\"http://msdn.microsoft.com/en-us/library/system.io.filestream.safefilehandle.aspx\">SafeFileHandle</a></span> property is accessed to expose the handle or the <span><span>FileStream</span></span> object is given the <span><a id=\"ctl00_MTCS_main_ctl73_ctl00_ctl24\" href=\"http://msdn.microsoft.com/en-us/library/system.io.filestream.safefilehandle.aspx\">SafeFileHandle</a></span> property in its constructor.</p>\r\n<p>&nbsp;</p>\r\n<p>&nbsp;</p>\r\n</div>",
    "PostedDate": "2010-01-20T00:14:41.02-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  }
]