0

anybody know how could i do that? i tried remake this code but it does not work. Unpack a zip using ZipInputStream (eg for Unseekable input streams)

patrycze
  • 29
  • 7

1 Answers1

1

You can do it without external dependency. Add System.IO.Compression.dll and use it like this

using (var client = new System.Net.Http.HttpClient())
using (var stream = client.GetStreamAsync("https://github.com/frictionlessdata/specs/archive/master.zip").Result)
{
    var basepath = Path.Combine(Path.GetTempPath() + "myzip");
    System.IO.Directory.CreateDirectory(basepath);

    var ar = new System.IO.Compression.ZipArchive(stream, System.IO.Compression.ZipArchiveMode.Read);
    foreach (var entry in ar.Entries)
    {
        var path = Path.Combine(basepath, entry.FullName);

        if (string.IsNullOrEmpty(entry.Name))
        {
            System.IO.Directory.CreateDirectory(Path.GetDirectoryName(path));
            continue;
        }

        using (var entryStream = entry.Open())
        {
            System.IO.Directory.CreateDirectory(Path.GetDirectoryName(path));
            using (var file = File.Create(path))
            {
                entryStream.CopyTo(file);
            }
        }
    }
}

You can replace HttpClient by WebClient or HttpRequest if you want.

If you want to extract only one file, replace foreach (var entry in ar.Entries) by :

var entry = ar.Entries.FirstOrDefault(e => e.FullName.EndWith("myFile.txt"));
if(entry == null)
    return;
Kalten
  • 4,092
  • 23
  • 32
  • thanks for answer but if i want extract and download single file from this repository? this is what I mean – patrycze Mar 30 '17 at 10:48
  • You can't extract a single file without download the entire zip file. Instead of iterate through all zip file entries, just select the entry you are looking for (ex : with linq). I will update my answer. – Kalten Mar 30 '17 at 11:58