ProgressBar with WPF and Ionic.zip

3

I use the Ionic.zip dll and in it unzipo and rezipo some files and folders. My manager asked me to put ProgressBar and I'm not sure how to do it using WPF. It's very fast and I do not know if it's worth it, I tried to explain it to him, but I was not convinced. So I'll have to do it.

How do I put a progress bar to indicate that some files are being zipped in my application? I use WPF. Below is the code for my method that zips files and folders.

private void CriarZip()
        {
            string path_destino = caminho_original + @"\Destino"; 
            string path_files = caminho_original + @"\Destino\Temp";           

            List<string> _filesDiretory = new List<string>();

            string nome_arquivo = nome_arquivo_zip + "_FarmExterna.zip";

            if (!nome_arquivo.Contains(".zip"))
            {
                MessageBox.Show("O nome do arquivo deve possuir a extensão .zip");
                return;
            }

            try
            {
                string[] files_new = Directory.GetFiles(path_files, "*", SearchOption.AllDirectories);
                string[] folder_new = Directory.GetDirectories(path_files, "*", SearchOption.AllDirectories);

                CriaPastaFarmInterna();
                CriaPastaFarmExterna();

                //Deleto os arquivo que não estão na Farm Externa
                foreach (var file in files_new)
                {
                    string t = string.Empty;
                    int pos = file.IndexOf(dirInicio);

                    if (pos > 0)
                    {
                        t = file.ToString().Substring(pos, file.Length - pos);

                        bool bListaArquivo = (from b in listaArquivosForaFarmExterna
                                              where b.Contains(t)
                                              select b).Count() > 0 ? true : false;
                        if (bListaArquivo)
                            File.Delete(file);
                    }
                    else
                    {
                        t = Path.GetFileName(file);
                        arquivos.Add(t);
                    }  

                }

                LimpaPastaWeb();
                DeletaPastaFarmExterna();

                //Adiciono arquivos que estão dentro da pasta base apenas
                foreach (var file in Directory.GetFiles(path_files))                    
                {
                    arquivos.Add(file);
                }

                //Aqui pego as pastas com arquivos que serão zipadas
                foreach (var file in Directory.GetDirectories(path_files))
                {
                    arquivos.Add(file);
                }

                string localNomeDestinoZIP = path_destino + "\" + nome_arquivo;

                if (arquivos.Count() > 0)
                {

                    processaDiretorio(path_files);
                    ZipUnzip.CriarArquivoZip(arquivos, localNomeDestinoZIP);
                    MessageBox.Show("Os arquivos selecionados foram compactados na pasta \n\n " +
                              localNomeDestinoZIP);
                }
                else
                    MessageBox.Show("Não há a pasta para ser compactada.");                

            }
            catch (Exception ex)
            {
                MessageBox.Show("Ocorreu um erro ao criar arquivo ZIP \n\n " + ex.Message);

            }
            finally
            {
                DeletarPastaTrabalho(path_files);
                Application.Current.Shutdown();
            }
        }

This is the routine that uses the ZipFile class to do the zip.

using (ZipFile zip = new ZipFile())
            {
                // percorre todos os arquivos da lista
                foreach (string item in arquivos)
                {
                    // se o item é um arquivo
                    if (File.Exists(item))
                    {
                        try
                        {
                            // Adiciona o arquivo na pasta raiz dentro do arquivo zip
                            zip.AddFile(item, "");
                        }
                        catch
                        {
                            throw;
                        }
                    }
                    // se o item é uma pasta
                    else if (Directory.Exists(item))
                    {
                        try
                        {
                            // Adiciona a pasta no arquivo zip com o nome da pasta 
                            zip.AddDirectory(item, new DirectoryInfo(item).Name);
                        }
                        catch
                        {
                            throw;
                        }
                    }
                }
                // Salva o arquivo zip para o destino
                try
                {
                    zip.Save(ArquivoDestino);
                }
                catch
                {
                    throw;
                }
            }
        }
    
asked by anonymous 17.03.2016 / 18:50

1 answer

3

There are classes that do not provide events or methods for us to know the progress of a task. Some alternatives may be:

Use an undetermined progress bar

Theindeterminateprogressbarisusedundertheseconditions,whereitisnotknownhowlongitwilltakeforatasktobecomplete,soitsanimationstaysuntiltheprocessisfinished.

InWPF:

<ProgressBarName="barraProgresso" Minimum="0" Maximum="10" IsIndeterminate="True"/>

In the code:

barraProgresso.IsIndeterminate = True;
// Zip / Unzip
barraProgresso.IsIndeterminate = False;

Use an alternative class

You can use another one that has properties and / or events that show progress. One I've already used is SevenZipSharp , click .

    
13.08.2016 / 17:38