Problems Streaming files

i was reading files inside a ZipFolder

var zipFile = new ZipFile(file);
foreach (ZipEntry zipEntry in zipFile)
{
    if (!zipEntry.IsFile)
    {
        continue;  // Ignore directories
    }

    var entryFileName = zipEntry.Name.ToLower();
    var zipStream = zipFile.GetInputStream(zipEntry);

    else if(entryFileName.EndsWith(".png"))
    {
        previews.Add(entryFileName, zipStream);                    
    }
    else
    {
        documents.Add(entryFileName, zipStream);                    
    }
}

and i was planning to Save those zipstream into a new FileStream but then when i'm validating the stream

if (stream.Length == 0)
    throw new ArgumentException("stream");

using (var newFile = new FileStream(fullName, FileMode.Create))
{
    stream.CopyTo(newFile);
}  

i got the exception because stream.lengt is equal to 0

i wonder if there's a better way to do this or why this stream isn't working

Upvotes: 0

Views: 90

Answers (1)

Peter Kiss
Peter Kiss

Reputation: 9319

ZIP files not always saves their length to themself so Length property can not work. Try stream.CanRead property.

Upvotes: 2

Related Questions