Jump to content

Unzipping zip folder with _SevenZipExtract() for a folder with password - (Moved)


ArcFreez
 Share

Recommended Posts

Hello everyone,

I am having trouble unzipping a folder with a password using SevenZip.au3 library. I am an extreme beginner, and have recently just picked up the language. I think I am passing in the arguments correctly to the function, but I might be wrong and that's where I will need help. I debugged with my friend to see if the UDF was working correctly, and realized the dll path was incorrect, and we fixed that and now the DllOpen() in the UDF works. But it is still failing to extract the zip file with the password since it is setting @error. Here is my code for unzipping the file. The code works all the way until I call _SevenZipExtract() and then it just writes fail to extract because @error is 0. UpdateStates(), and WriteToFile() functions are just my helper functions that just updates the states and writes the states to a file for later statistical analysis. 

; Writes to file based on whether it was detected or not
Func UnzipFile($file)
   ; first search the file with the zip extension
   $search = FileFindFirstFile($file & ".zip")

   ; Check if the search was succesful
   ; if it wasn't then we write to file and return
   If $search = -1 Then
      MsgBox(0, "Error", "No file found : " & $file & ".zip")
      UpdateStats($file, "FAILURE NO FILE")
      WriteToFile()
      Return
   EndIf

   ; if file is found then extract it
   $fileToExt = @WorkingDir & "\" & $file & ".zip"
   $zipSucc = _SevenZipExtract($fileToExt, @WorkingDir, '*', '-p' & $PASSWORD)
   ; if extraction fails then we want to write the data and return
   If $zipSucc = 0 Then
      MsgBox(0, "Error", "Failed to extract file : " & $fileToExt, $WAITSECS)
      UpdateStats($file, "FAILED TO UNZIP")
      WriteToFile()
      Return
   EndIf

   MsgBox(20, "Success", "Successfully unziped the file : " & $fileToExt, $WAITSECS)
   ; detect windows notification and write to file
   UpdateStats($file)
   WriteToFile()
   ; close the search
   FileClose($search)
EndFunc

Here is the _SevenZipExtract() function I was using 

;===============================================================================
;
; Description:      Extracts files from an archive
; Parameter(s):     $sArchive    - The Archive to use
;                   $sOutDir    - Where to extract the files
;                   $sFilter    - Files to extract from the Archive (e.g. *.exe)
;                   $sCMDLine   - Optional Options
;                   $hWnd       - The Window
; Requirement(s):   None
; Return Value(s):  On Success - Return 1
;                   On Failure - Return 0
; Author(s):        JAK-Software.org
; Note(s):          Needs 7-zip32.dll in the path
;
;===============================================================================
Func _SevenZipExtract($sArchive, $sOutDir = ".", $sFilter = '*', $sCMDLine = '', $hWnd = 0)
    $dll = DllOpen("7-zip32.dll")
    DllCall($dll, "int", "SevenZip", "hwnd", $hWnd, "str", 'x ' & $sCMDLine & ' -o' & $sOutDir & ' ' & $sArchive & ' ' & $sFilter, "int", 0)
    $error = @error
    DllClose($dll)
    If $error Then
        Return 0
    Else
        Return 1
    EndIf
EndFunc   ;==>_SevenZipExtract

The password screen should be attached, and I have the passwords to be inputted. I'm just trying to automate inputting password part, and I'm stuck at where I'm going wrong. 

 

 

Thank You.

All help is appreciated! 

problem.PNG

Link to comment
Share on other sites

  • Developers
3 hours ago, ArcFreez said:

'-p' & $PASSWORD

Shouldn't there be a space between "-p" and the password?

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

11 minutes ago, Jos said:

Shouldn't there be a space between "-p" and the password?

No, -pPASSWORD is correct.

-p (set Password) switch

Syntax

-p{password}
{password} Specifies password.

Examples :

7z a archive.7z -psecret -mhe *.txt

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

@ArcFreez :

You can use the commandline versions of 7-Zip directly :

-> copy 7zA.exe, 7zG.exe and 7z.dll (see latest version of 7-Zip) in a directory, here e.g. @ScriptDir

Info EXITCODES :

Spoiler

        0 No error
        1 Warning (Non fatal error(s)). For example, one or more files were locked by some  other application, so they were not compressed.
        2 Fatal error
        7 Command line error
        8 Not enough memory for operation
      255 User stopped the process

Example (q&d) :

Global $s7ZipProg     = @ScriptDir & '\' & "7za.exe" ; w/o  GUI-Progressbar
Global $s7ZipProgGUI  = @ScriptDir & '\' & "7zG.exe" ; with GUI-Progressbar

Global $sZipPassword  = "" ; <== Password
Global $sArchive      = "Archive.Zip" ; <== The Archive to use
Global $sOutDir       = @ScriptDir    ; <== Where to extract the files


; switch : -aoa = Overwrite all existing files without prompt.
; switch : -y   = suppress overwrite queries in the e and x commands.

; 1. w/o  GUI-Progressbar :
$sCommand = $s7ZipProg & ' x "' & $sArchive & '" -o"' & $sOutDir & '" -p' & $sZipPassword & ' -aoa -y'
RunWait ($sCommand, "", @SW_HIDE)

; 2. with GUI-Progressbar :
$sCommand = $s7ZipProgGUI & ' x "' & $sArchive & '" -o"' & $sOutDir & '" -p' & $sZipPassword & ' -aoa -y'
RunWait ($sCommand, "", @SW_SHOW)

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

@Musashi

Hello,

Sorry for asking again after you helped me figure out where I was going wrong, one last question, how do I check if 7zG.exe failed to extract because of incorrect password or something else? For instance, this screen shows up shown in the attachment. This is just part of my automation, but I can deal with it if that screen is guaranteed to show up when recording data. 

And I want the code set error using SetError(1) and to do this if it does fail and log it to a file. 

If @error Then
      MsgBox(0, "Error", "FAILURE UNZIP FILE", $WAITSECS)
      UpdateStats($file, "FAILURE UNZIP FILE")
      WriteToFile()
      Return
   EndIf

I did a little bit of research and someone said something about -bb3 

But they seem to be zipping the file rather than unzipping and don't delve much into the specifics of an error detection. 

Would it just be something like this :

; 2. with GUI-Progressbar :
$sCommand = $s7ZipProgGUI & ' x "' & $sArchive & '" -o"' & $sOutDir & '" -p' & $sZipPassword & ' -aoa -y -bb3'
; ... Do some code with error detection, and if the error is detected SetError(1) 
RunWait ($sCommand, "", @SW_SHOW)

Thank You.

 

problem2.PNG

Link to comment
Share on other sites

@Musashi

Nevermind,

I think I figured it out.

Func UnzipFile($file)
   ; first search the file with the zip extension
   $search = FileFindFirstFile($file & ".zip")

   ; Check if the search was succesful
   ; if it wasn't then we write to file and return
   If $search = -1 Then
      MsgBox(0, "Error", "No file found : " & $file & ".zip")
      UpdateStats($file, "FAILURE NO FILE")
      WriteToFile()
      Return
   EndIf

   ; if file is found then extract it
   $fileToExt = @WorkingDir & "\" & $file & ".zip"
   $s7ZipProgGUI = @ScriptDir & '\' & "7zG.exe" ; with GUI-Progressbar
   $sOutDir = @WorkingDir ; <== Where to extract the files
   $sCommand = $s7ZipProgGUI & ' x "' & $fileToExt & '" -o"' & $sOutDir & '" -p' & $PASSWORD & ' -aoa -y'
   $iReturnCode = RunWait ($sCommand, "", @SW_SHOW, $STDERR_CHILD + $STDOUT_CHILD)
   If $iReturnCode Then
      MsgBox(0, "Error", "FAILURE UNZIP FILE", $WAITSECS)
      UpdateStats($file, "FAILURE UNZIP FILE")
      WriteToFile()
      ProcessClose($iReturnCode)
      Return
   EndIf

   MsgBox(20, "Success", "Successfully unziped the file : " & $fileToExt, $WAITSECS)
   ; detect windows notification and write to file
   UpdateStats($file)
   WriteToFile()
   ; close the search
   FileClose($search)
EndFunc

Thanks. 

Link to comment
Share on other sites

16 minutes ago, ArcFreez said:

how do I check if 7zG.exe failed to extract because of incorrect password or something else?

Give me some time ( approx. 1 hour), I just have a few other things to finish :lol:.

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

@Musashi

Thank You.

I really appreciate it. If my way shown above is incorrect just let me know. 

I just figured it out thanks to other forms about closing the zip. 

I also realized I was killing two processes (child and the parent) when I was doing that code because of 

$STDERR_CHLD + $STDOUT_CHILD

And it was logging the same data twice into my csv file. But it was just suppose to be just 

$STDERR_CHILD

Which logged just once. It still extracts the file unexpectedly, but I guess it logs when it fails 😀. So, my automation works. 

Thank You.

Link to comment
Share on other sites

Hi @ArcFreez !

1 hour ago, ArcFreez said:

I really appreciate it. If my way shown above is incorrect just let me know. 

[...]

So, my automation works.  Thank You.

There are probably neater solutions, but as long as you're satisfied, I'd leave it as it is ;).

As far as I could figure out, 7zG.exe does not write an ERRStream, unlike 7za.exe. On the other hand, 7zG.exe provides you with a progress bar, even if you have to terminate the application with ProcessClose in case of an error (wrong password). I would stick to 7zG.exe :lol:.

Besides :
The Exitcodes of 7za.exe are not very informative anyway. This is e.g. the ERRStream in case a wrong password is used :

PID = 836

StdoutRead ==>  
7-Zip (a) 19.00 (x86) : Copyright (c) 1999-2018 Igor Pavlov : 2019-02-21

Scanning the drive for archives:
1 file, 48342224 bytes (47 MiB)

Extracting archive: C:\AutoIt\Zip-test\Archive.zip
--
Path = C:\AutoIt\Projekte\Zip-test\Archive.zip
Type = zip
Physical Size = 48342224
Sub items Errors: 1
Archives with Errors: 1
Sub items Errors: 1

Have a nice evening, Musashi

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...