kevp75
asked on
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.
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")
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I answered my own question
Here is the code:
Open in new window