DeflateStream.Length throws NotSupportedException #255

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

Originally created by @evil-shrike on GitHub (Oct 26, 2017).

Is it by design?

System.NotSupportedException: Specified method is not supported.
    at SharpCompress.Compressors.Deflate.DeflateStream.get_Length()

Why DeflateStream can't return its Length?

This DeflateStream is actually picked up from zipArchive.Entries by name:

  ZipArchiveEntry contentEntry = _zipArchive.Entries.FirstOrDefault(e => e.Key == itemName);
  if (contentEntry != null)
  {
    return contentEntry.OpenEntryStream();
  }

That contentEntry has Size property. But stream returned by OpenEntryStream (DeflateStream) throws on reading its Length property.
I can guess while it's happening (probably because ZipArchiveEntry.Size is extracted from metadata) but it's inconvenient. Because it means I can't return Stream in my API (not bound to the lib) - as it can throws.

I'd suggest to make that Stream make safe and not to throw in Length.

Originally created by @evil-shrike on GitHub (Oct 26, 2017). Is it by design? ``` System.NotSupportedException: Specified method is not supported. at SharpCompress.Compressors.Deflate.DeflateStream.get_Length() ``` Why DeflateStream can't return its Length? This DeflateStream is actually picked up from zipArchive.Entries by name: ``` ZipArchiveEntry contentEntry = _zipArchive.Entries.FirstOrDefault(e => e.Key == itemName); if (contentEntry != null) { return contentEntry.OpenEntryStream(); } ``` That `contentEntry` has Size property. But stream returned by `OpenEntryStream` (DeflateStream) throws on reading its `Length` property. I can guess while it's happening (probably because ZipArchiveEntry.Size is extracted from metadata) but it's inconvenient. Because it means I can't return Stream in my API (not bound to the lib) - as it can throws. I'd suggest to make that Stream make safe and not to throw in `Length`.
Author
Owner

@adamhathcock commented on GitHub (Oct 26, 2017):

That is by design. Most streams aren't seekable in this API so you can't just do Stream.Length. This is because very large archives or archives based on network streams are a primary use-case.

@adamhathcock commented on GitHub (Oct 26, 2017): That is by design. Most streams aren't seekable in this API so you can't just do Stream.Length. This is because very large archives or archives based on network streams are a primary use-case.
Author
Owner

@evil-shrike commented on GitHub (Oct 26, 2017):

Ok, in general it makes sense (one should check CanSeek before calling Stream.Length), but in this case Size is known...

@evil-shrike commented on GitHub (Oct 26, 2017): Ok, in general it makes sense (one should check CanSeek before calling `Stream.Length`), but in this case Size is known...
Author
Owner

@adamhathcock commented on GitHub (Oct 26, 2017):

The size isn't always known. The source of the data could be a stream that isn't seekable. Sometimes, size isn't known on a zip archive if it was created in a streaming manner.

There could be work done to know the stream size in some cases but I haven't put the work in to do that. Just don't do Stream.Length. Why do you need that?

@adamhathcock commented on GitHub (Oct 26, 2017): The size isn't always known. The source of the data could be a stream that isn't seekable. Sometimes, size isn't known on a zip archive if it was created in a streaming manner. There could be work done to know the stream size in some cases but I haven't put the work in to do that. Just don't do Stream.Length. Why do you need that?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#255