Progressbar with WPF and Ionic.zip

Asked

Viewed 176 times

3

I use the Ionic.zip dll and on it unzipo and rezipo some files and folders. My manager asked me to put a ProgressBar and I’m doubtful how to do it using WPF. It’s very fast and I don’t know if it’s worth it, I tried to explain it to him, but I didn’t convince him. Then I’ll have to do it.

How do I place a progress bar to indicate that some files are being zipped into my application? WPF usage. Below the code of my method that zips the 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 make 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;
                }
            }
        }

1 answer

4

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 indeterminate progress bar

The undetermined progress bar is used under these conditions, where it is not known how long it will take for a task to be complete, so its animation stays until it finishes the process.

In the WPF:

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

In the code:

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

Using an alternative class

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

Browser other questions tagged

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