Read contents of ZIP file without writing to disk

Asked

Viewed 95 times

-1

In the program I am writing I download a ZIP file over the internet and then need to upload the files extracted from ZIP to another place.

Turns out I can’t find a way to read the contents of that ZIP file - now a Stream - without first creating a Filestream and saving it to disk. I would prefer a thousand times to work everything in memory. Today I do so (with System.IO):

string zipLocation = Directory.GetCurrentDirectory() + "\\teste.zip";

//faz o download e grava no disco
using (GetObjectResponse response = await S3Client.GetObjectAsync(req))
using (Stream responseStream = response.ResponseStream)
using (FileStream fileStream = File.Create(zipLocation))
{
    responseStream.CopyTo(fileStream);
    fileStream.Seek(0, SeekOrigin.Begin);

    using (var zip = new ZipArchive(file, ZipArchiveMode.Read))
    {
        //loop pelos arquivos dentro do ZIP
        foreach (ZipArchiveEntry entry in zip.Entries)
        {
            //faz o upload
        }
    }
}

As you can see, I had to write the file to disk using File.Create(). How to do without this part?

2 answers

2

Have you seen the Dotnetzip.

using (ZipFile zip = ZipFile.Read("omeuzip"))
{
  ZipEntry FicheiroZip = zip["documento.txt"];
  FicheiroZip.Extract(OutputStream);
}

0

Look, I’m sorry I even opened that question. Working a little bit more on my code I managed to use Memorystream, yes. Before I thought it was not possible. I tried to delete the question and I did not give more.

...
using (GetObjectResponse response = await S3Client.GetObjectAsync(req))
using (Stream responseStream = response.ResponseStream)
using (MemoryStream memoryStream = new MemoryStream()) //funciona perfeitamente

Browser other questions tagged

You are not signed in. Login or sign up in order to post.