Solved

VB.NET Zipping Files

Posted on 2010-08-31
3
454 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:kevp75
  • 2
3 Comments
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33569797
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
 
LVL 25

Accepted Solution

by:
kevp75 earned 0 total points
ID: 33569893
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
 
LVL 25

Author Closing Comment

by:kevp75
ID: 33569903
I answered my own question
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now