11

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

これは機能していましたが、「ファイルが存在します」というエラーが表示されます。

これの理由は何ですか?代替手段はありますか?

4

5 に答える 5

8

上記の解決策はすべて正確ですが、決定的なものではありません。

圧縮されたファイルを一時フォルダーに解凍しようとすると、解凍しようとしている ZIP ファイル内に含まれる各ファイルに対して、"Temporary Folder For YOURFILE.zip " を表示するフォルダーがすぐに作成されます (C:\DocumentsおよびSettings\USERNAME\Local Settings\Temp) 。

そうです、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

以上で、これら 2 つの関数を VBScript でホストされているプログラムにコピー アンド ペーストすれば、Windows XP と Windows 7 で準備完了です。

ありがとう!

于 2012-03-29T23:08:30.393 に答える
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.")

新しい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 に答える