Question How can I get a valid stream from a zip file which contains xml?

Johnnnnnnnnnnnnny

New member
Joined
Nov 3, 2022
Messages
4
Programming Experience
Beginner
Hi Gents,

I have a specific requirement is to get a valid stream(which will be further operate) from given zip path?

The situation is use a support code snippet to determine given path is xml/zip file(one of these files inside zip is xml which what I want), if it's a xml then easily get a FileStream otherwise if it's a zip, then get Stream from zip(FileStream would be perfect but I've been tring FileStream but no solution).

To be more clear to the requirement, I came up with a current code to better explain what my point is:

example:
// I use hardcoding input path as an example here
public string iptPath = "D:\CSharp\compressFile\exampleZip.zip";
private FileStream stream;
// Check if given path a zip
if(Path.GetExtension(iptPath).EndsWith(".zip"))
    {
        if (iptPath is not null)
        {
            using var fileZipArchive = ZipFile.OpenRead(iptPath);
            // Here I know there will be only one xml file inside zip.
            var entry = fileZipArchive.Entries.Where(e => e.Name.EndsWith(".xml")).FirstOrDefault();
            stream = entry.Open() as FileStream;
            // Or I actually still get nothing if I typed code below
           // var streamZip = entry.Open();
        }
    }
    else
    {
        // This is goping to be purely xml file, as an example it should be "D:\CSharp\compressFile\exampleXml.xml";
        stream = File.OpenRead(InputFile!);
    }

I use stream = entry.Open(); but I can only get a DeflateStream which I can't use it further out of if{} statement.

Now All I want is how to change this code to get a valid(perfectly FileStream) Stream which contains the data in xml file that I can technically use it from there.


Regards,
Johnny
 
Last edited:
You can't get a FileStream on anything other than a file. An entry in a ZIP archive is not a file so you cannot get a FileStream on it. Given that all types that inherit Stream have basically the same functionality, you can generally do what you need to do with any type of stream. If you really do need a FileStream then you're going to have to extract the entry from the archive and save it to a file, then create a FileStream on that.
 
You can't get a FileStream on anything other than a file. An entry in a ZIP archive is not a file so you cannot get a FileStream on it. Given that all types that inherit Stream have basically the same functionality, you can generally do what you need to do with any type of stream. If you really do need a FileStream then you're going to have to extract the entry from the archive and save it to a file, then create a FileStream on that.
Thank you for replying so fast.
I'm wondering is there anyway I can read the xml data from Stream? I mean decompress it on Stream and read it directly from stream, then write it into another xml file which will be saved locally

Thanks,
Johnny
 
Yes you can. If you can read from a FileStream, you can read from a Stream. You can also create on output FileStream and use CopyTo() copy from the zip file string to a the destination file stream. The question though is why would you want to decompress and write into another file? Just read the stream regardless if the stream was an an uncompressed XML file, or if it the stream was decompressed as it was being read out of the zip file.
 
Yes you can. If you can read from a FileStream, you can read from a Stream. You can also create on output FileStream and use CopyTo() copy from the zip file string to a the destination file stream. The question though is why would you want to decompress and write into another file? Just read the stream regardless if the stream was an an uncompressed XML file, or if it the stream was decompressed as it was being read out of the zip file.
Thanks Skydiver,

I think I may know what you meant. But the thing is I couldn't get the stream which contains data from a xml which inside zip file.

The reason about why I want to decompress in stream and write it into wnother is because the rest of this function is to anonimize all data inside this xml, so I have to read it in Stream and operate it, then write it into another file.

So if I read the stream regardless then I get nothing no matter
var stream = File.OpenRead(iptPath);
OR
var stream = entry.Open();

All I get now is a DeflatStream and nothing inside, but when given path is a xml file, I use File.OpenRead(iptPath) I can get rich data like CanRead = true / CanWrite = true / byte = 10,000,000 etc.

Hope this make sense.

Cheers,
Johnny
 
What happened when you tried to read data from that DeflateStream? What actual problem are you trying to solve here?
Hi Jmcilhinney,

Eventually, I want to get data as a XmlReader type so that I can deal with the data from xml which inside a zip file and not decompress locally but on stream.

Thanks,
Johnny
 
The XmlReader.Create method has multiple overloads that will take a Stream as an argument. That means any type of stream (FileStream, MemoryStream, NetworkStream, DeflateStream, etc.). Like I said, you can generally do most of the same things with any type of stream. Just use the DeflateStream that you already have.
 
Back
Top Bottom