VB.NET Zipping Files

Ok.  I am able to zip all the files contained in TempPath, however there are folder/sub-folders,etc that I need to zip up as well, can you help me do this?

Here is what I have so far.
Try
            Dim SitePath As String, TempPath As String = "D:\Sites\ZipCM.NetManagement\Backup\temp\"
            SitePath = Common.BasePath() & "sites\" & AdminStuff.SiteID() & "\"
            Dim tmpZipFile As String = TempPath & "tempzip.zip"
            'Get The Template and files, and write them to XML files to the temp folder
            Dim objDb As New ZipCM.Database
            With objDb
                .ConnectionString = Common.ConnString
                .CommandType = 1
                .Query = "Select layoutContent From vwSiteLayout Where siteID = @SITEID"
                .ParamNames = New String() {"@SITEID"}
                .ParamValues = New String() {AdminStuff.SiteID()}
                .ExecuteDataReader()
                If .TotalRecords > 0 Then
                    Using objFile As New StreamWriter(TempPath & "template.xml")
                        objFile.WriteLine("<?xml version=""1.0"" encoding=""UTF-8"" ?>")
                        objFile.WriteLine(" <Site>")
                        objFile.WriteLine("     <id>" & AdminStuff.SiteID() & "</id>")
                        objFile.WriteLine("     <layout><![CDATA[")
                        objFile.WriteLine(.ReturnValues(0, 0)(1))
                        objFile.WriteLine("     ]]></layout>")
                        objFile.WriteLine(" </Site>")
                        objFile.Close()
                    End Using
                    Erase .ReturnValues
                End If
                'Now get all the pages and write them as xml files
                .Query = "Select * From vwSitePages Where siteID = @SITEID"
                .ParamNames = New String() {"@SITEID"}
                .ParamValues = New String() {AdminStuff.SiteID()}
                .ExecuteDataReader()
                If .TotalRecords > 0 Then
                    Dim tmpPage As String
                    For i As Long = 0 To .TotalRecords - 1
                        tmpPage = TempPath & .ReturnValues(0, i)(1) & ".xml"
                        Using objFile As New StreamWriter(tmpPage)
                            objFile.WriteLine("<?xml version=""1.0"" encoding=""UTF-8"" ?>")
                            objFile.WriteLine(" <Page>")
                            objFile.WriteLine("     <Title>" & .ReturnValues(2, i)(1) & "</Title>")
                            objFile.WriteLine("     <Link>" & .ReturnValues(3, i)(1) & "</Link>")
                            objFile.WriteLine("     <Keywords>" & .ReturnValues(4, i)(1) & "</Keywords>")
                            objFile.WriteLine("     <Description>" & .ReturnValues(5, i)(1) & "</Description>")
                            objFile.WriteLine("     <Content><![CDATA[" & .ReturnValues(6, i)(1) & "]]></Content>")
                            objFile.WriteLine("     <TagCloud>" & .ReturnValues(7, i)(1) & "</TagCloud>")
                            objFile.WriteLine("     <Added>" & .ReturnValues(8, i)(1) & "</Added>")
                            objFile.WriteLine("     <Modified>" & .ReturnValues(9, i)(1) & "</Modified>")
                            objFile.WriteLine("     <ShowMod>" & .ReturnValues(10, i)(1) & "</ShowMod>")
                            objFile.WriteLine("     <ShowTagCloud>" & .ReturnValues(11, i)(1) & "</ShowTagCloud>")
                            objFile.WriteLine("     <Active>" & .ReturnValues(12, i)(1) & "</Active>")
                            objFile.WriteLine("     <NavActive>" & .ReturnValues(13, i)(1) & "</NavActive>")
                            objFile.WriteLine("     <Privacy>" & .ReturnValues(14, i)(1) & "</Privacy>")
                            objFile.WriteLine("     <ParentPage>" & .ReturnValues(15, i)(1) & "</ParentPage>")
                            objFile.WriteLine("     <Order>" & .ReturnValues(16, i)(1) & "</Order>")
                            objFile.WriteLine(" </Page>")
                            objFile.Close()
                        End Using
                    Next
                    Erase .ReturnValues
                End If
            End With
            objDb = Nothing
            'Copy all the sites files to the temp folder
            CopyDirectory(SitePath & "\Files\", TempPath & "Files\", True)
            CopyDirectory(SitePath & "\Images\", TempPath & "Images\", True)
            CopyDirectory(SitePath & "\Flash\", TempPath & "Flash\", True)
            CopyDirectory(SitePath & "\Styles\", TempPath & "Styles\", True)
            CopyDirectory(SitePath & "\Scripts\", TempPath & "Scripts\", True)
            CopyDirectory(SitePath & "\_thumbs\", TempPath & "_thumbs\", True)
            'Zip everything in the temp folder, and move the zip file to 'Files'
            Dim FolderNames As String() = Directory.GetFiles(TempPath)
            Dim FileNames As String() = Directory.GetFiles(TempPath)
            Using objZip As New ZipOutputStream(File.Create(tmpZipFile))
                With objZip
                    .SetLevel(9)
                    Dim buffer() As Byte
                    ReDim buffer(4096)
                    For Each f As String In FileNames
                        Dim objFileEntry As ZipEntry = New ZipEntry(Path.GetFileName(f))
                        objFileEntry.DateTime = DateTime.Now
                        'objFileEntry.Size = f.Length
                        .PutNextEntry(objFileEntry)
                        objFileEntry = Nothing
                        Dim fs As FileStream = File.OpenRead(f)
                        Dim sourceBytes As Integer = 1
                        Do Until (sourceBytes <= 0)
                            sourceBytes = fs.Read(buffer, 0, buffer.Length)
                            objZip.Write(buffer, 0, sourceBytes)
                        Loop
                        fs.Close()
                    Next
                    .Finish()
                    .Close()
                End With
            End Using
            File.Copy(tmpZipFile, "D:\Sites\ZipCM.NetManagement\Backup\Files\" & Replace(DateTime.Today, "/", String.Empty) & ".zip")

Open in new window

LVL 25
kevp75Asked:
Who is Participating?
 
kevp75Connect With a Mentor Author Commented:
sorry.  SharpZip can, and I found the solution.

On top of that, it gets rid of a ton of code....


            Dim objZip As New FastZip
            objZip.CreateZip(tmpZipFile, TempPath, True, "")
            objZip = Nothing
0
 
ZhaolaiCommented:
Not sure if SharpZipLib can do folders, but J# zip can do. The assembly vjslib.dll is part of the .NET framework.
Here is the code:

Imports System.IO
Imports java.io
Imports java.util
Imports java.util.zip

    Public Sub CreateZipFile(ByVal sFolderPath As String)
        Dim fos As java.io.FileOutputStream = Nothing
        Dim zos As java.util.zip.ZipOutputStream = Nothing
        Dim di As System.IO.DirectoryInfo

        'create a zip file with same name in the same path
        fos = New java.io.FileOutputStream(sFolderPath & "MyZipFile.zip")
        zos = New java.util.zip.ZipOutputStream(fos)
        di = New System.IO.DirectoryInfo(sFolderPath)
        'procedure to zip a directory
        ZipDirectory(fos, zos, di, sFolderPath)
        zos.flush()
        zos.close()
        fos.flush()
        fos.close()
    End Sub

    Private Sub ZipDirectory(ByVal fos As java.io.FileOutputStream, ByVal zos As java.util.zip.ZipOutputStream, ByVal di As System.IO.DirectoryInfo, ByVal SRootDir As String)
        Dim fis As java.io.FileInputStream
        Dim ze As java.util.zip.ZipEntry

        'to get file info from the directory
        Dim fInfos As System.IO.FileInfo() = di.GetFiles
        Dim fInfo As System.IO.FileInfo

        For Each fInfo In fInfos
            If fInfo.Extension.ToLower <> ".zip" Then
                'give the zip entry or the folder arrangement for the file
                ze = New java.util.zip.ZipEntry(fInfo.FullName.Substring(SRootDir.LastIndexOf("\") + 1).Replace("\", "/"))

                'The DEFLATED method is the one of the methods to zip a file
                ze.setMethod(ZipEntry.DEFLATED)
                zos.putNextEntry(ze)

                'Input stream for the file to zip
                fis = New java.io.FileInputStream(fInfo.FullName)

                'Copy stream is a simple method to read a file input stream (file to zip) and write it to a file output stream(new zip file)
                CopyStream(fis, zos)

                zos.closeEntry()
                fis.close()
            End If
        Next

        'If the directory contains the sub directory the call the same procedure
        Dim dinfos As System.IO.DirectoryInfo() = di.GetDirectories()
        Dim dinfo As System.IO.DirectoryInfo
        For Each dinfo In dinfos
            ZipDirectory(fos, zos, dinfo, SRootDir)
        Next

    End Sub

    Private Sub CopyStream(ByVal src As java.io.FileInputStream, ByVal dest As java.util.zip.ZipOutputStream)
        Dim reader As New java.io.InputStreamReader(src)
        Dim writer As New java.io.OutputStreamWriter(dest)
        While reader.ready
            writer.write(reader.read)
        End While
        writer.flush()
    End Sub

Open in new window

0
 
kevp75Author Commented:
I answered my own question
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.