-1

I'm trying to use Shell32.dll to unzip a simple compressed file. I have a reference to Shell32.dll which shows up as Interop.Shell.dll on my development PC (Win7 64.) I push exe updates out via file copy from a central location and I would like to do this for the .dll as well.

The program fails on XP using the same file as it installed on my PC. I tried using a Shell.dll from the XP \Windows\System renamed to Interop.Shell.dll but I guess that would be to simple - it gets an error that it cannot load when I try to invoke the dll.

I'm open too another way of doing unzip that uses a dll that is agnostic as to platform. I've used 7Zip via Process.Start() but I'd like to avoid having to install/update a program on the clients.

Is there a common denominator Shell32.dll I could use the would run on any Win OS on/after XP?

Or do I have to create a separate build for each OS if I use Shell32.dll?

Thanks!

Function Unzip(FilePathofZip As String, DestinationFolder As String) As Boolean
    Try
        Dim sFile As String = FilePathofZip
        ' requires referenc to windows.system.shell32.dll
        Dim sc As New Shell32.Shell()

        If IO.Directory.Exists(DestinationFolder) Then IO.Directory.Delete(DestinationFolder, True)
        Application.DoEvents()
        IO.Directory.CreateDirectory(DestinationFolder)
        'Declare the folder where the files will be extracted
        Dim output As Shell32.Folder = sc.NameSpace(DestinationFolder)

        If FilePathofZip.EndsWith(".kmz") Then
            sFile = FilePathofZip & ".zip"
            IO.File.Move(FilePathofZip, sFile)
            Application.DoEvents()
        End If

        'Declare your input zip file as folder 
        Dim input As Shell32.Folder = sc.NameSpace(sFile)
        'Extract the files from the zip file using the CopyHere command .
        output.CopyHere(input.Items, 4)
        Return True
    Catch ex As Exception
        MsgBox(ex.Message)
        Return False
    End Try
End Function
rheitzman
  • 2,247
  • 3
  • 20
  • 36
  • The above code works on Vista64 and Win7 64 - only XP has an issue. – rheitzman Aug 19 '13 at 17:45
  • Surely you get a more descriptive error than "cannot load"? – Hans Passant Aug 19 '13 at 17:48
  • The error is Cannot load/find file. It also says it is looking for the Version 1.0 of the file - that XP version is version 6.x. So it is really complaining that I supplied the wrong file. If I provide the "correct" file it the error is Unable to Cast Com File.... Neither error is a surprise, I'm trying to call a non-XP Shell32.dll. – rheitzman Aug 19 '13 at 17:55
  • You should not distribute `Shell32.dll` or any other DLL that can be found in the System32 folder. Those files are part of Windows itself, and there are different versions for different versions of the OS. The "common denominator" is to not deploy at all, and use the one that is present already (that was put there by the OS installation, a service pack, or a hotfix). – Ken White Aug 19 '13 at 18:02
  • Should I be doing something like CreateObject(????) instead of adding a COM reference to the project? How do I determine the proper name of the object if all I know is the dll name? (My searches on the net for unzipping all say "add a reference" which is not correct for multiple platforms.) – rheitzman Aug 19 '13 at 18:16
  • Surely you get a more descriptive error than "cannot load/find file?" Please don't make us pull that tooth. Post the **exact** exception message and a stack trace. – Hans Passant Aug 19 '13 at 18:22
  • The program is running on a remote PC as a runtime EXE so the error is not easily captured and the stack trace is not available. I know I cannot use the current approach so the error message is not applicable any more. In general I'm trying to expand a compressed folder on XP, Vista, Win7. I'm currently on .Net 2.0 but could go to .Net 4 Client - assuming .Net has a usefully way to do the task. I haven't seen one short of 4.5. – rheitzman Aug 19 '13 at 18:42

1 Answers1

0

I switched to DotNetZip. Add Project Reference to the Ionic.Zip.Reduced.dll. Imports Ionic.Zip

This code is expecting .kmz files downloaded from ESRI REST services but should work with .zip files as well.

Function Unzip(FilePathofZip As String, DestinationFolder As String) As Boolean
    ' extract .kmz files downloaded from ESRI REST services
    Try
        Dim sFile As String = FilePathofZip

        If IO.Directory.Exists(DestinationFolder) Then IO.Directory.Delete(DestinationFolder, True)
        Application.DoEvents()
        IO.Directory.CreateDirectory(DestinationFolder)

        If FilePathofZip.EndsWith(".kmz") Then
            sFile = FilePathofZip & ".zip"
            IO.File.Move(FilePathofZip, sFile)
            Application.DoEvents()
        End If

        Try
            ' Using... required
            Using zip As ZipFile = ZipFile.Read(sFile)
                Dim e As ZipEntry
                For Each e In zip
                    e.Extract(DestinationFolder)
                Next
            End Using
        Catch ex1 As Exception
            MsgBox("Problem with compressed file - notify programmers" & vbCrLf & ex1.Message)
            Return False
        End Try

        Return True
    Catch ex As Exception
        MsgBox(ex.Message)
        Return False
    End Try
End Function
rheitzman
  • 2,247
  • 3
  • 20
  • 36