Extract file to a different folder

Jul 30, 2009 at 7:24 PM

I must be having a dumb day.  I want to do something simple: extract a specific file from a zip archive to a different root folder.  In the code fragment below, I get a warning that zip.extract is obsolete and to use zipentry.extract.  If I use zipentry, Intellisense shows only two methods: Equals and ReferenceEquals, no extract method.  If I create my own instance of zipentry, I now get an Intellisense displayed method of extract BUT now I get a warning not to use zipentry.extract but to use zip.extract.  I think we're stuck in an infinite loop here.  So let me ask:  How do I extract a specific member of a zip to a new location where I specify the root drive and folder and an overwrite=True option?

Thanks much!

Using zip As ZipFile = ZipFile.Read(sZipPath)
            For Each sFileName As String In zip.EntryFileNames
               If sFileName = sFilePath Then
                  zip.Extract(sFileName, sRootFolder, True)
                  Dim ZE As New ZipEntry
                  ZE.Extract(sRootFolder, True)
               End If
            Next
         End Using

 

Coordinator
Jul 30, 2009 at 7:42 PM

You're not actually in an infinite loop.  The first warning says to use the Extract() method on the ZipEntry instance.  The second warning suggests that you use the overload of ZipEntry.Extract that accepts a ExtractExistingFileAction, rather than a True/False.

To get a zip entry given an entry name, you can use the string indexer.  So, the resulting code is:

  Using zip As ZipFile = ZipFile.Read(sZipPath)
     For Each sFileName As String In zip.EntryFileNames
        If sFileName = sFilePath Then
           zip(sFileName).Extract(sRootFolder, ExtractExistingFileAction.OverwriteSilently)
        End If
     Next
  End Using

 

Jul 30, 2009 at 7:51 PM

Fast response!   I'll give it a try!  Thanks!

 

Jul 30, 2009 at 9:25 PM

It worked like a charm! 

May I suggest it might be the inspiration for an example in the help file?

Thanks Cheeso!

 

Coordinator
Jul 30, 2009 at 9:33 PM

that's a good suggestion.

Coordinator
Jul 30, 2009 at 9:54 PM

after further review, I think for your purposes the code could be simpler:

  Using zip As ZipFile = ZipFile.Read(sZipPath)
      If zip.EntryFileNames.Contains(sFilePath)
          Console.WriteLine("Extracting {0} to dir {1}", sFilePath, sRootFolder)
          zip(sFilePath).Extract(sRootFolder, ExtractExistingFileAction.OverwriteSilently)
      End If
  End Using

 

Jul 30, 2009 at 11:14 PM

I like the more concise version!  I'll use it!

However, I did run into an issue trying to match my filename against the zip entries:  my path uses backslashes but in the zip they are stored with forward slashes.    I'm now replacing \ with / before comparing.  Did I miss something in the chm file?  Is that a reasonable way to do it?

Continuing thanks for your great support!

 

Coordinator
Jul 31, 2009 at 1:13 AM

yes, that's a reasonable way to do it.  OR,  you can use the string indexer and test for Nothing.  Like this:

  Using zip As ZipFile = ZipFile.Read(sZipPath)
      Dim ze as ZipEntry = zip(sFilePath)
      If Not ze Is Nothing
          Console.WriteLine("Extracting {0} to dir {1}", sFilePath, sRootFolder)
          ze.Extract(sRootFolder, ExtractExistingFileAction.OverwriteSilently)
      End If
  End Using

Inside the string indexer on the ZipFile, it does the slash-swapping implicitly. If the named entry is not found in the ZipFile, then the string indexer returns a Nothing value. Keep in mind that paths inside the zipfile never include drive letters. So you may still have to strip that from sFilePath, if it is present.

Jul 31, 2009 at 9:55 PM

OK, I'll just do the replace. 

Your extract function works so quickly that I've changed the app design to permit real-time viewing of archived files. 

Thanks Cheeso, great work!