Zip bestand uitpakken

Status
Niet open voor verdere reacties.

Woffels

Gebruiker
Lid geworden
8 jan 2006
Berichten
249
Ik krijg het niet voor elkaar om een zip bestand uit te pakken. Na wat speurwerk kom ik op deze code, maar krijg steeds een foutmelding (Fout91):
Code:
Sub UnzipFile()
    Dim objShell As Object
    Dim objFolder As Object
    Dim objSource As Object
    
    ' Pad naar het zip-bestand
    Dim zipPath As String
    zipPath = "D:\zip.zip"
    
    ' Pad naar de doelmap voor het uitpakken
    Dim targetPath As String
    targetPath = "D:\"
    
    Set objShell = CreateObject("Shell.Application")
    Set objSource = objShell.Namespace(zipPath)
    Set objFolder = objShell.Namespace(targetPath)
    
    objFolder.CopyHere objSource.Items
    
    MsgBox "Klaar!", vbInformation
End Sub
 

Bijlagen

  • zip.zip
    209,4 KB · Weergaven: 12
  • Ziptest.xlsm
    13,3 KB · Weergaven: 10
Met externe software geen probleem uiteraard, maar met de macro in het toegevoegde bestand gaat het niet.
 
Doe het eens zo:
Code:
Sub UnzipFile()
    Dim objShell As Object
    Dim zipPath As Variant
    Dim targetPath As Variant
    
    zipPath = "D:\"
    targetPath = "D:\zip.zip"
    
    Set objShell = CreateObject("Shell.Application")
    objShell.Namespace(zipPath).CopyHere objShell.Namespace(targetPath).items
    MsgBox "Klaar!", vbInformation
End Sub
 
Laatst bewerkt:
Toch nog een uitdagingen in dit verhaal. Het kan namelijk ook een *.gz bestand zijn. Als je de gz extensie wijzigt naar .zip kan winrar er prima mee overweg, maar laat ik windows dit "omgenummerde" zip bestand unzippen, dan werkt het niet en de macro ook niet met een dit bestand. Is het mogelijk deze macro ook een gz bestand uit te laten pakken?

Heb een bestand toegevoegd met txt extensie, maar het is een gz bestand, maar omdat Helpmij deze extensie niet toestaat heb ik hem even .txt genoemd
 

Bijlagen

  • zip.txt
    5,9 KB · Weergaven: 11
Wordt het *.gz bestand standaard automatisch door de Verkenner in Windows geopend?
Nee? Dan ff je voorkeurs-gz-uitpakprogramma als standaard app instellen via Instellingen.
Dan zal de macro dat ook al het standaard programma herkennen.
 
Laatst bewerkt:
Dat wil ik voorkomen, omdat het er een flink aantal zijn die ik automatisch binnenhaal en dan als csv via power query aan elkaar plak.
 
Met VBA gaat het in ieder geval niet.
Je kan eventueel een shell opdracht gebruiken voor die conversie.
Overigens net m'n vorige bericht aangepast omdat FileStar niet gratis is.
 
HTML:
https://stackoverflow.com/questions/20222353/extract-all-gz-file-in-folder-using-vba-shell-command

Dit maar eens proberen
 
Dat bedoel ik dus, met een Shell opdracht.
 
Met 7-zip een oplossing gevonden, 7-zip dus wel even installeren.
https://www.rondebruin.nl/win/s7/win004.htm

Code:
Private Declare PtrSafe Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare PtrSafe Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Public Const PROCESS_QUERY_INFORMATION = &H400
Public Const STILL_ACTIVE = &H103

Sub Unzip_gz()
Dim PathZipProgram As String, NameUnZipFolder As String
Dim FileNameZip As Variant, ShellStr As String
'Path of the Zip program
PathZipProgram = "C:\program files\7-Zip\"
If Right(PathZipProgram, 1) <> "\" Then
    PathZipProgram = PathZipProgram & "\"
End If
'Check if this is the path where 7z is installed.
    If Dir(PathZipProgram & "7z.exe") = "" Then
        MsgBox "Please find your copy of 7z.exe and try again"
        Exit Sub
    End If
    NameUnZipFolder = "D:\Downloads\"
    FileNameZip = "D:\Downloads\zip.gz"
    ShellStr = PathZipProgram & "7z.exe x -aoa -r" _
             & " " & Chr(34) & FileNameZip & Chr(34) _
             & " -o" & Chr(34) & NameUnZipFolder & Chr(34) & " " & "*.*"
    ShellAndWait ShellStr, vbHide
End Sub

Public Sub ShellAndWait(ByVal PathName As String, Optional WindowState)
    Dim hProg As Long
    Dim hProcess As Long, ExitCode As Long
    'fill in the missing parameter and execute the program
    If IsMissing(WindowState) Then WindowState = 1
    hProg = Shell(PathName, WindowState)
    'hProg is a "process ID under Win32. To get the process handle:
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, hProg)
    Do
        'populate Exitcode variable
        GetExitCodeProcess hProcess, ExitCode
        DoEvents
    Loop While ExitCode = STILL_ACTIVE
End Sub
 
Dat kan simpeler:

Pak alle .gz bestanden in G:\ uit in G:\ met de commandlineversie van Izarc.

Code:
Sub M_snb()
   Shell """F:\Program Files\Izarc\Izarce"" G:\*.gz"
End Sub
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan