Another 7-zip script

Discuss and share scripts and script files...
Plastic
Posts: 36
Joined: 08 Sep 2008 00:18

Re: Another 7-zip script

Post by Plastic »

Hi lukescammell,

I also added "Options by archive type" you requested, thanks for your input ;)
Do you still have problems with relative path ?

Request 5. (Same basenames + Multiple archives) could be quite complex, so I'll leave it at that for now, sorry.

Request 6. conflict with my use of the script, so I won't do it. But feel free to modify it to your content!
I myself use labels to streamline even more my use
2009-09-15_004813.png
2009-09-15_004813.png (29.16 KiB) Viewed 4462 times
If there is only 1 label, it is executed directly.
If there us multiple labels, a menu is displayed so you can choose.
That way I can press Z to compress with my default option, and Shift+Z to choose from a list of options. It is quite convenient! :)

For the last bug you spotted, I am not sure why you got this, I'll have to investigate.
Maybe it's my _get_item_basename routine which fails with big path.
A native basename function would be cool though ;)

Regards

lukescammell
Posts: 744
Joined: 28 Jul 2006 13:15
Location: Kent, UK
Contact:

Re: Another 7-zip script

Post by lukescammell »

admin wrote:"The data area passed to a system call is too small" -- wow, never heard of this error. This message comes from "the system". I would assume there's a limit around 2048 characters. Could it be that you reached that limit?
All the files I was selecting are in the directory

Code: Select all

C:\Users\luke\Downloads\My Dropbox\PortableApps\Firefox2.0Portable\App\Firefox\components\
I don't know how the script works, but I assume it creates a list of the files you want to compress including their full path? If that's the case, then I guess it could be exceeding some limit I guess? Always glad to find another bug ;)
Used to update to the latest beta every day. Now I have children instead…
Windows 10 Pro x64 (everywhere except phone…)

Plastic
Posts: 36
Joined: 08 Sep 2008 00:18

Re: Another 7-zip script

Post by Plastic »

I don't know how the script works, but I assume it creates a list of the files you want to compress including their full path? If that's the case, then I guess it could be exceeding some limit I guess? Always glad to find another bug ;)
Yes, that's exactly what I do!
Maybe I could work around this by using the @filelist option of 7zip, putting all items in a temporary file.
I'll try that when I get more time

lukescammell
Posts: 744
Joined: 28 Jul 2006 13:15
Location: Kent, UK
Contact:

Re: Another 7-zip script

Post by lukescammell »

Wouldn't hurt to use a 7zip-filelist.ini file in the same directory as the script like the collect script. Would be nice for the file to be cleared after completion though from a privacy point on view...
Used to update to the latest beta every day. Now I have children instead…
Windows 10 Pro x64 (everywhere except phone…)

lukescammell
Posts: 744
Joined: 28 Jul 2006 13:15
Location: Kent, UK
Contact:

Re: Another 7-zip script

Post by lukescammell »

Hi Plastic, any chance of an update to this - I'm not XYscript savvy and I keep running into that bug...
Edit1: Hmm, yeah, ok, now I can't trigger it, perhaps Don increased the buffer size or whatever :mrgreen:
Edit2: Okay, just noticed you have updated the script... perhaps Don should start thinking about adding a version of Vanilla Addons to the XY site where scripts can be uploaded ;)
Disclaimer: that software's not ready for prime time yet, neither is Vanilla 2, but when they are...
Used to update to the latest beta every day. Now I have children instead…
Windows 10 Pro x64 (everywhere except phone…)

Plastic
Posts: 36
Joined: 08 Sep 2008 00:18

Re: Another 7-zip script

Post by Plastic »

Hi lukescammell, does that means your problem is already fixed ?

lukescammell
Posts: 744
Joined: 28 Jul 2006 13:15
Location: Kent, UK
Contact:

Re: Another 7-zip script

Post by lukescammell »

Nah, I've managed to get the bug back by following my old example - I think I've drafted a reply to this end but didn't get around to posting it... I have run into the bug on a few other occasions, but I can't remember specifics, definitely the same one though.

I've updated to your latest copy and ditched the -r, I guess that is what caused it to compress both /example.txt and /directory/example.txt into example.7z when nly /example.txt was selected?

And another thing.. :D Is there a way to prevent it from stripping text after a dot on folders? Currently compressing a folder such as /example.com-2009/ results in an archive of example.7z instead of example.com-2009.7z
Used to update to the latest beta every day. Now I have children instead…
Windows 10 Pro x64 (everywhere except phone…)

Plastic
Posts: 36
Joined: 08 Sep 2008 00:18

Re: Another 7-zip script

Post by Plastic »

Hi lukescammell, I think the last update should interest you ;)

lukescammell
Posts: 744
Joined: 28 Jul 2006 13:15
Location: Kent, UK
Contact:

Re: Another 7-zip script

Post by lukescammell »

Yay, that old bug has gone :) Thanks! I can now compress things with impunity :mrgreen:

However, I'm a bit wary of having a file list written to the local machine since it's against the idea of the app being portable so I humbly suggest the following change to line 9 from:

Code: Select all

    //$g_list_filename = "%TMP%\7zip.filelist";
to:

Code: Select all

    $g_list_filename = "<xypath>\Scripts\7zip.filelist";
This just seems like a more privacy friendly default, of course you can use whatever you like, but... ;)

One other minor suggestion to the script would be to add this little snippet (stolen from someone else, The collect script I think...) to line 105 just before /** PRIVATE **/:

Code: Select all

-
"Edit script : edit"
   self $ScriptFile, file;
   Open $ScriptFile;
-
"Cancel"
It just makes tweaking and changing the path nice and easy ;)
lukescammell wrote:And another thing.. :D Is there a way to prevent it from stripping text after a dot on folders? Currently compressing a folder such as /example.com-2009/ results in an archive of example.7z instead of example.com-2009.7z
I really appreciate all the hard work you've put into this, it makes XYplorer a first class citizen wrt file compression (something it most certainly isn't out of the box), but I don't suppose you have any idea how to solve this final issue do you?

Looking through the script I notice something about stripping the last .ext from filename - is it possible to prevent this from firing on directories?
Used to update to the latest beta every day. Now I have children instead…
Windows 10 Pro x64 (everywhere except phone…)

Plastic
Posts: 36
Joined: 08 Sep 2008 00:18

Re: Another 7-zip script

Post by Plastic »

Okay, your suggestions made sense + were easy = it's done ;)

About the ". in folder name" bug, it should already skip the "remove extension" part for folder, but I guess it doesn't work correctly...
I'll have a look.

Plastic
Posts: 36
Joined: 08 Sep 2008 00:18

Re: Another 7-zip script

Post by Plastic »

WARNING: There was a bug in the last release making the script do nothing for small filepath (:-/). it's corrected now.

About the ". in folder name bug", I have already found the cause. I am using the <curbase> internal variable to get the basename of the current item when doing 1 archive for 1 item ("Zip item" or "7-zip item"), but it seems this var also remove "extensions" on folder.

I'll do a fix shortly, but in the mean time, you can use the "1archive/item" option, as this one use my custom basename function.

Regards


EDIT: ". in folder name bug" corrected

lukescammell
Posts: 744
Joined: 28 Jul 2006 13:15
Location: Kent, UK
Contact:

Re: Another 7-zip script

Post by lukescammell »

Brilliant and thanks! I noticed you're using the 7zip @filelist command now, good idea!

I've been having a little read through the 7-Zip 9.09 beta help file and looking at some of the info on the various compression schemes that it supports. PPMD looks particularly good at compressing text files and LZMA2 looks good at larger compression jobs, so I'll come up with a few new settings that could be added to the script if you'd like. I suppose a decent tar, gzip and bz2 setting would also be sensible just for completeness...

Don, remind me again why XYplorer can't bundle 7za.exe command line or use the dll?
Used to update to the latest beta every day. Now I have children instead…
Windows 10 Pro x64 (everywhere except phone…)

lukescammell
Posts: 744
Joined: 28 Jul 2006 13:15
Location: Kent, UK
Contact:

Re: Another 7-zip script

Post by lukescammell »

I've been extending your most excellent script for my own nefarious purposes and I thought this might be of interest to <del>everyone</del> no one!

Vanilla 1.1.10 - 2009-11-23
Downloaded from http://vanillaforums.org/download/
http://vanillaforums.org/uploads/4IAUJPV5PVRW.zip

Code: Select all

Name                                 Size
---------------------------------  --------
vanilla-1.1.10                     1,735 KB  <-- Uncompressed Directory
vanilla-1.1.10.zip                   560 KB  <-- Original Download
vanilla-1.1.10.RIGHT.CLICK.zip       550 KB
vanilla-1.1.10.DEFLATE.zip           545 KB
vanilla-1.1.10.BZip2.Ultra.GUI.7z    323 KB
vanilla-1.1.10.RIGHT.CLICK.7z        315 KB
vanilla-1.1.10.LZMA2.7z              312 KB
vanilla-1.1.10.Ultra.GUI.7z          312 KB
vanilla-1.1.10.LZMA.7z               311 KB
vanilla-1.1.10.PPMd.7z               273 KB
XYplorer 8.70.0100 - 2009-12-08
Downloaded from http://xyplorer.com/download.htm
http://xyplorer.com/download/xyplorer_f ... nstall.zip
http://xyplorer.com/download/xyplorer_f ... nstall.rar

Code: Select all

Name                                                     Size  
-----------------------------------------------------  --------
xyplorer_full_8.70.0100_noinstall                      4,366 KB  <-- Uncompressed Directory
xyplorer_full_8.70.0100_noinstall.zip                  1,855 KB  <-- Original Download
xyplorer_full_8.70.0100_noinstall.RIGHT.CLICK.zip      1,800 KB
xyplorer_full_8.70.0100_noinstall.DEFLATE.zip          1,787 KB
xyplorer_full_8.70.0100_noinstall.rar                  1,626 KB  <-- Original Download
xyplorer_full_8.70.0100_noinstall.BZip2.Ultra.GUI.7z   1,607 KB
xyplorer_full_8.70.0100_noinstall.RIGHT.CLICK.7z       1,500 KB
xyplorer_full_8.70.0100_noinstall.PPMd.7z              1,496 KB
xyplorer_full_8.70.0100_noinstall.Ultra.GUI.7z         1,475 KB
xyplorer_full_8.70.0100_noinstall.LZMA.7z              1,474 KB
xyplorer_full_8.70.0100_noinstall.LZMA2.7z             1,471 KB

And then, just for shits and giggles I thought I'd UPX compress XYplrer.exe using "upx *.* --brute --nrv2d" with UPX 3.04w
http://upx.sourceforge.net/

Code: Select all

Name                                                          Size
----------------------------------------------------------  --------
xyplorer_full_8.70.0100_noinstall_UPX.PPMd.7z               1,534 KB
xyplorer_full_8.70.0100_noinstall_UPX                       1,534 KB  <-- UPX Compressed Directory
xyplorer_full_8.70.0100_noinstall_UPX.Ultra.GUI.7z          1,507 KB
xyplorer_full_8.70.0100_noinstall_UPX.LZMA.7z               1,507 KB
xyplorer_full_8.70.0100_noinstall_UPX.RIGHT.CLICK.7z        1,507 KB
xyplorer_full_8.70.0100_noinstall_UPX.BZip2.Ultra.GUI.7z    1,497 KB
xyplorer_full_8.70.0100_noinstall_UPX.RIGHT.CLICK.zip       1,494 KB
xyplorer_full_8.70.0100_noinstall_UPX.DEFLATE.zip           1,493 KB  <-- Zip DEFLATE beating PPMd, BZip2 and LZMA!
xyplorer_full_8.70.0100_noinstall_UPX.LZMA2.7z              1,490 KB

Code: Select all

Name                   Size  
-------------------  --------
XYplorer.exe         3,896 KB  <-- Original Executable
XYplorer.exe         1,064 KB  <-- UPX Compressed Executable
Here's the script in full, most people will still want the original and not this :mrgreen: There are probably some horrible bits f code in there that may quite possibly break anything and everything since I am a cut'n'paste coder :P

Code: Select all

/* Title:	Another 7-Zip Script
 * Author:	Plastic - http://www.xyplorer.com/xyfc/memberlist.php?mode=viewprofile&u=778
 * URL:		http://www.xyplorer.com/xyfc/viewtopic.php?f=7&t=3487
 * 
 * Modified by:	Luke Scammell - http://www.xyplorer.com/xyfc/memberlist.php?mode=viewprofile&u=273
 * URL:		http://www.xyplorer.com/xyfc/viewtopic.php?f=7&t=3487&p=40715#p40715
 */

/** CONFIG **/
// 7zip executable
"_get_config"
	global $g_exec, $g_filelist_path, $g_delete_filelist, $g_options_global_add;

	//$g_exec = "C:\Program Files\7-Zip\7z.exe";
	$g_exec = "<xypath>\..\7-ZipPortable\App\7-Zip64\7z.exe";

	$g_filelist_path = "<xypath>\Scripts\7zip.filelist";

	// Set to 1 to force deletion of the filelist after completion.
	// WARNING: This render the script modal
	$g_delete_filelist = 0;
	
	// Options that are added to all compression methods
	// -xr! is used to exclude (x) recursively (r) these file types
	// nppBackup is an optional directory created by Notepad++ that stores a version of the file you're editing every time you save
	$g_options_global_add = "-ssc -ssw -xr!nppBackup -xr!Thumbs.db -xr!ehthumbs_vista.db -xr!.DS_Store -xr!._* -xr!.Trashes -xr!__MACOSX"


// 7zip option by format

/************* 7z Default LZMA compression, good for general use *************/
"_get_7z_LZMA_add_options"
	global $g_options;
	$g_options = "-m0=LZMA:mc=256:fb=128 -mx -ms -mmt";

"_get_7z_extract_options"
	global $g_options;
	$g_options = "";

/************* 7z LZMA2 compression, faster and better for large files *************/
"_get_7z_LZMA2_add_options"
	global $g_options;
	$g_options = "-m0=LZMA2 -mx=8 -ms -mmt";

"_get_7z_LZMA2_extract_options"
	global $g_options;
	$g_options = "";

/************* 7z PPMD compression, better for text *************/
"_get_7z_PPMd_add_options"
	global $g_options;
	$g_options = "-m0=PPMd:o32:mem192m";

"_get_7z_PPMd_extract_options"
	global $g_options;
	$g_options = "";

/************* Zip Default DEFLATE compression, most compatible *************/
"_get_zip_DEFLATE_add_options"
	global $g_options;
	$g_options = "-mx=9 -mm=Deflate";

"_get_zip_DEFLATE_extract_options"
	global $g_options;
	$g_options = "";

/************* Tar Archive, needed in order to use Gzip and Bzip2 *************/
"_get_tar__add_options"
	global $g_options;
	$g_options = "";

"_get_tar__extract_options"
	global $g_options;
	$g_options = "";

/************* gzip compression, old Linux/*nix standard *************/
"_get_gzip__add_options"
	global $g_options;
	$g_options = "-mx=9";

"_get_gzip__extract_options"
	global $g_options;
	$g_options = "";

/************* Bzip2 compression, current Linux/*nix standard *************/
"_get_bzip2__add_options"
	global $g_options;
	$g_options = "-mx=9";

"_get_bzip2__extract_options"
	global $g_options;
	$g_options = "";

/************* xz compression, new Linux/*nix standard *************/
"_get_xz__add_options"
	global $g_options;
	$g_options = "-mx=9";

"_get_xz__extract_options"
	global $g_options;
	$g_options = "";


/** MENU **/
"Zi&p max compatible item(s) : zip"
	sub "_init";

	global $g_archive_type, $g_compression_method;
	$g_archive_type = "zip";
	$g_compression_method = "DEFLATE";

	sub "_do_compress_1archive"

"Zip max compatible items (1 archive/item) : mzip"
	sub "_init";

	global $g_archive_type, $g_compression_method;
	$g_archive_type = "zip";
	$g_compression_method = "DEFLATE";

	sub "_do_compress_Narchives"
-

"7&z general item(s) : 7z"
	sub "_init";

	global $g_archive_type, $g_compression_method;
	$g_archive_type = "7z";
	$g_compression_method = "LZMA";

	sub "_do_compress_1archive"

"7z general items (1 archive/item) : m7z"
	sub "_init";

	global $g_archive_type, $g_compression_method;
	$g_archive_type = "7z";
	$g_compression_method = "LZMA";

	sub "_do_compress_Narchives"
-

"7z la&rge item(s) : 7zLZMA2"
	sub "_init";

	global $g_archive_type, $g_compression_method;
	$g_archive_type = "7z";
	$g_compression_method = "LZMA2";

	sub "_do_compress_1archive"

"7z large items (1 archive/item) : m7zLZMA2"
	sub "_init";

	global $g_archive_type, $g_compression_method;
	$g_archive_type = "7z";
	$g_compression_method = "LZMA2";

	sub "_do_compress_Narchives"
-

"7z &text item(s) : 7zppmd"
	sub "_init";

	global $g_archive_type, $g_compression_method;
	$g_archive_type = "7z";
	$g_compression_method = "PPMd";

	sub "_do_compress_1archive"

"7z text items (1 archive/item) : m7zppmd"
	sub "_init";

	global $g_archive_type, $g_compression_method;
	$g_archive_type = "7z";
	$g_compression_method = "PPMd";

	sub "_do_compress_Narchives"
-

"Tar item(s) : tar"
	sub "_init";

	global $g_archive_type, $g_compression_method;
	$g_archive_type = "tar";
	$g_compression_method = "";

	sub "_do_compress_1archive"
-

"gzip items (1 archive/item - no folders) : gzip"
	sub "_init";

	global $g_archive_type, $g_compression_method;
	$g_archive_type = "gzip";
	$g_compression_method = "";

	sub "_do_compress_Narchives"
-

"bzip2 items (1 archive/item - no folders) : bzip2"
	sub "_init";

	global $g_archive_type, $g_compression_method;
	$g_archive_type = "bzip2";
	$g_compression_method = "";

	sub "_do_compress_Narchives"
-

"xz items (1 archive/item - no folders) : xz"
	sub "_init";

	global $g_archive_type, $g_compression_method;
	$g_archive_type = "xz";
	$g_compression_method = "";

	sub "_do_compress_Narchives"
-

"Extract &here : extract"
	sub "_init";

	global $g_list_items, $g_list_length, $g_env_curpath;
	global $param_extract_path, $param_item;
	global $param_item_path, $out_item_name;

	$i = 1;
	while ($i <= $g_list_length) {
		$item_path = gettoken($g_list_items, $i, "|");

		$param_extract_path = "$g_env_curpath";
		$param_item = $item_path;
		sub "_do_extract";

		$i = $i + 1;
	}

"E&xtract in <subfolder> : extract_subfolder"
	sub "_init";

	global $g_list_items, $g_list_length, $g_env_curpath;
	global $param_extract_path, $param_item;
	global $param_item_path, $out_item_name;

	$i = 1;
	while ($i <= $g_list_length) {
		$item_path = gettoken($g_list_items, $i, "|");

		$param_item_path = $item_path;
		sub "_get_item_basename";
		// Return $out_item_name

		$param_extract_path = "$g_env_curpath\$out_item_name";
		$param_item = $item_path;
		sub "_do_extract";

		$i = $i + 1;
	}

-
"Edit script : edit"
   self $scriptFile, file;
   Open $scriptFile;
-
"Cancel"

/** PRIVATE **/
"_init"
	sub "_get_config";
	sub "_get_list";
	sub "_get_env";
	sub "_check";

"_get_list"
	// Get selected list items
	global $g_list_items, $g_list_length;
	$g_list_items = getinfo("SelectedItemsPathNames", "|") . "|";
	$g_list_length = getinfo("CountSelected");

	$i = 1;
	while ($i <= $g_list_length) {
		$item_path = gettoken($g_list_items, $i, "|");
		if (exists($item_path) == 2) { // Folder -> Add "\" to work on folder content
			replace $g_list_items, $g_list_items, "$item_path|", "$item_path\|";
		}
		$i = $i + 1;
	}

	substr $g_list_items, $g_list_items, 0, -1; // Delete last separator

"_get_env"
	// Get useful environment variables
	global $g_env_curpath, $g_env_curfolder, $g_env_curbase;
	$g_env_curpath = <curpath>;
	$g_env_curfolder = <curfolder>;

"_check"
	// Check if parameters are ok
	global $g_list_length;
	end $g_list_length <= 0;

"_get_item_basename"
	// Get a full path in $param_item_path, and returne base in $out_item_name
	// ex: c:\dir1\dir2\base.ext => base
	//	  c:\dir1\dir2\ => dir2
	global $param_item_path;
	global $out_item_name;

	$out_item_name = $param_item_path;

	// Detect folder path. Folder path ends with "\" so we have to find if the path ends with a "\"
	// No reverse find for now, so we add a special character to do the trick
	$out_item_name = $out_item_name . "{";
	strpos $is_folder, $out_item_name, "\{";
	if ($is_folder >= 0) {
		$is_folder = 1;
		// Remove special char with last "\"
		replace $out_item_name, $out_item_name, "\{";
	}
	else {
		$is_folder = 0;
		// Remove special char
		replace $out_item_name, $out_item_name, "{";
	}

	strpos $last_slash_index, $out_item_name, "\";
	$last = $last_slash_index;
	while ($last_slash_index >= 0 && $last_slash_index < strlen($out_item_name)) {
		strpos $last_slash_index, $out_item_name, "\", $last_slash_index + 1;
		if ($last_slash_index != -1) {
			$last = $last_slash_index;
		}
	}
	$last_slash_index = $last;

	if ($last_slash_index != -1) {
		substr $out_item_name, $out_item_name, $last_slash_index + 1;
	}

	if ($is_folder == 0) {
		// Folder -> Do nothings
		// File -> Remove extension
		strpos $last_dot_index, $out_item_name, ".";
		$last = $last_dot_index;
		while ($last_dot_index >= 0 && $last_dot_index < strlen($out_item_name)) {
			strpos $last_dot_index, $out_item_name, ".", $last_dot_index + 1;
			if ($last_dot_index != -1) {
				$last = $last_dot_index;
			}
		}
		$last_dot_index = $last;

		if ($last_dot_index > 0) {
			strlen $length, $out_item_name;
			$last_dot_right = $last_dot_index - $length;
			substr $out_item_name, $out_item_name, 0, $last_dot_right;
		}
	}

"_do_compress_1archive"
	// 1 archive for all items
	global $g_list_items, $g_list_length, $g_env_curpath, $g_env_curfolder;
	global $g_filelist_path;
	global $param_items, $param_archive_path, $param_archive_name, $param_use_filelist;

	$param_archive_path = $g_env_curpath;

	if ($g_list_length == 1) {
		// 1 item -> archive name = item name
		global $param_item_path, $out_item_name;
		$param_item_path = $g_list_items;
		sub "_get_item_basename";
		$param_archive_name = $out_item_name;
	}
	else {
		// N items -> archive name = folder name
		$param_archive_name = $g_env_curfolder;
	}

	// Use file list g_list_items is too big
	if (strlen($g_list_items) > 1000) {
		replace $listfile_data, $g_list_items, "|", chr(10);
		if (writefile($g_filelist_path, $listfile_data) != 0) {
			$param_use_filelist = 1;
			sub "_do_compress";
		}
	} else {
		$g_filelist_path = "";
		replace $param_items, $g_list_items, "|", '" "';
		sub "_do_compress";
	}

"_do_compress_Narchives"
	// N archives : 1 archive per item
	global $g_list_items, $g_list_length, $g_env_curpath;
	global $param_items, $param_archive_path, $param_archive_name;
	global $param_item_path, $out_item_name;

	$param_archive_path = $g_env_curpath;

	$i = 1;
	while ($i <= $g_list_length) {
		$item_path = gettoken($g_list_items, $i, "|");

		$param_item_path = $item_path;
		sub "_get_item_basename";
		// Return $out_item_name

		$param_archive_name = $out_item_name;
		$param_items = $item_path;
		sub "_do_compress";

		$i = $i + 1;
	}

// ** Simple actions **
"_do_compress"
	global $g_exec, $g_archive_type, $g_compression_method, $g_options, $g_options_global_add;
	global $param_items, $param_archive_path, $param_archive_name, $param_use_filelist;
	global $g_filelist_path, $g_delete_filelist;
	sub "_get_" . $g_archive_type . "_" . $g_compression_method . "_add_options";

	if ($param_use_filelist == 1 and $g_filelist_path != "") {
		if ($g_delete_filelist == 1) {
			// Using file list parameter, we should wait for the command to end in order to delete the file
			run """$g_exec"" a -t$g_archive_type ""$param_archive_path\$param_archive_name.$g_compression_method.$g_archive_type"" $g_options $g_options_global_add @""$g_filelist_path""", , 1;
			delete 1,0,$g_filelist_path;
		}
		else {
			run """$g_exec"" a -t$g_archive_type ""$param_archive_path\$param_archive_name.$g_compression_method.$g_archive_type"" $g_options $g_options_global_add @""$g_filelist_path""", , 0;
		}
	}
	else {
		run """$g_exec"" a -t$g_archive_type ""$param_archive_path\$param_archive_name.$g_compression_method.$g_archive_type"" $g_options $g_options_global_add ""$param_items""", , 0;
	}

"_do_extract"
	global $g_exec, $g_options;
	global $param_extract_path, $param_item;
	// TODO Detect archive type
	$g_archive_type = "7z";
	sub "_get_" . $g_archive_type . "_extract_options";
	run """$g_exec"" x -o""$param_extract_path"" $g_options ""$param_item""", , 0;
Edit: fixed show-stopper bug that new scripting functionality brought in recently.
Last edited by lukescammell on 16 Aug 2010 14:50, edited 2 times in total.
Used to update to the latest beta every day. Now I have children instead…
Windows 10 Pro x64 (everywhere except phone…)

lukescammell
Posts: 744
Joined: 28 Jul 2006 13:15
Location: Kent, UK
Contact:

Re: Another 7-zip script

Post by lukescammell »

Plastic, you need to update your script, replacing line 123:

Code: Select all

        $g_list_items = getinfo("SelectedItemsPathNames", "|");
with:

Code: Select all

        $g_list_items = getinfo("SelectedItemsPathNames", "|") . "|";
This is why.

I've updated my modified script above.

Also, just a gentle suggestion, but what about adding the following to the beginning of your script so it's easy to find where it came from in the future!

Code: Select all

/* Title:	Another 7-Zip Script
 * Author:	Plastic - http://www.xyplorer.com/xyfc/memberlist.php?mode=viewprofile&u=778
 * URL:		http://www.xyplorer.com/xyfc/viewtopic.php?f=7&t=3487
 */

    /** CONFIG **/
etc...
Used to update to the latest beta every day. Now I have children instead…
Windows 10 Pro x64 (everywhere except phone…)

lukescammell
Posts: 744
Joined: 28 Jul 2006 13:15
Location: Kent, UK
Contact:

Re: Another 7-zip script

Post by lukescammell »

I made some more updates some time ago now when there were some more changes to scripting. The script still mostly works, but I'm calling out for help from the general community on fixing the last bug I've been unable to resolve.

The Bug [resolved!]
When I compress a single file, the resulting archive is no longer named like the original file, but "out_item_.EXT".

This doesn't happen on directories, and it doesn't happen if you zip up a bunch of files or a mix of files and directories (then it's, correctly, given the name of the containing directory).

So, please will some coding guru help fix this? I use this script multiple times every day (hour?) and while this isn't a show stopper, it certainly removes a lot of efficiency in using the script in the first place.

Thanks!

Edit: highend provided the fix (a typo) and I've updated the script below with the correction. Thanks highend!

Code: Select all

/* Title:       Another 7-Zip Script
 * Filename:    7zip.xys
 * Author:      Plastic – http://www.xyplorer.com/xyfc/memberlist.php?mode=viewprofile&u=778
 * URL:         http://www.xyplorer.com/xyfc/viewtopic.php?f=7&t=3487
 * 
 * Modified by: Luke Scammell – http://www.xyplorer.com/xyfc/memberlist.php?mode=viewprofile&u=273
 * URL:         http://www.xyplorer.com/xyfc/viewtopic.php?f=7&t=3487&p=40715#p40715
 * Updated:     2015-03-03_11-58-02
 */

/** CONFIG **/
// 7zip executable
"_get_config"
	global $g_exec, $g_filelist_path, $g_delete_filelist, $g_options_global_add;

	//$g_exec = "%ProgramFiles%\7-Zip\7z.exe"; // LS 2015-03-03 10:47:20 — Yes this environment variable works nicely thanks.
	$g_exec = "<xypath>\..\7-ZipPortable\App\7-Zip64\7z.exe";

	$g_filelist_path = "<xypath>\Scripts\7zip.filelist";

	// Set to 1 to force deletion of the filelist after completion.
	// WARNING: This render the script modal
	$g_delete_filelist = 0;
	
	// Options that are added to all compression methods
	// -xr! is used to exclude (x) recursively (r) these file types
	// nppBackup is an optional directory created by Notepad++ that stores a version of the file you're editing every time you save
	// _backup, _install are folders I use that I generally don't want to archive.
	$g_options_global_add = "-ssc -ssw -xr!nppBackup -xr!_backup* -xr!_install* -xr!.komodoproject -xr!*.komodoproject -xr!Thumbs.db -xr!ehthumbs_vista.db -xr!.DS_Store -xr!._* -xr!.Trashes -xr!__MACOSX"


// 7zip option by format

/************* 7z Default LZMA compression, good for general use *************/
"_get_7z_LZMA_add_options"
	global $g_options;
	$g_options = "-m0=LZMA:mc=256:fb=128 -mx -ms -mmt";

"_get_7z_extract_options"
	global $g_options;
	$g_options = "";

/************* 7z LZMA2 compression, faster and better for large files *************/
"_get_7z_LZMA2_add_options"
	global $g_options;
	$g_options = "-m0=LZMA2 -mx=8 -ms -mmt";

"_get_7z_LZMA2_extract_options"
	global $g_options;
	$g_options = "";

/************* 7z PPMD compression, better for text *************/
"_get_7z_PPMd_add_options"
	global $g_options;
	$g_options = "-m0=PPMd:o32:mem192m";

"_get_7z_PPMd_extract_options"
	global $g_options;
	$g_options = "";

/************* Zip Default DEFLATE compression, most compatible *************/
"_get_zip_DEFLATE_add_options"
	global $g_options;
	$g_options = "-mx=9 -mm=Deflate";

"_get_zip_DEFLATE_extract_options"
	global $g_options;
	$g_options = "";

/************* Tar Archive, needed in order to use Gzip and Bzip2 *************/
"_get_tar__add_options"
	global $g_options;
	$g_options = "";

"_get_tar__extract_options"
	global $g_options;
	$g_options = "";

/************* gzip compression, old Linux/*nix standard *************/
"_get_gzip__add_options"
	global $g_options;
	$g_options = "-mx=9";

"_get_gzip__extract_options"
	global $g_options;
	$g_options = "";

/************* Bzip2 compression, current Linux/*nix standard *************/
"_get_bzip2__add_options"
	global $g_options;
	$g_options = "-mx=9";

"_get_bzip2__extract_options"
	global $g_options;
	$g_options = "";

/************* xz compression, new Linux/*nix standard *************/
"_get_xz__add_options"
	global $g_options;
	$g_options = "-mx=9";

"_get_xz__extract_options"
	global $g_options;
	$g_options = "";


/** MENU **/
"Zi&p max compatible item(s) : zip"
	sub "_init";

	global $g_archive_type, $g_compression_method;
	$g_archive_type = "zip";
	$g_compression_method = "DEFLATE";

	sub "_do_compress_1archive"

"Zip max compatible items (1 archive/item) : mzip"
	sub "_init";

	global $g_archive_type, $g_compression_method;
	$g_archive_type = "zip";
	$g_compression_method = "DEFLATE";

	sub "_do_compress_Narchives"
-

"7&z general item(s) : 7z"
	sub "_init";

	global $g_archive_type, $g_compression_method;
	$g_archive_type = "7z";
	$g_compression_method = "LZMA";

	sub "_do_compress_1archive"

"7z general items (1 archive/item) : m7z"
	sub "_init";

	global $g_archive_type, $g_compression_method;
	$g_archive_type = "7z";
	$g_compression_method = "LZMA";

	sub "_do_compress_Narchives"
-

"7z la&rge item(s) : 7zLZMA2"
	sub "_init";

	global $g_archive_type, $g_compression_method;
	$g_archive_type = "7z";
	$g_compression_method = "LZMA2";

	sub "_do_compress_1archive"

"7z large items (1 archive/item) : m7zLZMA2"
	sub "_init";

	global $g_archive_type, $g_compression_method;
	$g_archive_type = "7z";
	$g_compression_method = "LZMA2";

	sub "_do_compress_Narchives"
-

"7z &text item(s) : 7zppmd"
	sub "_init";

	global $g_archive_type, $g_compression_method;
	$g_archive_type = "7z";
	$g_compression_method = "PPMd";

	sub "_do_compress_1archive"

"7z text items (1 archive/item) : m7zppmd"
	sub "_init";

	global $g_archive_type, $g_compression_method;
	$g_archive_type = "7z";
	$g_compression_method = "PPMd";

	sub "_do_compress_Narchives"
-

"Tar item(s) : tar"
	sub "_init";

	global $g_archive_type, $g_compression_method;
	$g_archive_type = "tar";
	$g_compression_method = "";

	sub "_do_compress_1archive"
-

"gzip items (1 archive/item - no folders) : gzip"
	sub "_init";

	global $g_archive_type, $g_compression_method;
	$g_archive_type = "gzip";
	$g_compression_method = "";

	sub "_do_compress_Narchives"
-

"bzip2 items (1 archive/item - no folders) : bzip2"
	sub "_init";

	global $g_archive_type, $g_compression_method;
	$g_archive_type = "bzip2";
	$g_compression_method = "";

	sub "_do_compress_Narchives"
-

"xz items (1 archive/item - no folders) : xz"
	sub "_init";

	global $g_archive_type, $g_compression_method;
	$g_archive_type = "xz";
	$g_compression_method = "";

	sub "_do_compress_Narchives"
-

"Extract &here|:visualfilter : extract"
	sub "_init";

	global $g_list_items, $g_list_length, $g_env_curpath;
	global $param_extract_path, $param_item;
	global $param_item_path, $out_item_name;

	$i = 1;
	while ($i <= $g_list_length) {
		$item_path = gettoken($g_list_items, $i, "|");

		$param_extract_path = "$g_env_curpath";
		$param_item = $item_path;
		sub "_do_extract";

		$i = $i + 1;
	}

"E&xtract in <subfolder>|:hotlist : extract_subfolder"
	sub "_init";

	global $g_list_items, $g_list_length, $g_env_curpath;
	global $param_extract_path, $param_item;
	global $param_item_path, $out_item_name;

	$i = 1;
	while ($i <= $g_list_length) {
		$item_path = gettoken($g_list_items, $i, "|");

		$param_item_path = $item_path;
		sub "_get_item_basename";
		// Return $out_item_name

		$param_extract_path = "$g_env_curpath\$out_item_name";
		$param_item = $item_path;
		sub "_do_extract";

		$i = $i + 1;
	}

-
"Edit &script|<xypath>\Scripts\icons\famfamfam_silk_icons_v013\script_edit.ico"
	Open self('file');
-
"Cancel"

/** PRIVATE **/
"_init"
	sub "_get_config";
	sub "_get_list";
	sub "_get_env";
	sub "_check";

"_get_list"
	// Get selected list items
	global $g_list_items, $g_list_length;
	$g_list_items = getinfo("SelectedItemsPathNames", "|") . "|";
	$g_list_length = getinfo("CountSelected");

	$i = 1;
	while ($i <= $g_list_length) {
		$item_path = gettoken($g_list_items, $i, "|");
		if (exists($item_path) == 2) { // Folder -> Add "\" to work on folder content
			$g_list_items = Replace($g_list_items, "$item_path|", "$item_path\|");
		}
		$i = $i + 1;
	}

	$g_list_items = substr($g_list_items, 0, -1); // Delete last separator

"_get_env"
	// Get useful environment variables
	global $g_env_curpath, $g_env_curfolder, $g_env_curbase;
	$g_env_curpath = <curpath>;
	$g_env_curfolder = <curfolder>;

"_check"
	// Check if parameters are ok
	global $g_list_length;
	end $g_list_length <= 0;

"_get_item_basename"
	// Get a full path in $param_item_path, and returne base in $out_item_name
	// ex: c:\dir1\dir2\base.ext => base
	//	  c:\dir1\dir2\ => dir2
	global $param_item_path;
	global $out_item_name;

	$out_item_name = $param_item_path;

	// Detect folder path. Folder path ends with "\" so we have to find if the path ends with a "\"
	// No reverse find for now, so we add a special character to do the trick
	$out_item_name = $out_item_name . "{";
	$is_folder = strpos($out_item_name, "\{");
	if ($is_folder >= 0) {
		$is_folder = 1;
		// Remove special char with last "\"
		$out_item_name = Replace($out_item_name, "\{");
	}
	else {
		$is_folder = 0;
		// Remove special char
		$out_item_name = Replace($out_item_name, "{");
	}

	$last_slash_index = strpos($out_item_name, "\");
	$last = $last_slash_index;
	while ($last_slash_index >= 0 && $last_slash_index < strlen($out_item_name)) {
		$last_slash_index = strpos($out_item_name, "\", $last_slash_index + 1);
		if ($last_slash_index != -1) {
			$last = $last_slash_index;
		}
	}
	$last_slash_index = $last;

	if ($last_slash_index != -1) {
		$out_item_name = substr($out_item_name, $last_slash_index + 1);
	}

	if ($is_folder == 0) {
		// Folder -> Do nothings
		// File -> Remove extension
		$last_dot_index = strpos($out_item_name, ".");
		$last = $last_dot_index;
		while ($last_dot_index >= 0 && $last_dot_index < strlen($out_item_name)) {
			$last_dot_index = strpos($out_item_name, ".", $last_dot_index + 1);
			if ($last_dot_index != -1) {
				$last = $last_dot_index;
			}
		}
		$last_dot_index = $last;

		if ($last_dot_index > 0) {
			$length = strlen($out_item_name);
			$last_dot_right = $last_dot_index - $length;
			$out_item_name = substr($out_item_name, 0, $last_dot_right);
		}
	}

"_do_compress_1archive"
	// 1 archive for all items
	global $g_list_items, $g_list_length, $g_env_curpath, $g_env_curfolder;
	global $g_filelist_path;
	global $param_items, $param_archive_path, $param_archive_name, $param_use_filelist;

	$param_archive_path = $g_env_curpath;

	if ($g_list_length == 1) {
		// 1 item -> archive name = item name
		global $param_item_path, $out_item_name;
		$param_item_path = $g_list_items;
		sub "_get_item_basename";
		$param_archive_name = $out_item_name;
	}
	else {
		// N items -> archive name = folder name
		$param_archive_name = $g_env_curfolder;
	}

	// Use file list g_list_items is too big
	if (strlen($g_list_items) > 1000) {
		$listfile_data = Replace($g_list_items, "|", chr(10));
		if (writefile($g_filelist_path, $listfile_data) != 0) {
			$param_use_filelist = 1;
			sub "_do_compress";
		}
	} else {
		$g_filelist_path = "";
		$param_items = Replace($g_list_items, "|", '" "');
		sub "_do_compress";
	}

"_do_compress_Narchives"
	// N archives : 1 archive per item
	global $g_list_items, $g_list_length, $g_env_curpath;
	global $param_items, $param_archive_path, $param_archive_name;
	global $param_item_path, $out_item_name;

	$param_archive_path = $g_env_curpath;

	$i = 1;
	while ($i <= $g_list_length) {
		$item_path = gettoken($g_list_items, $i, "|");

		$param_item_path = $item_path;
		sub "_get_item_basename";
		// Return $out_item_name

		$param_archive_name = $out_item_name;
		$param_items = $item_path;
		sub "_do_compress";

		$i = $i + 1;
	}

// ** Simple actions **
"_do_compress"
	global $g_exec, $g_archive_type, $g_compression_method, $g_options, $g_options_global_add;
	global $param_items, $param_archive_path, $param_archive_name, $param_use_filelist;
	global $g_filelist_path, $g_delete_filelist;
	sub "_get_" . $g_archive_type . "_" . $g_compression_method . "_add_options";

	if ($param_use_filelist == 1 and $g_filelist_path != "") {
		if ($g_delete_filelist == 1) {
			// Using file list parameter, we should wait for the command to end in order to delete the file
			run """$g_exec"" a -t$g_archive_type ""$param_archive_path\$param_archive_name.$g_compression_method.$g_archive_type"" $g_options $g_options_global_add @""$g_filelist_path""", , 1;
			delete 1,0,$g_filelist_path;
		}
		else {
			run """$g_exec"" a -t$g_archive_type ""$param_archive_path\$param_archive_name.$g_compression_method.$g_archive_type"" $g_options $g_options_global_add @""$g_filelist_path""", , 0;
		}
	}
	else {
		run """$g_exec"" a -t$g_archive_type ""$param_archive_path\$param_archive_name.$g_compression_method.$g_archive_type"" $g_options $g_options_global_add ""$param_items""", , 0;
	}

"_do_extract"
	global $g_exec, $g_options;
	global $param_extract_path, $param_item;
	// TODO Detect archive type
	$g_archive_type = "7z";
	sub "_get_" . $g_archive_type . "_extract_options";
	run """$g_exec"" x -o""$param_extract_path"" $g_options ""$param_item""", , 0;
Last edited by lukescammell on 03 Mar 2015 13:35, edited 3 times in total.
Used to update to the latest beta every day. Now I have children instead…
Windows 10 Pro x64 (everywhere except phone…)

Post Reply