0

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)

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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.

Comments

0
Dim zipNameArray As String()
Using zip As ZipFile = ZipFile.Read(ZipToUnpack)
    zipNameArray = zip.Select(Function(file) file.FileName).ToArray()
End Using

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.