Sachin Kainth
Sachin Kainth

Reputation: 46750

Extracting Zip file save to disk

I have a zip file at a URI (such as http://www.abc.com/a.zip) that I would like to open and save a file from it to disk. Is there a way in C# to open it without saving it to disk and then saving a file from it to disk?

Thanks,

Sachin

Upvotes: 0

Views: 2277

Answers (2)

The example of the extracting the zip file without saving the archive to the file system using DotNetZip.

private static void ExtractFromUrl(Uri uri, string directoryPath)
{
    using (var webClient = new WebClient())
    {
        var data = webClient.DownloadData(uri);
        using (var memoryStream = new MemoryStream(data))
        using (var zipFile = ZipFile.Read(memoryStream))
        {
            zipFile.ExtractAll(directoryPath);
        }                
    }
}

Upvotes: 3

KV Prajapati
KV Prajapati

Reputation: 94645

Use ZipFile .Net Framework 4.5 class or DotNetZip API.

ZipFile.ExtractToDirectory(zipPath, extractPath);

EDIT: You can prepare a stream or obtain byte array of URL via method of WebClient class.

string urlStr = "https://xyz.com/sample.zip";

 using (WebClient client = new WebClient())
  {
   byte []bytes=client.DownloadData(urlStr);
   using (MemoryStream ms = new MemoryStream(bytes))
   {
    using (ZipFile zip = ZipFile.Read(ms))
    {
     zip.ExtractAll(@"C:\csnet");
    }
   }
 }

Upvotes: 4

Related Questions