0

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

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
snakepitbean
  • 217
  • 4
  • 13
  • 2
    Using it as a stream would just read it as a stream of bytes, it would have no idea it was a zip file, nor try to decompress it. Could you please provide some code? Have you tried GZipStream or DeflateStream? http://stackoverflow.com/a/4923085/1373170 – Pablo Romeo Apr 11 '13 at 04:43

3 Answers3

1
  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.
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
1

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); 


        }
    }
}
KF2
  • 9,887
  • 8
  • 44
  • 77
Nimmi
  • 680
  • 8
  • 18
  • +1 on the recommendation to use DotNetZip, pretty straigh-forward tool for Zip files management. – wacdany Apr 11 '13 at 05:59
0

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

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

Community
  • 1
  • 1
dthorpe
  • 35,318
  • 5
  • 75
  • 119