C++/CLI and "using" keyword and how to close a zip file

Sep 2, 2009 at 6:44 AM

I'm using C++/CLI with DotNetZip and can't figure out how to close a zip file once I'm done with it.  All the examples use the using keyword, as in

using (ZipFile zip = new ZipFile("foo.zip"))

{

//... etc.

}

This keyword is not supported in C++/CLI, however, and I can't call the Dispose or Finalize methods since they're protected.  Is there a way I can cleanly close the file?

Coordinator
Sep 2, 2009 at 4:17 PM
Edited Sep 2, 2009 at 4:18 PM

The no-argument Dispose() method is public.

"Using" in C# is a shorthand for try....finally { object.Dispose(); }     For example,

using (var zip = ZipFile.Read("foo.zip")) 
{
  // etc
}

is equivalent to

var zip = ZipFile.Read("foo.zip");
try
{
 // etc
}
finally
{
  zip.Dispose();
}

 

Sep 2, 2009 at 6:30 PM

The Dispose() method is protected/private in the installation I downloaded.  It turns out that this is the equivalent: 

    ZipFile ^ zip;
    try
    {

       zip = gcnew ZipFile("test.zip");
     //...


    }
    finally
    {
        zip->~ZipFile();
    }

Coordinator
Sep 2, 2009 at 8:30 PM

Hmm, right.  That must be some sort of impedance mismatch between the languages.  Here's a DotNetZip "hello world" in C++/CLI:

// DotNetZip-HelloWorld.cpp
//

#include "stdafx.h"

using namespace System;
using namespace Ionic::Zip;

int main(array ^args)
{
    Console::WriteLine(L"Hello World");
    Console::WriteLine(L"Current directory is {0}", System::IO::Directory::GetCurrentDirectory());
        
    ZipFile ^ zip;
    try
    {
        zip = gcnew ZipFile();
        zip->AddEntry("Readme.txt", "", "This is the content.");
        zip->Save("test.zip");
    }
    finally
    {
        zip->~ZipFile();
    }

    Console::WriteLine(L"Press  to quit.");
    Console::ReadLine();
    return 0;
}

 

May 26, 2010 at 5:39 PM
Edited May 26, 2010 at 5:39 PM
Hi Cheeso, First of all, can I just say great library, I really appreciate all the effort you are putting in to develop and support it! I'm using it in a C++/CLI project to create encrypted and potentially multi-spanned archives, which are written to CD (hence the multi-spanning). I also have a small app that auto-launches from the first CD, collates the fragments to a directory on the recipient machine's hard-drive, requests the password and decrypts the archive, attempting to clear up the zip fragments after the extraction is complete. As a follow up to the above discussion, I'm getting an exception when deleting the spanned zip files after extracting, with the error text as follows: "The process cannot access the file 'xxxx.z01' because it is being used by another process." I'm using Ionic.Zip.Reduced.dll v1.9.1.5, on WindowsXP The code is broadly as follows: ... ZipFile^ zip; try { zip = ZipFile::Read(sourcePath); zip->Password = password; zip->ExtractAll(extractPath, ExtractExistingFileAction::OverwriteSilently); } catch(...) { } finally { delete zip; } try { Directory::Delete(sourcePath); } catch(Exception^ ex) { } ... Couple of notes: 1) I've used 'delete zip' rather than 'zip->~ZipFile()' as this has the same effect and is more idiomatic for C++. (I tried zip->~ZipFile() just incase, but get the same results.) 2) If the zip is a single span, it deletes just fine. 2a) To test, I took out the 'delete zip', and I then get the exception even for single span archives, as you might expect! 3) In Windows Explorer I can not delete any of the zip fragments until the extraction application exits. So, I was wondering if the Dispose method fails to close the zip fragments when dealing with a multi-span archive?
May 26, 2010 at 5:41 PM
Hmmm... don't know what has happened to all the carriage returns in that post. Sorry!
Coordinator
May 27, 2010 at 3:38 AM

I'd like you to open a new discussion, rather than resurrecting one that is 9 months old.

Thanks.