Page 1 of 1
Edit a file inside an archive
Posted: 13 Sep 2012 05:20
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.
Re: Edit a file inside an archive
Posted: 13 Sep 2012 09:41
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).
Re: Edit a file inside an archive
Posted: 13 Sep 2012 10:53
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.
Re: Edit a file inside an archive
Posted: 13 Sep 2012 11:02
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).
Re: Edit a file inside an archive
Posted: 13 Sep 2012 12:01
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

That's why I hoped there would be some other way to achieve it. Thanks, highend.
Re: Edit a file inside an archive
Posted: 13 Sep 2012 12:30
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...
Re: Edit a file inside an archive
Posted: 13 Sep 2012 14:15
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

Re: Edit a file inside an archive
Posted: 13 Sep 2012 15:10
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;
}
Re: Edit a file inside an archive
Posted: 13 Sep 2012 17:17
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:
Re: Edit a file inside an archive
Posted: 14 Sep 2012 01:40
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
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!!
Re: Edit a file inside an archive
Posted: 14 Sep 2012 09:47
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;
}
Re: Edit a file inside an archive
Posted: 14 Sep 2012 09:56
by kiwichick
You've done it again

THANK YOU!!!!!