从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
这是有效的,但现在我得到一个"文件存在"错误.
这是什么原因?还有其他选择吗?
以上所有解决方案都是准确的,但它们并不确定.
如果您尝试将压缩文件解压缩到临时文件夹中,将立即为您尝试提取的ZIP文件中包含的每个文件创建一个显示"YOURFILE.zip的临时文件夹"的文件夹(在C:\Documents
和中Settings\USERNAME\Local Settings\Temp
).
没错,如果你有50个文件,它会在你的临时目录中创建50个文件夹.
但是如果你有200个文件,它将停在99并崩溃说明 - 文件存在.
..
显然,这在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上应该不错.
谢谢!