Handle progress-bar

Dec 27, 2010 at 5:56 PM

Hey guys!

First: THANKS FOR THIS NICE LIBRARY! Really, much better than using external apps.

Second: I've read the documentation. But I don't know enough VB.net to really understand it all. I'm doing the following:

I'm creating a backup tool for a fixed folder. It don't change its location (apart from user profile folder), but the files inside it can change. And are a LOT of files, all of them really small. I've created almost everything: the forms, the controls, the savefile dialog and all. Now I want to add a progress bar that will increase after every file has been compressed, but I don't know how many files it has! And the maximum length of the progressbar will be the number of files, but I'm compressing a directory, not the files itselves.


How can I do this ? The lib can list all files and do a step everytime a file has been sucessfully added?

Thanks in advance!

Dec 29, 2010 at 1:02 AM

Hm... no one knows how can I handle the progress bar.. ?

The example code seems very hard to understand to me, I didn't got it. I think that a variable that recieves how many files the folder has can do it easily, but I don't know how to set up this variable. Someone can help me?

Coordinator
Jan 6, 2011 at 7:34 PM

Show some of your code, the code that uses the DotNetZip library, and I'll be able to make better suggestions.

Yes, to show a progress bar for zipping up a large set of files, you need to know the number of files being zipped.

In DotNetZip, normally you would call a method like ZipFile.AddDirectory().  This adds entries for all the files in the specified directory into the ZipFile object.  How many files is that?  Easy: reference ZipFile.Entries.Count().  This is the number of entries that will be saved when you call ZipFile.Save().

Your progress bar logic should be attached to the SaveProgress event generated by DotNetZip.  You can attach that logic after you call AddDirectory(). 

Does this help?

Good luck.

 

Jan 7, 2011 at 3:58 AM

Helped a bit, but I don't know how to make it increase :P

My code is here:

 

Imports System.IO
Imports Ionic.Zip

Public Class Main

    'Definie as vari�veis
    Dim NomedoBackup As String
    Dim Pasta As String
    Dim NomeAntigo As String
    Dim NomePadrao As String = "c:\profile.zip"

    'Captura a pasta do perfil do usu�rio
    Dim Perfil As String = Environment.GetEnvironmentVariable("UserProfile")

    'Captura a localiza��o da pasta 'meus documentos/documentos'
    Dim Documentos As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)


    Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Define a pasta a que ser� comprimida
        Pasta = Perfil & "\AppData\Local\Criterion Games"

    End Sub

    'Janela procurar arquivo
    Private Sub Procurar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Procurar.Click


        'Cria a janela de procurar arquivo
        Dim Salvar As New SaveFileDialog()

        'Pasta inicial
        Salvar.InitialDirectory = Documentos
        Salvar.Filter = "Zip Files (*.zip)|*.zip"

        'Detecta quando o OK � pressionado
        If Salvar.ShowDialog() = DialogResult.OK Then

            'Define o nome do arquivo
            NomedoBackup = Salvar.FileName
            Caminho.Text = NomedoBackup
            NomeAntigo = Salvar.FileName

        End If

    End Sub

    'Bot�o de backup
    Private Sub Backup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Backup.Click

        'Garante que n�o ir� gerar erro verificando se a vari�vel est� vazia
        If NomedoBackup = "" Then
            'Define um nome padr�o
            NomedoBackup = NomePadrao

            Using zip As ZipFile = New ZipFile
                'Define a compress�o para a melhor poss�vel
                zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression
                'Adiciona a pasta previamente definida
                zip.AddDirectory(Pasta)
                'Salva o arquivo
                zip.Save(NomedoBackup)
            End Using

            'Mensagem alertando que nenhum nome foi definido
            MsgBox("You didn't defined a name for the backup. By default, it has been saved to: " & NomePadrao, MsgBoxStyle.Information, "Warning!")

        Else
            Using zip As ZipFile = New ZipFile
                'Define a compress�o para a melhor poss�vel
                zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression
                'Adiciona a pasta previamente definida
                zip.AddDirectory(Pasta)
                'Salva o arquivo
                zip.Save(NomedoBackup)
            End Using
        End If
    End Sub


End Class


It doesn't refer to any progressbar because I removed it since I didn't knew how to work with it :P I have another question... how can I embed the DLL into my app and use it from there? I've searched a bit, but none seems to be clear :)

And thanks for the response. I was almost losing my faith that someone would reply!

Coordinator
Jan 7, 2011 at 1:23 PM

Regarding embedding assemblies, see this question and answer: http://stackoverflow.com/questions/222655/embedding-assemblies-inside-another-assembly/625115

Regarding the progressbar, you need to use a SaveProgress event. 

See this documentation: http://cheeso.members.winisp.net/DotNetZipHelp/html/2b8624b0-6823-7e33-a864-2c7bbd9835e7.htm

There are also examples in the DotNetZip source distribution. 

You can also look in the example code here: http://code.msdn.microsoft.com/DotNetZip/Wiki/View.aspx?title=VB%20WinForms%20Example

Good luck.

Jan 8, 2011 at 1:34 AM

Got it working! Thanks!

I'll only now rename all variables to names that make sense to me, so it will become even easier to understand (:

Coordinator
Jan 12, 2011 at 2:09 AM

Glad to hear you got it working.