Extract file content into string without creating a flie

Find quick help here to get you started with Hollywood
amyren
Posts: 410
Joined: Thu May 02, 2019 11:53 am

Extract file content into string without creating a flie

Post by amyren »

What I am trying to do is to is to get data from this encrypted zip file without creating a physical file.

I have a provided a small example here. First I create a virtualfile to store the secret data and use that virtual file to create the encrypted zip file.
That works without creating a physical secretmessage.data file.
But when extracting the file it does not work to extract it into the virtualfile by using secretfile2$. When using the actual filename the physical file will be created and the secret data is exposed.

Is there a way to extract the encrypted file content into strings without creating an unencrypted file?

Code: Select all

@REQUIRE "zip"
secretfile$ = DefineVirtualFileFromString("This\nis\nthe\nvery\nsecret\nmessage", "secretmessage.data")
zip.OpenArchive(1, "secret.zip", #MODE_WRITE)
zip.AddFile(1, secretfile$, {Encryption = #ZIP_EM_AES_128, Password = "test1234"})
zip.CloseArchive(1)
UndefineVirtualStringFile(secretfile$)
NPrint("ZIP created")

secretfile2$ = DefineVirtualFileFromString("", "secretmessage2.data")
zip.OpenArchive(1, "secret.zip")
zip.ExtractFile(1, 0, "secretmessage2.data", {Password = "test1234"})
zip.CloseArchive(1)
NPrint("File extracted")
OpenFile(1, "secretmessage2.data")
	While Not Eof(1) Do NPrint(ReadLine(1))
CloseFile(1)
UndefineVirtualStringFile(secretfile2$)
WaitLeftMouse
Flinx
Posts: 342
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: Extract file content into string without creating a flie

Post by Flinx »

I wanted to respond quickly because I thought it would be easy, but it took longer than expected. First, here is a working example for reading from a zip archive:

Code: Select all

@REQUIRE "zip", {InstallAdapter = True}
secretfile$ = DefineVirtualFileFromString("This\nis\nthe\nvery\nsecret\nmessage", "secretmessage.data")
zip.OpenArchive(1, "secret.zip", #MODE_WRITE)
zip.AddFile(1, secretfile$, {Password = "test1234"})
zip.CloseArchive(1)
UndefineVirtualStringFile(secretfile$)
NPrint("ZIP created")

OpenFile(1, "secret.zip/secretmessage.data", #MODE_READ, {Password="test1234"})
	While Not Eof(1) Do NPrint(ReadLine(1))
CloseFile(1)
WaitLeftMouse
Unfortunately, this does not work with the encryption method you have chosen. All my attempts in this regard ended with “Cannot read file secretmessage.data !”, even when trying with CopyFile(), for example. Maybe this is a bug.
amyren
Posts: 410
Joined: Thu May 02, 2019 11:53 am

Re: Extract file content into string without creating a flie

Post by amyren »

When not setting the Encryption tag the password is not used, the zip file opens without any password.
But thanks for the effort.

I guess I could just let the file be stored on disk temporary and delete it immediately after reading. Perhaps even wipe the file before deleting so it apear empty for any undelete programs.
Not as secure as encryption, but the file should be created and deleted so fast that the end user never know it was there anyway.
User avatar
jPV
Posts: 734
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: Extract file content into string without creating a flie

Post by jPV »

amyren wrote: Mon Oct 13, 2025 10:07 pm But when extracting the file it does not work to extract it into the virtualfile by using secretfile2$. When using the actual filename the physical file will be created and the secret data is exposed.

Is there a way to extract the encrypted file content into strings without creating an unencrypted file?
You have couple of bugs there. First, you should define the virtual file writable, and secondly, you should use virtual file descriptor instead of its filename.

Here's the fixed version with comments on the lines that I changed:

Code: Select all

@REQUIRE "zip"
secretfile$ = DefineVirtualFileFromString("This\nis\nthe\nvery\nsecret\nmessage", "secretmessage.data")
zip.OpenArchive(1, "secret.zip", #MODE_WRITE)
zip.AddFile(1, secretfile$, {Encryption = #ZIP_EM_AES_128, Password = "test1234"})
zip.CloseArchive(1)
UndefineVirtualStringFile(secretfile$)
NPrint("ZIP created")

secretfile2$ = DefineVirtualFileFromString("", "secretmessage2.data", True) ; Make virtual file writable (the True argument)
zip.OpenArchive(1, "secret.zip")
zip.ExtractFile(1, 0, secretfile2$, {Password = "test1234"}) ; Extract to the file descriptor, don't use the filename
zip.CloseArchive(1)
NPrint("File extracted")
OpenFile(1, secretfile2$) ; Open with the file descriptor, not with the filename
	While Not Eof(1) Do NPrint(ReadLine(1))
CloseFile(1)
UndefineVirtualStringFile(secretfile2$)
WaitLeftMouse
Flinx
Posts: 342
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: Extract file content into string without creating a flie

Post by Flinx »

Even though jPV has built a working script, I still want to correct my faulty one too (Password must be in the user tags). This gives you two solutions.

Code: Select all

@REQUIRE "zip", {InstallAdapter = True}
secretfile$ = DefineVirtualFileFromString("This\nis\nthe\nvery\nsecret\nmessage", "secretmessage.data")
zip.OpenArchive(1, "secret.zip", #MODE_WRITE)
zip.AddFile(1, secretfile$, {Encryption = #ZIP_EM_AES_128, Password = "test1234"})
zip.CloseArchive(1)
UndefineVirtualStringFile(secretfile$)
NPrint("ZIP created")

OpenFile(1, "secret.zip/secretmessage.data", #MODE_READ, {UserTags = {Password = "test1234"}} )
	While Not Eof(1) Do NPrint(ReadLine(1))
CloseFile(1)
WaitLeftMouse
amyren
Posts: 410
Joined: Thu May 02, 2019 11:53 am

Re: Extract file content into string without creating a flie

Post by amyren »

Thank you both for your suggestions.
The script from jPV was the one most similar to my first attempt, but fixed into a working condition.
I think I will go along with the suggestion from Flix, since that way I could read the data directly without creating another virtual file.

I must admit I sometimes find it confusing finding the correct syntax, like when to use curly brackets or not (in this case double curly brackets)

Later in the script i added an option to delete that zip file, but it fails with an error (unable to delete). Ialso tried to force it to be deleted, but here again I am unsure if the syntax is correct.

Code: Select all

DeleteFile("serverconfig.zip",{force=True})
Flinx
Posts: 342
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: Extract file content into string without creating a flie

Post by Flinx »

amyren wrote: Thu Oct 16, 2025 10:58 am Later in the script i added an option to delete that zip file, but it fails with an error (unable to delete). Ialso tried to force it to be deleted, but here again I am unsure if the syntax is correct.
The syntax is correct, and Force should not be necessary.
Did you do this with my script, starting with InstallAdapter = True? This does indeed appear to be a bug.
If the Zip adapter is installed, then DeleteFile() fails on all valid Zip archives, regardless of where they are located or where they come from.
amyren
Posts: 410
Joined: Thu May 02, 2019 11:53 am

Re: Extract file content into string without creating a flie

Post by amyren »

Flinx wrote: Thu Oct 16, 2025 12:45 pm
amyren wrote: Thu Oct 16, 2025 10:58 am Later in the script i added an option to delete that zip file, but it fails with an error (unable to delete). Ialso tried to force it to be deleted, but here again I am unsure if the syntax is correct.
The syntax is correct, and Force should not be necessary.
Did you do this with my skript, starting with InstallAdapter = True? It seems that is really a bug.
If the Zip adapter is installed, then DeleteFile fails on all valid Zip archives, regardless of where they are located or where they come from.
I was looking into this myself, trying to create a smaller MWE out of my main program, and found that the deletefile worked fine until I added the zip plugin.
As you also concluded, I think this must be a bug.
It seem that the bug only happens for encrypted zip files. If I make a sample unencrypted zip archive it works without errors.
Also it only happens when InstallAdapter = True is set.
In my case my workaround is to use the jPV solution which does not require installadapter
Flinx
Posts: 342
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: Extract file content into string without creating a flie

Post by Flinx »

You can replace the InstallAdapter by an Adapter="zip" in the argument table of OpenFile, then DeleteFile works in my script too.
amyren
Posts: 410
Joined: Thu May 02, 2019 11:53 am

Re: Extract file content into string without creating a flie

Post by amyren »

Flinx wrote: Thu Oct 16, 2025 1:42 pm You can replace the InstallAdapter by an Adapter="zip" in the argument table of OpenFile, then DeleteFile works in my script too.
Nice find.
Inspired by your finding I found that you can keep the InstallAdapter if you add Adapter="Inbuilt" in the argument table of DeleteFile.
Post Reply