I have a problem I need to try to solve.
Here is the sequence of events I need to accomplish.
1. Pull in listing of every file name in a selected directory
2. They are .zip files so I need to then iterate through each file filtered by the first two letters in the filename, unzip it and retrieve the text file that is in the archive.
3. I have to then open the file and import the data into an SQL table then close the file, the zip file and go to the next file and do the same appending data from each file to the SQL table.
If I could get help in how to iterate through each file name and the process for unzipping it and get access to the text file, I can do the rest as far as imports, etc.
The file names change daily in the folder and the only constant in the filenames are the first two letters. i.e. MS-89-098-234-jutlssgy. There will always be a new file the next day that starts with the MS.
Please help.
Jeff

Dynamic file manipulation
cosmopoet
How about the following
Dim FirstTwoLetters as string = "MS"
For Each foundFile As String In My.Computer.FileSystem.GetFiles _
("C:\test")
If foundFile.substring = FirstTwoLetters then
...Process the file with first two letter match
End If
Next
As far as the process of unzipping them goes - you could try shelling the zip file.
http://www.vb-helper.com/howto_shell_zip_and_unzip.html
Provides a simple way but there are numerous other zip components out there to use.
A simple web search on WB.NET + zip will reveal a lot of links with components etc.
zagolin
The is a Microsoft example that uses the java libraries to handle the zip files. http://msdn.microsoft.com/msdnmag/issues/03/06/ZipCompression/default.aspx
I have been trying to convert the code to VB. I keep get an invalid cast exception on the following line. Any help would be greatly appreceated.
'Reset to first object in collection. Here is where I get the invalid cast exception
m_Wrapped = m_Method
Here is the main form
Imports System
Imports
System.Collections'Imports java.util
Imports
java.util.zip'Imports CsZip
Imports
System.Collections.SpecializedImports
System.IOImports
System.ComponentModelImports
System.ReflectionPublic
Class Form1 Private m_CurrentFile As java.util.zip.ZipFile Public Property CurrentFile() As ZipFile Get Return m_CurrentFile End Get Set(ByVal value As ZipFile) Trym_CurrentFile.close()
Catch ex As Exception End Trym_CurrentFile = value
If m_CurrentFile.ToString <> "" ThenDisplayEntries()
Text = m_CurrentFile.getName
End If End Set End Property Private Sub DisplayEntries()zipListView.BeginUpdate()
zipImageList.Images.Clear()
zipListView.Items.Clear()
Dim imgs As New ListDictionary Dim entry As ZipEntry For Each entry In New CsZip.EnumerationAdapter(New CsZip.EnumerationMethod(AddressOf CurrentFile.entries)) If Not entry.isDirectory Then 'Try Dim name As String = entry.getName Dim item As New ListViewItem(Path.GetFileName(name))item.SubItems.Add(entry.getSize.ToString)
item.SubItems.Add(entry.getCompressedSize.ToString)
item.SubItems.Add(Path.GetDirectoryName(name))
item.Tag = entry
Dim ext As String = Path.GetExtension(name) If imgs(ext) = Nothing Then Dim ic As Icon = IconFromFileType(name) If ic.ToString <> "" ThenzipImageList.Images.Add(ic)
imgs(ext) = zipImageList.Images.Count - 1
End If If imgs(ext).ToString <> "" Thenitem.ImageIndex = imgs(ext)
End IfzipListView.Items.Add(item)
End If 'Catch ex As Exception 'End Try End If NextzipListView.EndUpdate()
End SubThe class that does the zip file handleing
Imports
SystemImports
System.CollectionsImports
java.utilImports
java.util.zipImports
System.Collections.SpecializedNamespace
frmZip Public Delegate Function EnumerationMethod() As Enumeration Public Class EnumerationAdapter Implements IEnumerable Private m_Method As EnumerationMethod Public Class EnumerationWrapper Implements IEnumerator Private m_Method As EnumerationMethod Private m_Wrapped As Enumeration Private m_Current As Object Public Sub New(ByRef method As EnumerationMethod)m_Method = method
End Sub Public ReadOnly Property Current() As Object Implements System.Collections.IEnumerator.Current Get Return m_Current End Get End Property Public Sub Reset() Implements System.Collections.IEnumerator.Reset ' Reset to first object in collection. If m_Method Is Nothing Then Throw New InvalidOperationException Try 'Reset to first object in collection. Here is where I get the invalid cast exceptionm_Wrapped = m_Method
If m_Wrapped Is Nothing Then Throw New InvalidOperationException Catch ex As ExceptionMessageBox.Show(ex.Message.ToString)
End Try
End Sub Public Function MoveNext() As Boolean Implements System.Collections.IEnumerator.MoveNext If m_Wrapped Is Nothing Then Reset() Dim Result As Boolean = m_Wrapped.hasMoreElements If Result Then m_Current = m_Wrapped.nextElement Return Result End Function End Class Public Sub New(ByRef method As EnumerationMethod) If method Is Nothing Then Throw New ArgumentExceptionm_Method = method
End Sub Public Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator Return New EnumerationWrapper(m_Method) End Function End Class Public Delegate Function FilterEntryMethod(ByRef e As ZipEntry) As Boolean Public Class ZipUtils Public Shared Sub CopyStream(ByVal from As java.io.InputStream, ByVal [to] As java.io.OutputStream) Dim buffer(8192) As SBytebuffer(8192) =
New SByte Dim got As Integer While (got = from.read(buffer, 0, buffer.Length)) > 0[to].write(buffer, 0, got)
End While End Sub Public Shared Sub ExtractZipFile(ByVal file As ZipFile, ByVal path As String, ByVal filter As FilterEntryMethod) Dim entry As ZipEntry For Each entry In New EnumerationAdapter(New EnumerationMethod(AddressOf file.entries)) If Not entry.isDirectory() Then If filter Is Nothing Or filter(entry) Then Dim s As java.io.InputStream = file.getInputStream(entry) Try Dim fname As String = System.IO.Path.GetFileName(entry.getName()) Dim newpath As String = System.IO.Path.Combine(path, System.IO.Path.GetDirectoryName(entry.getName()))System.IO.Directory.CreateDirectory(newpath)
Dim dest As New java.io.FileOutputStream(System.IO.Path.Combine(newpath, fname)) TryCopyStream(s, dest)
Finallydest.close()
End Try Finallys.close()
End Try End If End If Next entry End Sub Public Shared Function CreateEmptyZipFile(ByRef fileName As String) As ZipFile Dim zosSPDownload As ZipOutputStream = New ZipOutputStream(New java.io.FileOutputStream(fileName))zosSPDownload.close()
Return New ZipFile(fileName) End Function Public Shared Function UpdateZipFile(ByRef file As ZipFile, ByRef filter As FilterEntryMethod, ByRef newFiles() As String) As ZipFile Dim prev As String = file.getName() Dim tmp As String = System.IO.Path.GetTempFileName() Dim [to] As New ZipOutputStream(New java.io.FileOutputStream(tmp)) TryCopyEntries(file, [to], filter)
' add entries here If Not (newFiles Is Nothing) Then Dim f As String For Each f In newFiles Dim z As New ZipEntry(f.Remove(0, System.IO.Path.GetPathRoot(f).Length))z.setMethod(ZipEntry.DEFLATED)
[to].putNextEntry(z)
Try Dim s As New java.io.FileInputStream(f) TryCopyStream(s, [to])
Finallys.close()
End Try Finally[to].closeEntry()
End Try Next f End If Finally[to].close()
End Tryfile.close()
' now replace the old file with the new oneSystem.IO.File.Copy(tmp, prev,
True)System.IO.File.Delete(tmp)
Return New ZipFile(prev) End Function Public Overloads Shared Sub CopyEntries(ByRef from As ZipFile, ByRef [to] As ZipOutputStream)CopyEntries(from, [to],
Nothing) End Sub Public Overloads Shared Sub CopyEntries(ByRef from As ZipFile, ByRef [to] As ZipOutputStream, ByRef filter As FilterEntryMethod) Dim entry As ZipEntry Try For Each entry In New EnumerationAdapter(New EnumerationMethod(AddressOf from.entries)) If filter Is Nothing Or filter(entry) Then Dim s As java.io.InputStream = from.getInputStream(entry) Try[to].putNextEntry(entry)
TryCopyStream(s, [to])
Finally[to].closeEntry()
End Try Finallys.close()
End Try End If Next entry Catch ex As ExceptionMessageBox.Show(ex.Message.ToString)
End Try End Sub End ClassEnd
NamespaceIf you want to see a working example visit www.ftpex.com