Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB.NET Zipping Files

Posted on 2010-08-31
3
Medium Priority
?
467 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

719 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