Home Forums .NET libraries Xceed Zip & Real-Time Zip for .NET zip multiple directories and preserving path

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #20391 |

     Is there way to zip multiple directories to one zip file while preserving path.
    I would like Backup.zip to contain:
    Path1
          apples.txt
          pear.txt
       Mail  —-sub folder
           letter1.txt
    Path2
          textbooks.txt
          mathbook.txt
       USA —sub folder
           letter1.txt

    Dim file1 As New DiskFolder(“\\WIN7\Path1”)
    Dim file2 As New DiskFolder(“\\WIN7\Path2”)
    Dim zipFile As AbstractFile = New DiskFile(“\\WIN7\Backup\Backup.zip”)
    Dim archive As New ZipArchive(zipFile)

    archive.BeginUpdate()

    file1.CopyFilesTo(archive, True, True)

    For Each f As AbstractFolder In file1.GetFolders(False)
    f.CopyTo(archive, True)
            Next
    file2.CopyFilesTo(archive, True, True)

            For Each f As AbstractFolder In file2.GetFolders(False)
                f.CopyTo(archive, True)
            Next
            archive.EndUpdate()

    Imported from legacy forums. Posted by David (had 1031 views)

    Xceed Support
    Member
    Post count: 5658

    Hi David,

    You can create a zip file from two folders with subfolders in them. Try the following (this code was translated from a c# to VB translator:

    ———–

    Private Sub btnZipTest_Click(sender As Object, e As EventArgs)
    	txtConsole.Text = ""
    	' Let's say we want to create the following zip file.
    	Dim zipFile As AbstractFile = New DiskFolder(Path.GetTempPath()).GetFile("New.zip")
    	txtConsole.Text += "Creating " + zipFile.FullName
    
    	' In order to create a new zip file, all we have to do is make sure
    	' that file does not exist before creating our ZipArchive.
    	If zipFile.Exists Then
    		' If you run this snippet twice, this will happen the second time.
    		txtConsole.Text = "Deleting {0}" + zipFile.FullName
    		zipFile.Delete()
    	End If
    
    	' Now that the file does not exist, we can create a new zip file.
    	Dim archive As New ZipArchive(zipFile)
    
    	' At this point, no zip file exists yet.
    	txtConsole.Text += vbCr & vbLf
    	txtConsole.Text += If(zipFile.Exists, "It exists", "It doesn't exist")
    
    	Dim sr2 As AbstractFolder = New DiskFolder("D:\Music")
    	Dim sr3 As AbstractFolder = New DiskFolder("D:\Training")
    
    	Try
    		archive.BeginUpdate()
    		sr2.CopyTo(archive, True)
    		sr3.CopyTo(archive, True)
    		archive.EndUpdate()
    		txtConsole.Text += vbCr & vbLf & "It worked!"
    	Catch epn As Exception
    		If epn.InnerException IsNot Nothing Then
    			txtConsole.Text += vbCr & vbLf & "Error: " & epn.InnerException.ToString()
    		Else
    			txtConsole.Text += vbCr & vbLf & "Error: " & epn.Message.ToString()
    		End If
    		txtConsole.Text += "Stack Trace: " & vbCr & vbLf + epn.StackTrace
    	Finally
    		txtConsole.Text += vbCr & vbLf & "And we're done!"
    	End Try
    End Sub

    ———–

    Imported from legacy forums. Posted by Marc [Xceed] (had 159 views)

    Xceed Support
    Member
    Post count: 5658

    Concerning the code above, if the path is in the root, it will throw an error advising you that it cannot copy from the root directory.

     

    The files will merge since the destination is the
    same and CopyFilesTo takes the folder contents not the folder itself.
    Especially in the case of a root folder as it has no name anyway. If you
    want both your sources to be placed in different places, you can take
    advantage of the fact that you can specify a detailed destination when
    preparing a copy. You can try the following:
    ———
    ‘ Let’s say we want to create the following zip file.
    Dim zipFile As AbstractFile = New
            DiskFolder(Path.GetTempPath()).GetFile(“New.zip”)
    txtConsole.Text += “Creating ” + zipFile.FullName

    ‘ In order to create a new zip file, all we have to do is make sure
    ‘ that file does not exist before creating our ZipArchive.
    If zipFile.Exists Then
        ‘ If you run this snippet twice, this will happen the second time.
        txtConsole.Text = “Deleting ” + zipFile.FullName
        zipFile.Delete()
    End If

    ‘ Now that the file does not exist, we can create a new zip file.
    Dim archive As New ZipArchive(zipFile)

    ‘ At this point, no zip file exists yet.
    txtConsole.Text += vbCr & vbLf
    txtConsole.Text += If(zipFile.Exists, “It exists”, “It doesn’t exist”)

    Dim sr2 As AbstractFolder = New DiskFolder(“C:\Music”)
    Dim sr3 As AbstractFolder = New DiskFolder(“C:\Training”)
    Dim MyFolder As AbstractFolder = New DiskFolder(Path.GetTempPath()).GetFolder(“MyFolder”)

    Try
        archive.BeginUpdate()

        MyFolder = archive.GetFolder(sr2.Name)
        sr2.CopyFilesTo(MyFolder, True, True)

        MyFolder = archive.GetFolder(sr3.Name)
        sr3.CopyFilesTo(MyFolder, True, True)

        archive.EndUpdate()

        txtConsole.Text += vbCr & vbLf & “It worked!”
    Catch epn As Exception
        If epn.InnerException IsNot Nothing Then
            txtConsole.Text += vbCr & vbLf & “Error: ” & epn.InnerException.ToString()
        Else
            txtConsole.Text += vbCr & vbLf & “Error: ” & epn.Message.ToString()
        End If
        txtConsole.Text += “Stack Trace: ” & vbCr & vbLf + epn.StackTrace
    Finally
        txtConsole.Text += vbCr & vbLf & “And we’re done!”
    End Try

    Imported from legacy forums. Posted by Marc [Xceed] (had 998 views)

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.