限制 zip 文件大小
Limiting zip file size
我在 VBScript 中有一个脚本,它获取文件夹的内容并将其放入一个 zip 文件中。它主要基于我在另一个 post 中发现的内容,因为我不是 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 文件,但我无法找到这意味着什么以及如何参数化操作的解释。
限制 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
<b><i>Set oZip = .GetFile(zipFile)</i></b>
End With
With CreateObject("Shell.Application")
<b><i>cnt = 0
For Each currentItem In .NameSpace(sFolder).Items
cnt = cnt + 1</i></b>
.NameSpace(zipFile).CopyHere <b><i>currentItem</i></b>
Do Until .NameSpace(zipFile).Items.Count = <b><i>cnt</i></b>
WScript.Sleep 1000
Loop
<b><i>If oZip.Size > 83886080 Then
Set lastItem = .NameSpace(zipFile).ParseName(f.Name)
.NameSpace("C:\temp").MoveHere lastItem
Exit For
End If</i></b>
Next
End With
End Sub
当然,还有比这更智能的策略来优化 space 使用,但探索它们的范围太广,无法在此处找到答案。
终于用7zip的便携版解决了。在那里我可以使用我需要的参数创建 zip 文件:
Set oShell = CreateObject("WScript.Shell")
strCmd = "7-Zipz.exe a -v80m " & zipFile & " " & sFolder
oShell.Run(strCmd)
Set oShell = Nothing
我在 VBScript 中有一个脚本,它获取文件夹的内容并将其放入一个 zip 文件中。它主要基于我在另一个 post 中发现的内容,因为我不是 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 文件,但我无法找到这意味着什么以及如何参数化操作的解释。
限制 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
<b><i>Set oZip = .GetFile(zipFile)</i></b>
End With
With CreateObject("Shell.Application")
<b><i>cnt = 0
For Each currentItem In .NameSpace(sFolder).Items
cnt = cnt + 1</i></b>
.NameSpace(zipFile).CopyHere <b><i>currentItem</i></b>
Do Until .NameSpace(zipFile).Items.Count = <b><i>cnt</i></b>
WScript.Sleep 1000
Loop
<b><i>If oZip.Size > 83886080 Then
Set lastItem = .NameSpace(zipFile).ParseName(f.Name)
.NameSpace("C:\temp").MoveHere lastItem
Exit For
End If</i></b>
Next
End With
End Sub
当然,还有比这更智能的策略来优化 space 使用,但探索它们的范围太广,无法在此处找到答案。
终于用7zip的便携版解决了。在那里我可以使用我需要的参数创建 zip 文件:
Set oShell = CreateObject("WScript.Shell")
strCmd = "7-Zipz.exe a -v80m " & zipFile & " " & sFolder
oShell.Run(strCmd)
Set oShell = Nothing