tags:

views:

46

answers:

4

Hi, I'm looping through a zip file trying to add the file name of each file within. Is this the correct method?

Dim ZipNameArray(?)

Using zip As ZipFile = ZipFile.Read(ZipToUnpack)
    For Each file In zip
        ZipNameArray(?) = file .FileName
    Next
End Using

I do not know the array size until I start looping through the zip (To work out the number of files within). How do I increment the Array? file is not a number? (It's a ZipEntry)

+1  A: 

You could use an ArrayList object, add the items to it, then call .ToArray() at the end to get an array of ZipEntry objects.

Andrew Lewis
+3  A: 

I would use an generic List(of ZipFile) for this. They are more fail-safe and easier to read.

Dim zips as new List(of ZipFile)

Using zip As ZipFile = ZipFile.Read(ZipToUnpack)
        For Each file In zip
           zips.add(file)
        Next
End Using

And when you want to iterate through:

For each zip as ZipFile in zips 
     dim fileName as string=zip.FileName
Next

In 99% you can forget Arrays in .Net and when you need one you get it with List.ToArray

Tim Schmelter
A: 

Since you don't know the array size there are two options. You could go through the Zip file twice. The first time just count the number of files, then create your array and then go through a second time to add the name of each file.

If your zip file is too large you could always initialize your array to some constant number (say 10) and when you reach the eleventh filename you grow your array by "redim"ing it

For example:

Dim Names(10) as String
Dim counter as Integer
counter = 0
Go through zip {
   counter += 1
   if counter = size of Names then
       ReDim Preserve Names(size of Names + 10) 
   add fileName
 }

More information about arrays (including redim) is here.

Kyra
A: 
Dim zipNameArray As String()
Using zip As ZipFile = ZipFile.Read(ZipToUnpack)
    zipNameArray = zip.Select(Function(file) file.FileName).ToArray()
End Using
Christian Hayter