Jump to content

Problem in deleting temporary zip file after loop creation


neazoi
 Share

Recommended Posts

Hello, I have written the next gui application. At some point it creates a tmp.zip file and when the loop finishes it should delete it. But it does not do so. It deletes the file only when I close the program, or I use a restart script to restart the program.

I think it has something to do with not properly closing the zip file, because when I try to click on the Encode button for the second time, a message is displayed "zip File not found or no file permissions"

Could you please help a bit with it?

; ====================================================
; =============== CDW encoder/decoder ================
; ====================================================
; Version: 0.9
; Language:       English
; Author:         "sv3ora"
; Website:        http://www.qrp.gr/cdw
;
; ----------------------------------------------------------------------------
; Script Start
; ----------------------------------------------------------------------------

#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <String.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <Zip.au3>
#include <base32.au3>
; #include files for encryption and GUI constants

_Main()


Func RestartScript()
      If @Compiled = 1 Then
         Run( FileGetShortName(@ScriptFullPath))
      Else
         Run( FileGetShortName(@AutoItExe) & " " & FileGetShortName(@ScriptFullPath))
      EndIf
   Exit
EndFunc


Func _Main()
    ; Creates window
    GUICreate('CDW encoder/decoder v0.9 by SV3ORA', 370, 330)

    ; Creates main edit
    Local $idEditText = GUICtrlCreateEdit('', 5, 5, 360, 280)

    ; Encoding/Decoding buttons
    Local $idEncodeButton = GUICtrlCreateButton('Encode', 5, 290, 85, 35)
    Local $idDecodeButton = GUICtrlCreateButton('Decode', 96, 290, 85, 35)

    ; Shows window
    GUISetState()


    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop



            Case $idEncodeButton
                ; When you press Encode
;;;;;;;;;;;;;; SELECT FILE TO ENCODE ;;;;;;;;;;;;;;;;
                ; Create a constant variable in Local scope of the message to display in FileOpenDialog.
               Local $sMessage = "Select file to encode."

               ; Display an open dialog to select a file.
               Local $sFileOpenDialog = FileOpenDialog($sMessage, @DesktopDir & "\", "Images (*.jpg;*.png;*.gif)|All files (*.*)", $FD_FILEMUSTEXIST + $FD_MULTISELECT)
               If @error Then
                  ; Display the error message.
                  MsgBox($MB_SYSTEMMODAL, "", "No file was selected.")
                  RestartScript()
                  ; Change the working directory (@WorkingDir) back to the location of the script directory as FileOpenDialog sets it to the last accessed folder.
                  FileChangeDir(@DesktopDir)
               Else
                  ; Change the working directory (@WorkingDir) back to the location of the script directory as FileOpenDialog sets it to the last accessed folder.
                  FileChangeDir(@DesktopDir)

                  ; Replace instances of "|" with @CRLF in the string returned by FileOpenDialog.
                  $sFileOpenDialog = StringReplace($sFileOpenDialog, "|", @CRLF)
               EndIf

;;;;;;;;;;;;;; COMPRESS SELECTED FILE ;;;;;;;;;;;;;;;;
               ;$sFileOpenDialog2 is the tmp.zip file and its path
               Local $sFileOpenDialog2 = @ScriptDir&"\tmp.zip"

               ;compress the file selected
               ;First check if tmp.zip file exists
               Local $iFileExists = FileExists($sFileOpenDialog2)

               ; Display a message of whether the file exists or not.
               If $iFileExists Then
                  _Zip_AddFile(@ScriptDir&"\tmp.zip",$sFileOpenDialog) ;add chosen file ($sFileOpenDialog) to the zip archive
               Else
                  Local $Zip = _Zip_Create(@ScriptDir&"\tmp.zip") ;Create The Zip File. Returns a Handle to the zip File
                  _Zip_AddFile(@ScriptDir&"\tmp.zip",$sFileOpenDialog) ;add chosen file ($sFileOpenDialog) to the zip archive
               EndIf

;;;;;;;;;;;;;; READ COMPRESSED FILE ;;;;;;;;;;;;;;;;
                  ; Open the tmp.zip file for reading and store the handle to a variable.
                  Local $sFileOpen = FileOpen($sFileOpenDialog2, $FO_READ)
                     If $sFileOpen = -1 Then
                        MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")
                        Return False
                     EndIf

                  ; Read the contents of the tmp.zip file using the handle returned by FileOpen.
                  Local $dataZIP = FileRead($sFileOpen)

                  ;Close the temporary tmp.zip file
                  FileClose($sFileOpenDialog2)

                  ; Delete the temporary tmp.zip file.
                  FileDelete($sFileOpenDialog2)

                  ;;;;base32 example;;;;
                  ;$a = _Base32_Encode("AutoIt3")
                  ;MsgBox(0,"Encoded",$a)
                  ;$b = _Base32_Decode($a)
                  ;MsgBox(0,"Decoded",$B)
                  ;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;; ENCODE CONTENTS OF COMPRESSED FILE TO ZBASE32 ;;;;;;;;;;;;;;;;
                  ;Encode the contents of the tmp.zip file to zbase32
                  $encodedDataZIP = _Base32_Encode($dataZIP)
;;;;;;;;;;;;;; DISPLAY ENCODED DATA ;;;;;;;;;;;;;;;;
                  ; Put encoded data to text box
                  GUICtrlSetData($idEditText, $encodedDataZIP)




            Case $idDecodeButton
                ; When you press Decode

                ; Calls the encryption. Sets the data of editbox with the encrypted string
                ;$dEncrypted = _Crypt_DecryptData(GUICtrlRead($idEditText), GUICtrlRead($idInputPass), $iAlgorithm) ; Decrypt the data using the generic password string. The return value is a binary string.
                ;GUICtrlSetData($idEditText, BinaryToString($dEncrypted))

        EndSwitch
    WEnd
EndFunc   ;==>_Main
Edited by neazoi
Link to comment
Share on other sites

No, you are closing a filename in stead of the filehandle returned by the UDF.

Jos

 

Could you please help me, how to do this?

Maybe FileClose($Zip) ?

I have tried this an it did not worked.

I am a new user of autoit (great program really!) I would like to know how to do this please.

Link to comment
Share on other sites

  • Developers

Had a closer look at the code and you seem to open it a seccond time without closing.

Try replacing this portion:

;First check if tmp.zip file exists
                Local $iFileExists = FileExists($sFileOpenDialog2)
                Local $Zip
                ; Display a message of whether the file exists or not.
                If $iFileExists Then
                    _Zip_AddFile(@ScriptDir & "\tmp.zip", $sFileOpenDialog) ;add chosen file ($sFileOpenDialog) to the zip archive
                Else
                    $Zip = _Zip_Create(@ScriptDir & "\tmp.zip") ;Create The Zip File. Returns a Handle to the zip File
                    _Zip_AddFile(@ScriptDir & "\tmp.zip", $sFileOpenDialog) ;add chosen file ($sFileOpenDialog) to the zip archive
                EndIf

                ;;;;;;;;;;;;;; READ COMPRESSED FILE ;;;;;;;;;;;;;;;;
                ; Open the tmp.zip file for reading and store the handle to a variable.
                Local $sFileOpen = FileOpen($sFileOpenDialog2, $FO_READ)
                If $sFileOpen = -1 Then
                    MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")
                    Return False
                EndIf

                ; Read the contents of the tmp.zip file using the handle returned by FileOpen.
                Local $dataZIP = FileRead($sFileOpen)

                ;Close the temporary tmp.zip file
                FileClose($sFileOpen)
                FileClose($Zip)

                ; Delete the temporary tmp.zip file.
                FileDelete($sFileOpenDialog2)

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

Had a closer look at the code and you seem to open it a seccond time without closing.

Try replacing this portion:

;First check if tmp.zip file exists
                Local $iFileExists = FileExists($sFileOpenDialog2)
                Local $Zip
                ; Display a message of whether the file exists or not.
                If $iFileExists Then
                    _Zip_AddFile(@ScriptDir & "\tmp.zip", $sFileOpenDialog) ;add chosen file ($sFileOpenDialog) to the zip archive
                Else
                    $Zip = _Zip_Create(@ScriptDir & "\tmp.zip") ;Create The Zip File. Returns a Handle to the zip File
                    _Zip_AddFile(@ScriptDir & "\tmp.zip", $sFileOpenDialog) ;add chosen file ($sFileOpenDialog) to the zip archive
                EndIf

                ;;;;;;;;;;;;;; READ COMPRESSED FILE ;;;;;;;;;;;;;;;;
                ; Open the tmp.zip file for reading and store the handle to a variable.
                Local $sFileOpen = FileOpen($sFileOpenDialog2, $FO_READ)
                If $sFileOpen = -1 Then
                    MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")
                    Return False
                EndIf

                ; Read the contents of the tmp.zip file using the handle returned by FileOpen.
                Local $dataZIP = FileRead($sFileOpen)

                ;Close the temporary tmp.zip file
                FileClose($sFileOpen)
                FileClose($Zip)

                ; Delete the temporary tmp.zip file.
                FileDelete($sFileOpenDialog2)

Jos

 

Thank you,

I see what my problems were. I corrected these based on your great help, thank you all.

There is another thing that worries me below. I have a restartscript placed in it, because if the user selects no file (presses cancel) the script hangs up.

Is there any way to correct this?

;;;;;;;;;;;;;; SELECT FILE TO ENCODE ;;;;;;;;;;;;;;;;
                ; Create a constant variable in Local scope of the message to display in FileOpenDialog.
               Local $sMessage = "Select file to encode."

               ; Display an open dialog to select a file.
               Local $sFileOpenDialog = FileOpenDialog($sMessage, @DesktopDir & "\", "Images (*.jpg;*.png;*.gif)|All files (*.*)", $FD_FILEMUSTEXIST + $FD_MULTISELECT)
               If @error Then
                  ; Display the error message.
                  MsgBox($MB_SYSTEMMODAL, "", "No file was selected.")
                  RestartScript()
                  ; Change the working directory (@WorkingDir) back to the location of the script directory as FileOpenDialog sets it to the last accessed folder.
                  FileChangeDir(@DesktopDir)
               Else
                  ; Change the working directory (@WorkingDir) back to the location of the script directory as FileOpenDialog sets it to the last accessed folder.
                  FileChangeDir(@DesktopDir)

                  ; Replace instances of "|" with @CRLF in the string returned by FileOpenDialog.
                  $sFileOpenDialog = StringReplace($sFileOpenDialog, "|", @CRLF)
               EndIf

A final thing is that, I see in the zip script '?do=embed' frameborder='0' data-embedContent>>there is a function to create a virtual zip.

Is it created as a zip file in memory (not a real file in the disk)? I may use this if so, to avoid having to create a real file on disk and then erase it.

Thanks once again for your fast help!

PS. I am really thrilled about autoit, I will suggest it to people!

Edited by neazoi
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...