[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)
Quote:
Object reference not set to an instance of an object.
Do I need to identify each entry individually before adding it to the list?
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:
Using archive As ZipArchive = ZipFile.Open(stringArchiveName, ZipArchiveMode.Update)
Return archive.Entries.ToList()
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.