Thursday, 14 December 2017

command line - How to 7za file without specify archive name


Say have an executable target.exe, is there any way to compress this file into target.zip without specify archive name(output name) by using 7zip command line?



Answer



I don't think it's possible with the command line utility alone. The a command expects an archive file name (to add to or create) and a list of files.


If you try without specifying an archive name, you get a System Error: Incorrect Function because the first argument expected is an archive name. If you specify an executable for example, it will say archive type not supported, as it will try to make a .exe archive.


However: 7-zip is open source software. You can modify how the a command works to expect different arguments, or even make a new switch of your own!


Here's a Perl script I quickly threw together which should be able to do what you want:


#!C:/Perl/bin/perl.exe

use warnings;
use strict;

exit unless @ARGV;

my $path = "C:\\Program Files\\7-Zip\\7z.exe"; # modify this accordingly
my $infile = (split /\./,$ARGV[0])[0];

system("\"$path\" a $infile @ARGV");

The script will take the name of the first argument (before the . in the file name) and use that as the base for the archive name. I've assumed you're on Windows by the .exe reference, change the shebang line if not.


Place the script somewhere in your PATH and use it like so:


7z.pl target.exe otherfile someotherfile

It will create an archive named target.7z with those 3 files enclosed.


No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...