snakepitbean
snakepitbean

Reputation: 227

How do you convert a physical zip file to System.IO.Stream object

How do you convert a physical zip file to System.IO.Stream object? I tried -

  1. System.IO.Stream stream = File.ReadAllBytes(Path + "\\" + "ZipFile.zip");
  2. StreamReader stream = new StreamReader(Path + "\\" + "ZipFile.zip");
  3. System.IO.Stream stream = new FileStream(Path + "\\" + "ZipFile.zip", FileMode.Open); stream.Close();

None of them seems to be working.
When trying to read the stream it either says the stream is not readable
or the file seems corrupt at the destination and cannot be unzipped

Upvotes: 0

Views: 3224

Answers (3)

Nimmi
Nimmi

Reputation: 690

Try this code.For using this code you have to download dotnetzip-ionic.dll

using Ionic.Zip;
using Ionic.Crc;
using System.IO;

protected void Page_Load(object sender, EventArgs e)
    {

using (ZipFile zip = ZipFile.Read(ZipPath))
    {

        foreach (ZipEntry entry in zip)
        {

            CrcCalculatorStream reader = entry.OpenReader();
            MemoryStream memstream = new MemoryStream();
            reader.CopyTo(memstream);
            byte[] bytes = memstream.ToArray();
            System.IO.Stream stream = new System.IO.MemoryStream(bytes); 


        }
    }
}

Upvotes: 1

dthorpe
dthorpe

Reputation: 36092

Take a look at ZipInputStream from SharpZipLib: https://github.com/icsharpcode/SharpZipLib

via https://stackoverflow.com/a/593036/301152

Upvotes: 0

Alexei Levenkov
Alexei Levenkov

Reputation: 100545

  1. No way to get it to compile (casting byte[] to Stream)... So not sure how you get runtime error here.
  2. StreamReader is not a stream - not sure what conversion you expect here.
  3. It looks like you immediately close the stream - this seem to be the only line that matches the title and would produce exact runtime error. Fix - don't close the stream till you are done reading from it.

Upvotes: 1

Related Questions