11

When extracting files from a ZIP file I was using the following.

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

This was working, but now I get a "The File Exists" Error.

What is the reason for this? Are there any alternatives?

4

5 回答 5

8

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

如果您尝试将压缩文件提取到临时文件夹中,则会立即为您尝试提取的 ZIP 文件中包含的每个文件C:\Documents(在和中Settings\USERNAME\Local Settings\Temp)创建一个显示“YOURFILE.zip 的临时文件夹”的文件夹。

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

但是,如果您有 200 个文件,它将在 99 处停止并崩溃说明 - The File Exists

..

显然,这不会发生在 Windows 7 上,我在上面看到了这些贡献。但无论如何,我们仍然可以进行检查。好吧,这就是你修复它的方法:

    '========================
    'Sub: UnzipFiles
    'Language: vbscript
    'Usage: UnzipFiles("C:\dir", "extract.zip")
    'Definition: UnzipFiles([Directory where zip is located & where files will be extracted], [zip file name])
    '========================
    Sub UnzipFiles(folder, file)
        Dim sa, filesInzip, zfile, fso, i : i = 1
        Set sa = CreateObject("Shell.Application")
            Set filesInzip=sa.NameSpace(folder&file).items
        For Each zfile In filesInzip
            If Not fso.FileExists(folder & zfile) Then
                sa.NameSpace(folder).CopyHere(zfile), &H100 
                i = i + 1
            End If
            If i = 99 Then
            zCleanup(file, i)
            i = 1
            End If
        Next
        If i > 1 Then 
            zCleanup(file, i)
        End If
        fso.DeleteFile(folder&file)
    End Sub

    '========================
    'Sub: zCleanup
    'Language: vbscript
    'Usage: zCleanup("filename.zip", 4)
    'Definition: zCleanup([Filename of Zip previously extracted], [Number of files within zip container])
    '========================
    Sub zCleanUp(file, count)   
        'Clean up
        Dim i, fso
        Set fso = CreateObject("Scripting.FileSystemObject")
        For i = 1 To count
           If fso.FolderExists(fso.GetSpecialFolder(2) & "\Temporary Directory " & i & " for " & file) = True Then
           text = fso.DeleteFolder(fso.GetSpecialFolder(2) & "\Temporary Directory " & i & " for " & file, True)
           Else
              Exit For
           End If
        Next
    End Sub

就是这样,将这两个函数复制并粘贴到您的 VBScript 托管程序中,您应该可以在 Windows XP 和 Windows 7 上运行。

谢谢!

于 2012-03-29T23:08:30.393 回答
4

您可以使用VBScript 中的DotNetZip

要解压现有的 zipfile,覆盖任何可能存在的文件:

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.")

要创建一个新的 zip 文件:

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.")
于 2009-03-28T09:27:55.803 回答
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 )  
于 2011-07-15T16:42:53.270 回答
2

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

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

于 2008-11-14T21:33:01.680 回答
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
于 2008-12-03T18:53:28.953 回答