Solved

VB.NET Zipping Files

Posted on 2010-08-31
3
460 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

752 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