Friday, September 26, 2008

Unzipping a zip file with subfolder with SharpZipLib and

Unzipping routine using ICSharpCode (IC#Code) released open source dll, called SharpZipLib (#ZipLib), downloadable here

Imports ICSharpCode.SharpZipLib.Zip
If File.Exists(fileName) Then

    destPath = fileName.Substring(0, fileName.Length - 4)

    If Not Directory.Exists(destPath) Then
        For Each s As String In Directory.GetFiles(destPath)
    End If

    Dim inStream As New ZipInputStream(File.OpenRead(fileName))
    Dim outStream As FileStream
    Dim entry As ZipEntry
    Dim buff(2047) As Byte
    Dim bytes As Integer

    Do While True
            entry = inStream.GetNextEntry()
            If entry Is Nothing Then
                Exit Do
            End If

            If entry.Name.Last() = "/" Then
                Directory.CreateDirectory(destPath & "\" & _
                entry.Name.Replace("/", "\"))
                    outStream = File.Create(destPath & _
                    "\" & entry.Name, 2048)
                    Do While True
                        bytes = inStream.Read(buff, 0, 2048)
                        If bytes = 0 Then
                            Exit Do
                        End If
                        outStream.Write(buff, 0, bytes)
                End Try
            End If
ex As ZipException
            rtn += ex.Message & vbCrLf
        End Try

    rtn = "File '" & fileName & "' not found."
End If

Thursday, September 18, 2008

Recursive copy of Files and Folders with VB.Net

It seems so banal, but sometimes the copy of files and folders conteined into a folder can undermine a developer, maybe for its banality.
For this reason, here you have an example of recoursive copy of files and folders.

Private Sub Copy(ByVal SourcePath As String, _
            ByVal DestinationPath As String)
    Dim Files() As String

    If DestinationPath.Chars(DestinationPath.Length - 1) <>
            Path.DirectorySeparatorChar Then
        DestinationPath += Path.DirectorySeparatorChar
    End If

    If Not Directory.Exists(DestinationPath) Then
    End If

    Files = Directory.GetFileSystemEntries(SourcePath)

    Dim Element As String

    For Each Element In Files
        'Sub folders
        If Directory.Exists(Element) Then
            Copy(Element, DestinationPath +
            ' File in folder
            File.Copy(Element, DestinationPath +
                Path.GetFileName(Element), True)
        End If
    Next Element
End Sub