I made a simple script with 7-zip which zip the selected files.
The behavior is as follows :
- 1 file FILE selected -> FILE.zip
- several files selected (FOLDER/file1, FOLDER/file2, ...) -> FOLDER.zip
In clear, if one file is selected, the zip take the name of the file, if more, the zip take the name of the folder.
====================
NEW VERSION (2010/08/16):
- Centralization of the executable path in _get_exec
- Extraction of single/multiple archive in current folder or in a subfolder (using the archive name)
- Several compression type (currently zip and 7z, easy to add more)
- Ability to compress in 1 archive per item (file or folder)
- Rewritten to use new conditional and loop syntax (at last !)
- Added some suggestions made by lukescammell
- Added options by archive type
- Corrected a bug which could add some unwanted items to the archives (apparently caused by -r option)
- For whose who compress really long filepath, the script use 7-zip file list option when all the filepath are longer than 1000 characters. Default file name and deletion of the filelist are configurable.
Please be aware that by setting $g_delete_list_file to 1, the script become modal (you can't do other things with XY while it runs) as I have to wait for completion before the deletion.
- Corrected ". in folder name" bug
- Adapted for change in beta v9.40.0007 (DO NOT UPDATE IF YOU USE A PREVIOUS VERSION)
- Added a header
These last changes are thanks to lukescammell. Thank you guy!
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
* Last update : 2010-08-18
*/
/** CONFIG **/
// 7zip executable
"_get_config"
global $g_exec, $g_filelist_path, $g_delete_filelist;
//$g_exec = "C:\Program Files\7-Zip\7z.exe";
$g_exec = "<xypath>\..\7-ZipPortable\App\7-Zip\7z.exe";
$g_filelist_path = "<xyscripts>\7zip.filelist";
// Set to 1 to force deletion of the filelist after completion.
// WARNING: This render the script modal
$g_delete_filelist = 0;
// 7zip option by format (Notice: -r option is unnecessary)
"_get_7z_add_options"
global $g_options;
$g_options = "-mx=9 -ms=on -mmt=4 -ssw";
"_get_7z_extract_options"
global $g_options;
$g_options = "";
"_get_zip_add_options"
global $g_options;
$g_options = "";
"_get_zip_extract_options"
global $g_options;
$g_options = "";
/** MENU **/
"Zip item(s) : zip"
sub "_init";
global $g_archive_type;
$g_archive_type = "zip";
sub "_do_compress_1archive"
"Zip items (1 archive/item) : mzip"
sub "_init";
global $g_archive_type;
$g_archive_type = "zip";
sub "_do_compress_Narchives"
"7z item(s) : 7z"
sub "_init";
global $g_archive_type;
$g_archive_type = "7z";
sub "_do_compress_1archive"
"7z items (1 archive/item) : m7z"
sub "_init";
global $g_archive_type;
$g_archive_type = "7z";
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;
}
"Extract 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_options;
global $param_items, $param_archive_path, $param_archive_name, $param_use_filelist;
global $g_filelist_path, $g_delete_filelist;
sub "_get_" . $g_archive_type . "_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_archive_type"" $g_options @""$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_archive_type"" $g_options @""$g_filelist_path""", , 0;
}
}
else {
run """$g_exec"" a -t$g_archive_type ""$param_archive_path\$param_archive_name.$g_archive_type"" $g_options ""$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;
Have fun!