totoro
totoro

Reputation: 3407

DotNetZip fails with "stream does not support seek operations"

I am using DotNetZip in C# to unzip from a stream as follows:

public static void unzipFromStream(Stream stream, string outdir)
{   //omit try catch block
    using (ZipFile zip = ZipFile.Read(stream)){
        foreach (ZipEntry e in zip){
            e.Extract(outdir, ExtractExistingFileAction.OverwriteSilently);
        }
    }
}

stream is obtained using

WebClient client = new WebClient();
Stream fs = client.OpenRead(url);

However, I got the following exception

exception during extracting zip from stream System.NotSupportedException: This stream does not support seek operations.
at System.Net.ConnectStream.get_Position()
at Ionic.Zip.ZipFile.Read(Stream zipStream, TextWriter statusMessageWriter, Encoding encoding, EventHandler`1 readProgress)

On the server side(ASP.NET MVC 4), returning FilePathResult or FileStreamResult both caused this exception.

Should I obtain the stream differently on the client side? Or how to make server return a "seekable" stream? Thanks!

Upvotes: 4

Views: 2223

Answers (1)

Jim Mischel
Jim Mischel

Reputation: 134065

You'll have to download the data to a file or to memory, and then create a FileStream or a MemoryStream, or some other stream type that supports seeking. For example:

WebClient client = new WebClient();
client.DownloadFile(url, filename);
using (var fs = File.OpenRead(filename))
{
    unzipFromStream(fs, outdir);
}
File.Delete(filename);

Or, if the data will fit into memory:

byte[] data = client.DownloadData(url);
using (var fs = new MemoryStream(data))
{
    unzipFromStream(fs, outdir);
}

Upvotes: 7

Related Questions