使用VBScript从ZIP文件中提取文件

时间:2008-11-14 21:03:22

标签: vbscript zip

从ZIP文件中提取文件时,我使用了以下内容。

Sub Unzip(strFile)
' This routine unzips a file. NOTE: The files are extracted to a folder '
' in the same location using the name of the file minus the extension.  '
' EX. C:\Test.zip will be extracted to C:\Test '
'strFile (String) = Full path and filename of the file to be unzipped. '
Dim arrFile
    arrFile = Split(strFile, ".")
    Set fso = CreateObject("Scripting.FileSystemObject")
    fso.CreateFolder(arrFile(0) & "\ ")
    pathToZipFile= arrFile(0) & ".zip"
    extractTo= arrFile(0) & "\ "
    set objShell = CreateObject("Shell.Application")
    set filesInzip=objShell.NameSpace(pathToZipFile).items
    objShell.NameSpace(extractTo).CopyHere(filesInzip)
    fso.DeleteFile pathToZipFile, True
    Set fso = Nothing
    Set objShell = Nothing
End Sub 'Unzip

这很有效,但现在我收到了“文件存在”错误。

这是什么原因?还有其他选择吗?

5 个答案:

答案 0 :(得分:8)

以上所有解决方案都是准确的,但它们并不确定。

如果您尝试将压缩文件解压缩到临时文件夹中,将立即创建一个显示“YOURFILE.zip的临时文件夹”的文件夹(C:\Documents和{{ 1}})您正在尝试提取的ZIP文件中包含 EACH FILE

没错,如果你有50个文件,它会在你的临时目录中创建50个文件夹。

如果您有200个文件,它将停在99并且崩溃说明 - 文件存在

...

显然,在我看到上面提到的贡献的Windows 7上不会发生这种情况。但无论如何,我们仍然可以进行检查。好的,这就是你解决它的方法:

Settings\USERNAME\Local Settings\Temp

就是这样,将这两个功能复制并粘贴到您的VBScript托管程序中,你应该很高兴,在Windows XP& Windows 7。

谢谢!

答案 1 :(得分:4)

您可以使用VBScript中的DotNetZip

要解压缩现有的zip文件,请覆盖可能存在的所有文件:

WScript.echo("Instantiating a ZipFile object...")
Dim zip 
Set zip = CreateObject("Ionic.Zip.ZipFile")

WScript.echo("Initialize (Read)...")
zip.Initialize("C:\Temp\ZipFile-created-from-VBScript.zip")

WScript.echo("setting the password for extraction...")
zip.Password = "This is the Password."

' set the default action for extracting an existing file
' 0 = throw exception
' 1 = overwrite silently
' 2 = don't overwrite (silently)
' 3 = invoke the ExtractProgress event
zip.ExtractExistingFile = 1

WScript.echo("extracting all files...")
Call zip.ExtractAll("extract")

WScript.echo("Disposing...")
zip.Dispose()

WScript.echo("Done.")

创建新的zipfile:

dim filename 
filename = "C:\temp\ZipFile-created-from-VBScript.zip"

WScript.echo("Instantiating a ZipFile object...")
dim zip2 
set zip2 = CreateObject("Ionic.Zip.ZipFile")

WScript.echo("using AES256 encryption...")
zip2.Encryption = 3

WScript.echo("setting the password...")
zip2.Password = "This is the Password."

WScript.echo("adding a selection of files...")
zip2.AddSelectedFiles("*.js")
zip2.AddSelectedFiles("*.vbs")

WScript.echo("setting the save name...")
zip2.Name = filename

WScript.echo("Saving...")
zip2.Save()

WScript.echo("Disposing...")
zip2.Dispose()

WScript.echo("Done.")

答案 2 :(得分:3)

上面的答案完全正确,但我认为我将所有内容都包含在我正在使用的完整解决方案中:

strZipFile = "test.zip"     'name of zip file
outFolder = "."             'destination folder of unzipped files (must exist)
'If using full paths rather than relative to the script, comment the next line
pwd = Replace(WScript.ScriptFullName, WScript.ScriptName, "")

Set objShell = CreateObject( "Shell.Application" )
Set objSource = objShell.NameSpace(pwd+strZipFile).Items()
Set objTarget = objShell.NameSpace(pwd+outFolder)
intOptions = 256
objTarget.CopyHere objSource, intOptions

'Clean up
Set WshShell = CreateObject("Wscript.Shell")
tempfolder = WshShell.ExpandEnvironmentStrings("%temp%")
Set fso = CreateObject("Scripting.FileSystemObject")
Call fso.DeleteFolder(tempfolder + "\Temporary Directory 1 for " + strZipFile, True )  

答案 3 :(得分:2)

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_23022290.html

检查您的临时目录。如果您有99个与此解压缩过程相关联的文件夹,请尝试删除它们。

答案 4 :(得分:2)

我在解压缩程序的开头添加了以下代码,以便在解压缩之前删除这些目录:

For i = 1 To 99
  If aqFileSystem.Exists(GetAppPath("Local Settings", "") & "\Temp\Temporary Directory " & i & " for DialogState.zip") = True Then
      result = aqFileSystem.ChangeAttributes(GetAppPath("Local Settings", "") & "\Temp\Temporary Directory " & i & " for DialogState.zip", 1 OR 2, aqFileSystem.fattrFree) 
      Call DelFolder(GetAppPath("Local Settings", "") & "\Temp\Temporary Directory " & i & " for DialogState.zip")  
   Else
      Exit For
   End If
Next