Results 1 to 2 of 2

Thread: [RESOLVED] ZipArchive get entry list

  1. #1
    Frenzied Member
    Join Date
    Sep 08
    Posts
    1,028

    Resolved [RESOLVED] ZipArchive get entry list

    Can I please have some help in getting a list of all the items in a ZipArchive.

    Here is my code:

    Code:
        Public Function returnListOfAllFilesInZipArchive(stringArchiveName As String) As List(Of ZipArchiveEntry)
    
            Dim zipArchiveEntryList As List(Of ZipArchiveEntry)
    
            Using archive As ZipArchive = ZipFile.Open(stringArchiveName, ZipArchiveMode.Update)
                For Each entry As ZipArchiveEntry In archive.Entries
                    zipArchiveEntryList.Add(entry)
                Next
            End Using
    
            Return zipArchiveEntryList
    
        End Function
    And I use it like this:

    Code:
    Dim listOfZipEntries As List(Of ZipArchiveEntry) = returnListOfAllFilesInZipArchive(archiveFileName)
    I am getting an error at:

    Code:
    zipArchiveEntryList.Add(entry)
    Object reference not set to an instance of an object.
    Do I need to identify each entry individually before adding it to the list?

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,860

    Re: ZipArchive get entry list

    You really shouldn't need help at this stage with a NullReferenceException. You simply look at each reference on the line and see which one is Nothing. Given that there is only one relevant reference on that line you shouldn't even need to do that. Not only that, the Exception Assistant window would have given you some suggestions and one of those is exactly your issue.

    Apart from that, just use a LINQ query:
    vb.net Code:
    1. Using archive As ZipArchive = ZipFile.Open(stringArchiveName, ZipArchiveMode.Update)
    2.     Return archive.Entries.ToList()
    3. End Using
    That said, is a List really appropriate in that situation? Maybe it is but, if you don't intend to add and remove items from that List, you should be using an array instead. You should always use an array in preference to a List unless you specifically need a List. In that case you can call ToArray rather than ToList.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •