I want to handle the contents of a ZIP file, that was downloaded, only into memory (without using the hard disk!). packageStream
contains the bytes of the ZIP file.
MemoryStream packageStream = // Stream of downloaded ZIP file
var zipPackage = System.IO.Packaging.Package.Open(packageStream, FileMode.Open, System.IO.FileAccess.Read);
var parts = zipPackage.GetParts();
var rels = zipPackage.GetRelationships();
parts
and rels
do not contain any items.
Can anybody tell me what I'm doing wrong?
Is a MemoryStream
the wrong stream type?
Is the Package
class the wrong class for ZIP files in this scenario?
If you can tell me any alternatives, please no third party projects.
Thanks!
UPDATE
This answer in the duplicated question solved my problem (ZipArchive class). The accepted answer uses a third party project.
How you can handle .zip files, depends on the Framework version you are using. Streams are fully interchangeable and can even encapsulate one another. The question is if you have a class that can actually take a stream and do zip Operations.
If you have at least Framework 4.5, you got the ZipArchive class and this whole thing is childsplay. The examples take a Stream and it can be any implemention of the Stream abstract class. All that memory stream is, is a readyonly stream.
If you are under that, I am pretty sure you are hosed. There simply is no class that can take a Stream and do ZIP operations on it before 4.5. Most approaches invovle saving the .zip file on the disk and decompressing it using a console programm with IO redirection. Both Windows ZIP functionaltiy and 7zip work for that, but both involve writing on the Disk.
Perhaps you can use the code part of 7-zip to do a in-process/in memory zip operation. But that might be a tricky.