GZipStream's FileName should be set when constructing object #220

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

Originally created by @turbolocust on GitHub (Jul 15, 2017).

One issue I've found is that the FileName of GZipStream only is set after the first call of the Read() method. That should happen when constructing the object like it's implemented in the commons-compress library (written in Java) of Apache Foundation. The current behavior makes an object-oriented design more complicated.

using (var outputStream = await file.OpenStreamForWriteAsync())
 {
          var bytes = new byte[DefaultBufferSize];
          int readBytes;
          while ((readBytes = stream.Read(bytes, 0, bytes.Length)) > 0)
          {
               await outputStream.WriteAsync(bytes, 0, readBytes, Token);
          }
}

In the example code above I'd have to rename the file after the loop or read the compressed file first, create the output file and then start to decompress the compressed file. The extract of the method shown above is defined in an abstract class.

Originally created by @turbolocust on GitHub (Jul 15, 2017). One issue I've found is that the FileName of GZipStream only is set after the first call of the Read() method. That should happen when constructing the object like it's implemented in the commons-compress library (written in Java) of Apache Foundation. The current behavior makes an object-oriented design more complicated. ``` using (var outputStream = await file.OpenStreamForWriteAsync()) { var bytes = new byte[DefaultBufferSize]; int readBytes; while ((readBytes = stream.Read(bytes, 0, bytes.Length)) > 0) { await outputStream.WriteAsync(bytes, 0, readBytes, Token); } } ``` In the example code above I'd have to rename the file after the loop or read the compressed file first, create the output file and then start to decompress the compressed file. The extract of the method shown above is defined in an abstract class.
claunia added the question label 2026-01-29 22:08:35 +00:00
Author
Owner

@adamhathcock commented on GitHub (Jul 16, 2017):

Use GZipReader or GZipArchive if you want a nicer interface.

Streams are lower level and definitely logic like advancing the stream to load the file name shouldn’t happen in a constructor.

@adamhathcock commented on GitHub (Jul 16, 2017): Use GZipReader or GZipArchive if you want a nicer interface. Streams are lower level and definitely logic like advancing the stream to load the file name shouldn’t happen in a constructor.
Author
Owner

@turbolocust commented on GitHub (Jul 19, 2017):

I see. Again referring to commons-compress, the stream should be reset after reading the header. That's exactly what they do. There's an init() method in the constructor which reads the header and resets its aggregated Inflater instance (as well as the CRC) afterwards. This way one can access the metadata right after creating the stream and that is much more useful in my opinion. Either way I'll look into this...

@turbolocust commented on GitHub (Jul 19, 2017): I see. Again referring to commons-compress, the stream should be reset after reading the header. That's exactly what they do. There's an `init()` method in the constructor which reads the header and resets its aggregated Inflater instance (as well as the CRC) afterwards. This way one can access the metadata right after creating the stream and that is much more useful in my opinion. Either way I'll look into this...
Author
Owner

@adamhathcock commented on GitHub (Jul 19, 2017):

You can’t read the stream then reset position because not all streams are seekable.

Dealing with non-seekable streams is a feature of the library.

@adamhathcock commented on GitHub (Jul 19, 2017): You can’t read the stream then reset position because not all streams are seekable. Dealing with non-seekable streams is a feature of the library.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#220