限制zip文件大小

时间:2016-05-24 10:45:52

标签: vbscript zip

我在VBScript中有一个脚本,它获取文件夹的内容并将其放入zip中。它主要基于我在另一篇文章中找到的内容,因为我不是VBS专家。这是功能:

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

我需要将结果文件分成大小有限的文件(80MB)。有没有办法做到这一点?

据我了解,Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, chr(0))部分表示要创建一个zip文件,但我无法找到解释这意味着什么以及如何对行动进行参数化的说明。

2 个答案:

答案 0 :(得分:1)

限制zip文件大小的最简单方法是继续添加文件,直到超过最大大小,然后删除最后添加的项目。

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

当然,有更多智能策略来优化空间使用,但是探索这些策略对于答案来说太宽泛了。

答案 1 :(得分:0)

我终于使用7zip的便携版本解决了这个问题。在那里,我可以使用我需要的参数创建zip文件:

(Register)long_ThreadStackSize
相关问题