Edit a file inside an archive

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
kiwichick
Posts: 676
Joined: 08 Aug 2012 04:14
Location: Windows 10 Pro 22H2, 150% scaling

Edit a file inside an archive

Post by kiwichick »

Hi there, How would I Open/Edit (not Extract) a particular file inside an archive?

Example: I have an archive named MyArchive.zip which contains, among others, a file named MyFile.txt. I want to open MyArchive.zip then open (not extract) MyFile.txt so that I can edit, save and close it and update the archive without having to extract and repack it.

MyFile.txt is ONLY file I want to open and has the same name in any archive I would run this script on.

I have, of course, been able to use the openwith command to open the archive but I don't know how to then get the script to open MyFile.txt. Cheers.
Windows 10 Pro 22H2

highend
Posts: 14955
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Edit a file inside an archive

Post by highend »

been able to use the openwith command
You're already opening it with a different application (than XYplorer). You won't be able to edit files inside this archive, if the external application doesn't support that. As long as XYplorer doesn't incorporate native handling of .zip files, your lost (apart from writing a script that extracts that single file, and updates it in the zip file after editing it | which most of the archive tools should be able to).
One of my scripts helped you out? Please donate via Paypal

kiwichick
Posts: 676
Joined: 08 Aug 2012 04:14
Location: Windows 10 Pro 22H2, 150% scaling

Re: Edit a file inside an archive

Post by kiwichick »

Yes sorry highend, I may not have been as clear as I could have been. I already do everything manually, I just wanted to automate the first part of the process with a script.

1. Double-click the archive to open it (7-zip is my default). 7-Zip File Manager displays archive contents.
2. Double-click the text file to Open it - or right-click the text file and select either Open or Edit.
3. Edit the text file.
4. Save the text file.
5. Close the text file.
6. Click OK for file modification and to "update it in the archive".

Steps 1 and 2 are the only steps I require the script to do. Does this make it any easier? Cheers.
Windows 10 Pro 22H2

highend
Posts: 14955
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Edit a file inside an archive

Post by highend »

As long as 7zip does not have command line options to achieve what you want, you're still lost... XYplorer isn't able to control external applications in "a graphical" way. You can just extract a special file from an archive, edit it afterwards (manually) and then let the the archiver update it (so that the file is repacked).
One of my scripts helped you out? Please donate via Paypal

kiwichick
Posts: 676
Joined: 08 Aug 2012 04:14
Location: Windows 10 Pro 22H2, 150% scaling

Re: Edit a file inside an archive

Post by kiwichick »

Yes I thought that would be the case. Unfortunately, although 7-zip does have command line options, Open and Edit are the two that aren't included :cry: That's why I hoped there would be some other way to achieve it. Thanks, highend.
Windows 10 Pro 22H2

highend
Posts: 14955
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Edit a file inside an archive

Post by highend »

What seems so wrong with just extracting one file from an existing archive, modify it and readd it afterwards?

Let's assume 7zip (the command line version) is in the same directory like the archive and the text file that should
be modified is called readme.txt:

1.) Just extract the readme.txt (takes a fraction of a second)
7za.exe e test.zip readme.txt

2.) Open the file with openwith() and your favorite text editor. Edit and save it.

3.) Readd that file to the archive (on my system it takes 2-3 seconds with an archive size of ~2,8 GB)
7za.exe u test.zip readme.txt

Isn't so difficult...
One of my scripts helped you out? Please donate via Paypal

kiwichick
Posts: 676
Joined: 08 Aug 2012 04:14
Location: Windows 10 Pro 22H2, 150% scaling

Re: Edit a file inside an archive

Post by kiwichick »

Thanks highend, There's nothing "so wrong with just extracting one file from an existing archive, modify it and readd it afterwards". It just assumes that I know how to do that with command line, which I don't.

And not that your help isn't appreciated, but it would much more helpful if you could give examples of the actual code. After all:

7za.exe e test.zip readme.txt

and

7za.exe u test.zip readme.txt

don't do anything on their own and it can become very confusing trying to find the correct code to tie your bits and pieces together :oops:
Windows 10 Pro 22H2

highend
Posts: 14955
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Edit a file inside an archive

Post by highend »

The "correct code" is a foreach loop...

Code: Select all

$unzip = "D:\Users\Highend\Downloads\7za920\7za.exe";
	$editor = "C:\Windows\notepad.exe";
	$tempDir = "%TEMP%";
	$myFile = "readme.txt";

	// Main part
	foreach($file, get("SelectedItemsPathNames", "|"), "|") {
		run """$unzip"" e ""$file"" -o""$tempDir"" ""$myFile"" -y", , 2;
		run """$editor"" ""$tempDir\$myFile""", , 2;
		run """$unzip"" u ""$file"" ""$tempDir\$myFile""", , 2;
	}
One of my scripts helped you out? Please donate via Paypal

serendipity
Posts: 3360
Joined: 07 May 2007 18:14
Location: NJ/NY

Re: Edit a file inside an archive

Post by serendipity »

I hate to suggest this, but I've found that the not so preferred Windows explorer is probably easiest way to do it (given that you don't have to rely upon 7-zip or others installed).
Try this, select a zip file and paste this in addressbar:

Code: Select all

::run "explorer.exe <curitem>";

kiwichick
Posts: 676
Joined: 08 Aug 2012 04:14
Location: Windows 10 Pro 22H2, 150% scaling

Re: Edit a file inside an archive

Post by kiwichick »

highend, Thank you so so so so much!! You're awesome!! I didn't even come close to getting that and I would have been forever figuring it out. Perfecto :appl:

PS: Would you happen to know the 7-zip commands to 'add selected to separate' zip files (create one zip file for each item selected)? I have seen various pieces of code on the net but I can't make any of them work on 'selected' files and folders - they either do all the files or all the folders.

Cheers!!
Windows 10 Pro 22H2

highend
Posts: 14955
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Edit a file inside an archive

Post by highend »

This has nothing to do with "special" 7zip commands, it's a standard one. You only need to do it in a for-each-loop...

Code: Select all

$unzip = "D:\Users\Highend\Downloads\7za920\7za.exe";

	// Main part
	foreach($file, get("SelectedItemsPathNames", "|"), "|") {
		$baseName = getpathcomponent($file, "base");
		run """$unzip"" a ""$baseName.zip"" ""$file""", , 2;
	}
One of my scripts helped you out? Please donate via Paypal

kiwichick
Posts: 676
Joined: 08 Aug 2012 04:14
Location: Windows 10 Pro 22H2, 150% scaling

Re: Edit a file inside an archive

Post by kiwichick »

You've done it again :appl: :appl: :appl: THANK YOU!!!!!
Windows 10 Pro 22H2

Post Reply