Build archive in memorystream while keeping it open #133

Closed
opened 2026-01-29 22:07:09 +00:00 by claunia · 6 comments
Owner

Originally created by @zaryk on GitHub (Sep 27, 2016).

Is there a way to build archive in memorystream while keeping it open? The below way seems to create it, but once returned, it is not open. I also looked at the ZipArchive constructor where there is an option to keep it open, but passing in the stream said that it could not find zip headers. I just need a regular zip. I know the supported formats for streams are the below, so does that mean this will not work?

BZip2Stream Both
GZipStream Both
DeflateStream Both
LZMAStream Both
PPMdStream Both

internal virtual MemoryStream InternalDownloadStream(int offset, int readSize) { var archive = ZipArchive.Create(); archive.AddAllFromDirectory(directoryInfo.FullName, "*.*", SearchOption.AllDirectories); var stream = new MemoryStream(); if (offset == 0 && readSize == 0) { archive.SaveTo(stream, new CompressionInfo() { DeflateCompressionLevel = Core.SharpCompress.Compressor.Deflate.CompressionLevel.Default, Type = CompressionType.None }); return stream; } else { throw new NotImplementedException(); } }

Originally created by @zaryk on GitHub (Sep 27, 2016). Is there a way to build archive in memorystream while keeping it open? The below way seems to create it, but once returned, it is not open. I also looked at the ZipArchive constructor where there is an option to keep it open, but passing in the stream said that it could not find zip headers. I just need a regular zip. I know the supported formats for streams are the below, so does that mean this will not work? BZip2Stream Both GZipStream Both DeflateStream Both LZMAStream Both PPMdStream Both ` internal virtual MemoryStream InternalDownloadStream(int offset, int readSize) { var archive = ZipArchive.Create(); archive.AddAllFromDirectory(directoryInfo.FullName, "*.*", SearchOption.AllDirectories); var stream = new MemoryStream(); if (offset == 0 && readSize == 0) { archive.SaveTo(stream, new CompressionInfo() { DeflateCompressionLevel = Core.SharpCompress.Compressor.Deflate.CompressionLevel.Default, Type = CompressionType.None }); return stream; } else { throw new NotImplementedException(); } } `
claunia added the question label 2026-01-29 22:07:09 +00:00
Author
Owner

@adamhathcock commented on GitHub (Sep 28, 2016):

You mean you want an option on the 'SaveTo' method on WritableArchives to leave the stream open?

@adamhathcock commented on GitHub (Sep 28, 2016): You mean you want an option on the 'SaveTo' method on WritableArchives to leave the stream open?
Author
Owner

@adamhathcock commented on GitHub (Sep 28, 2016):

Also, you need to reset the MemoryStream position back to 0 to make it usable too.

@adamhathcock commented on GitHub (Sep 28, 2016): Also, you need to reset the `MemoryStream` position back to 0 to make it usable too.
Author
Owner

@zaryk commented on GitHub (Sep 28, 2016):

  • You mean you want an option on the 'SaveTo' method on WritableArchives to leave the stream open?

I suppose. It doesn't really matter where.

  • Also, you need to reset the MemoryStream position back to 0 to make it usable too.

I am sure I tried that as well, but maybe not. Will get back to you. Setting position immediately after SaveTo, before return, I get the same thing, "cannot access a closed stream".

@zaryk commented on GitHub (Sep 28, 2016): - You mean you want an option on the 'SaveTo' method on WritableArchives to leave the stream open? I suppose. It doesn't really matter where. - Also, you need to reset the MemoryStream position back to 0 to make it usable too. I am sure I tried that as well, but maybe not. Will get back to you. Setting position immediately after SaveTo, before return, I get the same thing, "cannot access a closed stream".
Author
Owner

@adamhathcock commented on GitHub (Sep 29, 2016):

I thought the options got passed down. Anyway, I've just refactored the way options work for the next release. Maybe it will be more clear.

@adamhathcock commented on GitHub (Sep 29, 2016): I thought the options got passed down. Anyway, I've just refactored the way options work for the next release. Maybe it will be more clear.
Author
Owner
@adamhathcock commented on GitHub (Sep 29, 2016): https://github.com/adamhathcock/sharpcompress/blob/master/USAGE.md#create-zip-archive-from-all-files-in-a-directory-and-save-in-memory
Author
Owner

@zaryk commented on GitHub (Sep 29, 2016):

cool thanks! I will check it out.

@zaryk commented on GitHub (Sep 29, 2016): cool thanks! I will check it out.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#133