Limiting zip file size

2019-09-08 18:29发布

I have a script in VBScript that gets the content of a folder and gets it into a zip. It is mainly based on what I found in another post, as I am no VBS expert. This is the function:

Sub ArchiveFolder (zipFile, sFolder)
    With CreateObject("Scripting.FileSystemObject")
        zipFile = .GetAbsolutePathName(zipFile)
        sFolder = .GetAbsolutePathName(sFolder)

        With .CreateTextFile(zipFile, True)
            .Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, chr(0))
        End With
    End With

    With CreateObject("Shell.Application")
        .NameSpace(zipFile).CopyHere .NameSpace(sFolder).Items

        Do Until .NameSpace(zipFile).Items.Count = _
                 .NameSpace(sFolder).Items.Count
            WScript.Sleep 1000 
        Loop
    End With
End Sub

I need to divide the resulting file in size limited files (80MB). Is there a way to do this?

As I understand it, the Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, chr(0)) part is indicating to create a zip file, but I am not able to find an explanation of what this means and how to parametrize the action.

标签: vbscript zip
2条回答
Explosion°爆炸
2楼-- · 2019-09-08 18:54

I have finally solved it by using 7zip's portable version. There I can create the zip file with the parameters I need:

Set oShell = CreateObject("WScript.Shell")
strCmd = "7-Zip\7z.exe a -v80m " & zipFile & " " & sFolder 
oShell.Run(strCmd)
Set oShell = Nothing
查看更多
The star\"
3楼-- · 2019-09-08 18:58

The simplest approach to limiting the size of a zip file is to continue adding files until the maximum size is exceeded, then remove the last item added.

Sub ArchiveFolder (zipFile, sFolder)
    With CreateObject("Scripting.FileSystemObject")
        zipFile = .GetAbsolutePathName(zipFile)
        sFolder = .GetAbsolutePathName(sFolder)

        With .CreateTextFile(zipFile, True)
            .Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, chr(0))
        End With

        Set oZip = .GetFile(zipFile)
    End With

    With CreateObject("Shell.Application")
        cnt = 0
        For Each currentItem In .NameSpace(sFolder).Items
            cnt = cnt + 1
            .NameSpace(zipFile).CopyHere currentItem

            Do Until .NameSpace(zipFile).Items.Count = cnt
                WScript.Sleep 1000
            Loop

            If oZip.Size > 83886080 Then
                Set lastItem = .NameSpace(zipFile).ParseName(f.Name)
                .NameSpace("C:\temp").MoveHere lastItem
                Exit For
            End If
        Next
    End With
End Sub

Of course there are more intelligent strategies than this to optimize space usage, but exploring them would be far too broad for an answer here.

查看更多
登录 后发表回答