如何压缩多个文件并在压缩时拆分zip

时间:2018-09-21 12:38:02

标签: vb.net zip zipfile ziparchive

您好,所以我已经搜索了很长时间,还没有找到任何关于此的信息。我搜索给定文件夹中的所有文件夹,并在ziparchive中为每个文件创建一个条目。我需要保持饲料结构。这就是我现在想出的。

Imports System.IO
Imports System.IO.Compression
'a list of folders I want to zip. These are all located in the testfolder.
Public Shared ListofBackupFolders As New List(Of String) From {"SDK", "Programms", "Application", "Office", "Reports", "UserSettings", "de", "Intrastat", "XSD"}

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim d As New DirectoryInfo("c:\temp\testfolder")
    Dim directories = d.GetDirectories("*")
    File.Create("C:\temp\test.zip").Close()
    For Each directory In directories
        'Only searches the Folders I selecten in the list
        If ListofBackupFolders.Contains(directory.Name) Then
            SearchFolder(directory.FullName)
        End If
    Next
End Sub
Private Sub SearchFolder(path As String)
    Dim dirInfo As New DirectoryInfo(path)
    Dim subDirectories = dirInfo.GetDirectories("*")
    If path.EndsWith("Application") Then
        '>>> Some of the selected folders are in the Application folder
        For Each subdirectory In subDirectories
            If ListofBackupFolders.Contains(subdirectory.Name) Then
                'recursion to get all the folders in the selected folder
                SearchFolder(subdirectory.FullName)
            End If
        Next
    Else
        If subDirectories.Any Then
            For Each subdirectory In subDirectories
                '>>> recursion to get all the folders in the selected folder
                SearchFolder(subdirectory.FullName)
            Next
        End If
    End If
    Dim files = dirInfo.GetFiles("*")
    Using zipToOpen As FileStream = New FileStream("C:\temp\test.zip", FileMode.Open)
        Using archive As ZipArchive = New ZipArchive(zipToOpen, ZipArchiveMode.Update)
            Dim readmeEntry As ZipArchiveEntry
            For Each file In files
                'for every File in the folders it creates the path in the zip 
                Dim zipPath = file.FullName.Replace("C:\", "").Replace("\", "/")
                readmeEntry = archive.CreateEntryFromFile(file.FullName, zipPath, CompressionLevel.Fastest)
            Next
            If Not subDirectories.Any AndAlso Not files.Any Then
                'if there are no files in  a folder i still want the folder in the zip
                Dim folderPath = path.Replace("C:\", "").Replace("\", "/") & "/"
                readmeEntry = archive.CreateEntry(folderPath)
            End If
        End Using
    End Using
End Sub

现在我遇到的问题是,我还没有找到在给定体积(例如200mb)之后分割zip的方法。

我需要做的另一件事是,我有一个文件,例如2gb,我也必须将其拆分。这是与其他代码分开的。

0 个答案:

没有答案
相关问题