how to extract zip files in c# to a folder where system is installed

Asked

Viewed 2,473 times

1

How to Extract Zip Files in to a folder where the system is installed?

using System;
using System.IO.Compression;
using System.Windows;
using System.Xml;

namespace TestXml
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string Dir2 = @"c:\IASD\Cantina Escolar\";
            XmlDocument doc = new XmlDocument();
            doc.Load("http://www.meusite.com/arquivoXML.xml");

            XmlNode node = doc.DocumentElement.SelectSingleNode("/Application/Version");
            XmlNode node1 = doc.DocumentElement.SelectSingleNode("/Application/ZipFile");
            string version = node.InnerText;
            string zipfile = node1.InnerText;

            string End = "http://www.meusite.com/";

            string Arq = version;
            string file = zipfile;

            string Arquivo = String.Concat(End, zipfile);
            string Arquivo2 = String.Concat(@"c:\IASD\Cantina Escolar\",zipfile); 

            WebClient webClient = new WebClient();
            webClient.DownloadFile(Arquivo, @"C:\IASD\Cantina Escolar\"+zipfile);

            ZipFile.ExtractToDirectory(Arquivo2, zipfile);
        }
    }
}

The error message displayed is:

URI formats are not supported.

So to summarize:

I go to the server and put a zip, which is an update to be downloaded. I manually change the XML file by placing the version of the file, which is nothing more than the name of the file to be unzipped. The method reads XML and has to download and unzip the zip file in the system installation folder.

Could you help me?

  • 2

    You are making some confusion, as far as I understand about your problem is that you want to unzip a file that will be available in an online url. It turns out that you need to first download that file to your local disk, only then extract that file.

  • I download it only that it does not go to the folder where I determined it to be. It is going to the folder bin Debug. am downloading with Webclient Webclient = new Webclient(); Webclient.Downloadfile(File,@"c: Directory folder is creating a folder with the file name and not unzipping the file directly in the directory I determined.

  • Paste your complete code by downloading the file.

  • I have no punctuation to answer the question itself and here it is unviable.

  • I’ll have to make another answer.

2 answers

1

This is because you are waiting for the unzipping function to be able to download your zip file, and that is not how it works. First you need to download the file, save it somewhere and then open it and unzip it.

The following code should work:

using System;
using System.IO.Compression;
using System.Windows;
using System.Xml;

namespace TestXml
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var dir = "c:\\pasta\\Diretório_onde_descompactar";

            //abrindo e lendo um arquivo xml para encontrar a versão que está disponível
            XmlDocument doc = new XmlDocument();
            doc.Load("http://www.meusite.com/pasta/arquivoXML.xml");
            XmlNode node = doc.DocumentElement.SelectSingleNode("/Application/Version");
            var version = node.InnerText;

            //Aqui eu pego o endereço onde é para descompactar
            // e informo para o ZipFile.ExtractTodirectory
            //passando a concatenação como parâmentro
            var url = "http://meusite.com/pasta/";

            var arq = version;

            var urlArquivo = String.Concat(url, arq, ".zip");

            // Download
            var webRequest = (HttpWebRequest)WebRequest.Create(urlArquivo);            
            var response = webRequest.GetResponse() as HttpWebResponse;
            var stream = response.GetResponseStream();

            using (ZipInputStream zipStream = new ZipInputStream(stream))
            {
                ZipEntry currentEntry;
                while ((currentEntry = zipStream.GetNextEntry()) != null)
                {
                    currentEntry.Extract(dir, ExtractExistingFileAction.OverwriteSilently);
                }
            }
        }
    }
}
  • I download it only that it does not go to the folder where I determined it to be. It is going to the folder bin Debug. am downloading with Webclient Webclient = new Webclient(); Webclient.Downloadfile(File,@"c: Directory folder.

  • You can update your code on the question so I can see exactly what is running now?

  • Updated code

1

After the code change, the problem that occurs is that the use of DownloadFile is incorrect. String.Concat does not map the directory to the machine: just assemble a String with a directory name.

The correct in this case is to use Path.Combine().

The final code stays like this:

using System;
using System.IO.Compression;
using System.IO;
using System.Windows;
using System.Xml;

namespace TestXml
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string Dir2 = @"c:\IASD\Cantina Escolar\";
            XmlDocument doc = new XmlDocument();
            doc.Load("http://www.meusite.com/arquivoXML.xml");

            XmlNode node = doc.DocumentElement.SelectSingleNode("/Application/Version");
            XmlNode node1 = doc.DocumentElement.SelectSingleNode("/Application/ZipFile");
            string version = node.InnerText;
            string zipfile = node1.InnerText;

            string End = "http://www.meusite.com/";

            string Arq = version;
            string file = zipfile;

            string Arquivo = String.Concat(End, zipfile);
            string destino = Path.Combine(@"c:\IASD\Cantina Escolar\" + zipfile);

            WebClient webClient = new WebClient();
            webClient.DownloadFile(Arquivo, destino);

            ZipFile.ExtractToDirectory(@"c:\IASD\Cantina Escolar\", destino);
        }
    }
}
  • When I use Server.Mappath I need to import something else because it is giving the error: The name 'Server' does not exist in the Current context

  • Your application is not Web, right? I will update the answer.

  • With Path.Combine the same problem occurred. It downloaded normally but unzipped into Debug bin and created a directory with the same zip file name.

  • An issue for those who are working with system updates of this type is when the code that is responsible for the update is within the system itself and it is not possible to replace the files that are running and cannot run the methods that do the update without the application be started. Is there any update routine I can apply in this case that would solve this problem? I appreciate the help.

Browser other questions tagged

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