2008-11-14 12 views
10

Podczas wyodrębniania plików z pliku ZIP korzystałem z następujących czynności.Wyodrębnij pliki z pliku ZIP za pomocą VBScript

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 

To działało, ale teraz pojawia się błąd "Plik istnieje".

Jaki jest tego powód? Czy są jakieś alternatywy?

+0

@ Tester101 Proszę oznaczyć jedną z odpowiedzi jako odpowiedź lub samodzielnie. Dziękuję –

Odpowiedz

2

I dodaje następujący kod do początku mojego postępowania plików do usunięcia tych katalogów przed rozpakować:

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 
4

Można użyć DotNetZip z VBScript.

Aby rozpakować istniejący zipfile, zastępując wszystkie pliki, które mogą istnieć:

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

Aby utworzyć nowy 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

nie ma odpowiedzi powyżej, które są idealnie poprawne, ale myślę, że” Zawij wszystko do pełnego rozwiązania, którego używam:

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) 
8

Wszystko powyżej zolu są dokładne, ale nie są ostateczne.

Jeśli próbujesz wyodrębnić spakowany plik do folderu tymczasowego, natychmiast zostanie utworzony folder wyświetlający "Folder tymczasowy dla pliku YOURFILE.zip" (w C: \ Documents and Settings \ USERNAME \ Local Settings \ Temp) dla KAŻDE PLIKI zawarte w pliku ZIP, który próbujesz wyodrębnić.

Zgadza się, jeśli masz 50 plików, utworzy on 50 folderów w twoim katalogu temp.

ale jeśli masz 200 plików, zatrzyma na 99 i katastrofy stwierdzając - plik istnieje

..

Widocznie ten nie występuje w systemie Windows 7, z wkładów wyświetlić powyżej . Ale niezależnie, nadal możemy mieć kontrole. W porządku, więc tak to naprawisz.

'======================== 
    '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 

I to on, skopiuj i wklej te dwie funkcje w swojej VBScript gospodarzem programu i powinno być dobrze iść, Windows XP & Windows 7.

Dzięki!

+1

ładny kod do sprzątania; dzięki! fso.GetSpecialFolder (TemporaryFolder) będzie jeszcze ładniejszy :) – Juliusz

+0

Miałem z tym sporo problemów, dopóki nie zacząłem używać pełnych, absolutnych ścieżek. ". \ zipfile.zip" w ogóle mi nie działało. – sirdank