Reading and writing a password protected zip file Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
roysubs
Posts: 425
Joined: 29 Sep 2018, 16:37

Reading and writing a password protected zip file

07 May 2021, 04:08

Say that I have a password protected zip file containing a single .txt file.

During the running of my script, the first time that I access this file, I prompt the user to input a password, then for the rest of the session of that AutoHotkey script, I use that password repeatedly to access that zip file and read the .txt file or update the .txt file.

I'm just not sure of how to approach this. Collecting the password from the user is fine, but does someone have good experience of zip files of how I can open that zip file to read the file inside and to update the contents of the zip file? Ideally, I want to do this without generating any temp files on the hard disk that would show the contents of the .txt file. So basically this is to maintain quite simple/basic security on a .txt file that will contain information that I want to keep relatively private (this is not super-critical information, so even if this is not great security it's more than good enough for my needs as long as temp files with the information are not generated on disk while reading and writing to the zip file).

i.e. filename: mystuff.zip containing mystuff.txt
How would I open the zip and read the whole contents of mystuff.txt?
How would I open the zip and overwrite the whole contents of mystuff.txt with my new text?
User avatar
Smile_
Posts: 858
Joined: 03 May 2020, 00:51

Re: Reading and writing a password protected zip file  Topic is solved

07 May 2021, 05:40

To read you can do this:
data := RunWaitOne("7za.exe e -so -pqwerty mystuff.zip mystuff.txt") with:
data = is the variable to store the content into.
RunWaitOne() = the function used to run a command and return it's output.
7z.exe e -so -pqwerty mystuff.zip mystuff.txt = the used command line (Requires 7 zip executable)
Note: the password is always after -p switch, for this example it is qwerty.

In case you don't want to install 7 zip you can a standalone version, download directly here, you should find executable with 7za.exe name, it is all waht you need.

Now to overwrite the content you can do this:

Code: Select all

; To Create new text file with new content,
NewFile := FileOpen("mystuff.txt", "w")
NewFile.Write("Mynewstuff")              ; specify the new content here
NewFile.Close()
Then add it to the archive with overwrite -aoa mode and delete the file after compression option -sdel.

Code: Select all

RunWait, 7za.exe a -aoa -pqwerty mystuff.zip mystuff.txt -sdel
Since the text file is not huge at size, it will be almost instant while it is updating it content so you wont be able to access to mystuff.txt content easy (Not sure maybe it can be accessible).

Full example:

Code: Select all

; To read and store mystuff.txt (inside mystuff.zip) content to 'data' variable.
data := RunWaitOne("7za.exe e -so -pqwerty mystuff.zip mystuff.txt")
MsgBox % "Read data : " data

; To Create new text file with new content,
NewFile := FileOpen("mystuff.txt", "w")
NewFile.Write("Mynewstuff")              ; specify the new content here
NewFile.Close()

; To write and overwrite mystuff.txt (inside mystuff.zip) content with the new content of the created text file (mystuff.txt).
RunWait, 7za.exe a -aoa -pqwerty mystuff.zip mystuff.txt -sdel

; To read and store mystuff.txt (inside mystuff.zip) content to 'data' variable.
data := RunWaitOne("7za.exe e -so -pqwerty mystuff.zip mystuff.txt")
MsgBox % "Read data : " data

; Copied function from https://www.autohotkey.com/docs/commands/Run.htm#Examples
RunWaitOne(command) {
    ; WshShell object: http://msdn.microsoft.com/en-us/library/aew9yb99
    shell := ComObjCreate("WScript.Shell")
    ; Execute a single command via cmd.exe
    exec := shell.Exec(ComSpec " /C " command)
    ; Read and return the command's output
    return exec.StdOut.ReadAll()
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: fiendhunter, Google [Bot] and 249 guests