Progress Reporting | Background Worker | Using using | How To

Jan 13, 2013 at 2:48 AM
Edited Jan 13, 2013 at 3:01 AM

Hi,

I have spent several days trying to get this obviously excellent library working to conventions that I understand. The documentation is non-existent and this forum is obviously only visited when we have a problem getting it to work. Clearly this library took quite some time to write and just must work - hence my persistence. After much reading of comments in this forum, I came across the solution only with educated guesses. I share the results herewith.

First of all - get an updated port of this library at this github address. Compile that library and add a reference in your project to the Windows assembly. The reference adding will fix all the compile time errors - it did for me targeting .Net 4.

The following code is the guts of my Forms project. I've edited the real one quite a bit as it contains heaps for test code and trial/error coding.

setup() is called in my form initialization method. bwinstallupdates is my backgroundworker

    public string SevenZipDLLPath { get; set; }

    private void setup()
    {
      if (Environment.Is64BitOperatingSystem)
        SevenZipDLLPath = Path.Combine(Application.StartupPath, "bin", "7zx64.dll");
      else
        SevenZipDLLPath = Path.Combine(Application.StartupPath, "bin", "7zx32.dll");

      SevenZipExtractor.SetLibraryPath(SevenZipDLLPath);
     }
       
    private void button1_Click(object sender, EventArgs e)
    {   
      bwInstallUpdates.RunWorkerAsync(logger);
    }
       
    void extr_ExtractionFinished(object sender, EventArgs e)
    {
      sevenzipFinished = true;
    }

    void extr_FileExists(object sender, FileOverwriteEventArgs e)
    {

    }

    void extr_FileExtractionStarted(object sender, FileInfoEventArgs e)
    {

    }

    void extr_Extracting(object sender, ProgressEventArgs e)
    {
      bwInstallUpdates.ReportProgress(e.PercentDelta);
    }
       
       
    private bool installMC(string archiveName)
    {
      string targetDirectory = Path.Combine(tempDirectory, @"Webserver\htdocs");
      string workingDirectory = Path.Combine(tempDirectory, "Work");
      string newVersionFile = Path.Combine(Application.StartupPath, archiveName);
      string tempBackupDirectory = Path.Combine(tempDirectory, "TempBackup");
      string tempBackupFile = Path.Combine(tempBackupDirectory, "OnlineBackup.zip");

      bool success = true;

      if (File.Exists(newVersionFile))
      {
        if (Directory.Exists(workingDirectory))
          Directory.Delete(workingDirectory, true);

        Directory.CreateDirectory(workingDirectory);

        currentlyInstallingText = "Extracting archive:";
        bwInstallUpdates.ReportProgress(0);
    
        string fileName = @"C:\Trash\Test.zip";
        string directory = workingDirectory;

        sevenzipFinished = false;

        using (SevenZipExtractor extr = new SevenZipExtractor(newVersionFile, "password"))
        {
          extr.Extracting += new EventHandler<ProgressEventArgs>(extr_Extracting);
          extr.FileExtractionStarted += new EventHandler<FileInfoEventArgs>(extr_FileExtractionStarted);
          extr.FileExists += new EventHandler<FileOverwriteEventArgs>(extr_FileExists);
          extr.ExtractionFinished += new EventHandler<EventArgs>(extr_ExtractionFinished);
          extr.EventSynchronization = EventSynchronizationStrategy.AlwaysAsynchronous;
          extr.ExtractArchive(directory);
        }
        return success;
            }
        }
       
    private void bwInstallUpdates_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
      pbUpdating.Visible = true;
      pbUpdating.Increment(e.ProgressPercentage);
      pbUpdating.Refresh();
        }
       
        private void bwInstallUpdates_DoWork(object sender, DoWorkEventArgs e)
    {
      installMC("MC.zip");
    }

    private void bwInstallUpdates_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {

    }
      

I hope the above helps you.