StrokesPlus.net
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
soooulp  
#1 Posted : Saturday, December 25, 2021 2:41:19 PM(UTC)
soooulp

Rank: Advanced Member

Reputation:

Groups: Approved
Joined: 4/23/2020(UTC)
Posts: 105
China

Thanks: 37 times
Was thanked: 6 time(s) in 5 post(s)
It will extract an archive including .zip .rar or other format supported by 7z that is selected in conditions.

The file(s) or folder(s) in the first level of an archive:

1 One file or just one folder (even with a password)
Extract to the current folder directly

2 Some files (with folders)
Extract to the new folder with the archive filename

Thank you Rob for your big support.

By the way, it can be set to a hotkey by a little change if you need it.

Code:
var result = 1;
var fullPath = "";
var tempfullPath = "";
var outdir = "";
var selectedFiles;
var isDesktop = false;
var success = false;
var passwordbool = false;
var archivepassword = "";
var checkpswbool = false;

if(action.Control.HWnd.ToInt32() == sp.DesktopWindowListView().HWnd.ToInt32()) {
    //Desktop
    selectedFiles = sp.GetSelectedFilesOnDesktop();
    isDesktop = true;
} else {
    //Not Desktop
    selectedFiles = sp.GetSelectedFilesInExplorer(action.Window.HWnd);
}

if(selectedFiles.Length == 1) {

    fullPath = selectedFiles[0];

    if(isDesktop) {
        fullPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) + "\\" + fullPath;
    }

    var fileNameNoExt = System.IO.Path.GetFileNameWithoutExtension(fullPath);
    var folderName = System.IO.Path.GetDirectoryName(fullPath);

    outdir = folderName + "\\" + fileNameNoExt;

    var path = fullPath.replaceAll("\\","\\\\")
    var p = new System.Diagnostics.Process();
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.FileName = "C:\\Program Files\\7-Zip\\7z.exe";
    p.StartInfo.CreateNoWindow = true;
    p.StartInfo.Arguments = "l \"" + path + "\" -x!*\\*";
    p.Start();
    var output = p.StandardOutput.ReadToEnd();
    p.WaitForExit();

    var lines = output.split(/\r\n|\n\r|\n|\r/);
    var filenum = 0;
    var foldnum = 0;
    var filepath = "";
    var str = "";

    for(var i = 0; i < lines.length - 1; i++) {
        if(/^[0-9]{4}-/.test(lines[i])) {
            if(i = lines.length - 2) {
                if(lines[lines.length - 2].indexOf("folders") > -1) {
                    foldnum = lines[lines.length - 2].split(', ')[1].split(' folders')[0];
                }
                if(lines[lines.length - 2].indexOf("files") > -1) {
                    filenum = lines[lines.length - 2].slice(53).split(' files')[0];
                }
            } 

        }
    }


    var q = new System.Diagnostics.Process();
    q.StartInfo.UseShellExecute = false;
    q.StartInfo.RedirectStandardOutput = true;
    q.StartInfo.FileName = "C:\\Program Files\\7-Zip\\7z.exe";
    q.StartInfo.CreateNoWindow = true;
    q.StartInfo.Arguments = "l -slt \"" + path + "\" -x!*\\*";
    q.Start();
    var output = q.StandardOutput.ReadToEnd();
    q.WaitForExit();

    var linesq = output.split(/\r\n|\n\r|\n|\r/);

    for(var i = 0; i < linesq.length - 1; i++) {
        if(linesq[i].indexOf("Encrypted = +") > -1) {
            passwordbool = true;
        }
    }

    if(passwordbool) {
        archivepassword = getPassword();
    }

    var r = new System.Diagnostics.Process();
    r.StartInfo.UseShellExecute = false;
    r.StartInfo.RedirectStandardOutput = true;
    r.StartInfo.FileName = "C:\\Program Files\\7-Zip\\7z.exe";
    r.StartInfo.CreateNoWindow = true;
    r.StartInfo.Arguments = "t -p\"" + archivepassword + "\" \"" + path + "\"" //-x!*\\* 排除文件夹内
    r.Start();
    var output = r.StandardOutput.ReadToEnd();
    r.WaitForExit();

    var linesr = output.split(/\r\n|\n\r|\n|\r/);

    for(var i = 0; i < linesr.length - 1; i++) {
        if(linesr[i].indexOf("Everything is Ok") > -1) {
            checkpswbool = true;
        }
    }

    StrokesPlus.Console.Log("filenum" + filenum + " " + "foldnum" + foldnum);
    if(checkpswbool) {
        if((filenum == 1 && foldnum == 0) || (filenum == 0 && foldnum == 1)) { //
            result = sp.RunProgram('C:\\Program Files\\7-Zip\\7z.exe', 
                                               `x "${fullPath}" -p"${archivepassword}" -o"${folderName}" -aou`, 
                                               'open', 'hidden', false, true, true); 
        } else if((filenum > 1 && foldnum == 0) || (filenum == 0 && foldnum > 1) || (filenum > 0 && foldnum > 0)) { //
            result = sp.RunProgram('C:\\Program Files\\7-Zip\\7z.exe', 
                                       `x "${fullPath}" -p"${archivepassword}" -o"${outdir}" -aou`, 
                                       'open', 'hidden', false, true, true);
        }
    }
} else if(selectedFiles.Length > 1) { 
    for(i = 0; i < selectedFiles.Length; i++)
    {
        tempfullPath = selectedFiles[i];

        if(isDesktop) {
            tempfullPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) + "\\" + tempfullPath;
        }
        var tempfolderName = System.IO.Path.GetDirectoryName(tempfullPath);
        result = sp.RunProgram('C:\\Program Files\\7-Zip\\7z.exe', 
                                           `x "${tempfullPath}" -o"${tempfolderName}" -aou`, 
                                           'open', 'hidden', false, true, true); 
    }
} else {

}

if(result == 0) { 
    success = true;
    sp.ShowBalloonTip("ZIP Extract", "Success", "info", 1000);
} 
if(!success) {
    if(!checkpswbool && archivepassword != "") {
        sp.ShowBalloonTip("ZIP Extract", "Error Password", "error", 1000);
    } else if(!checkpswbool && archivepassword == "") {
        
    } else {
        sp.ShowBalloonTip("ZIP Extract", "Failed", "error", 1000);
    }
}


function getPassword() {
    var input;
    var Forms = forms.System.Windows.Forms;
    var form = new Forms.Form;
    var tt = new System.Windows.Forms.ToolTip();
    var ht = sp.ForegroundWindow().Screen.Bounds.Height;
    var wd = sp.ForegroundWindow().Screen.Bounds.Width;

    form.StartPosition = Forms.FormStartPosition.Manual;
    form.FormBorderStyle = Forms.FormBorderStyle.FixedToolWindow;
    form.TopMost = true;
    form.ControlBox = false;
    form.ShowInTaskbar = false;
    form.Margin = new Forms.Padding(0);
    form.Padding = new Forms.Padding(1); 
    form.MinimumSize = new Size(100, 10);
    form.Size = new Size(300, 10);
    form.Location = new Point(wd/2 - 130, ht/2 - 60);
    form.AutoSize = true;
    form.GetType().GetProperty("DoubleBuffered",
            host.flags(clr.System.Reflection.BindingFlags.NonPublic,
            clr.System.Reflection.BindingFlags.Instance))
            .SetValue(form, true);
    form.BackColor = Color.WhiteSmoke;
    

    var label = new Forms.Label

    label.Text = "PSW:";
    label.Location = new Point(5, 8);
    label.Font = new System.Drawing.Font("微软雅黑", 8, FontStyle.Regular);
    label.Height = 20;
    label.Width = 45;
    //tt.SetToolTip(label, "Click to move");
    form.Controls.Add(label);

    var textBox = new Forms.TextBox;
    textBox.Size = new Size(150, 20);
    textBox.Location = new Point(50, 5);
    textBox.Font = new System.Drawing.Font("微软雅黑", 8, FontStyle.Regular);
    textBox.Text = "";
    form.Controls.Add(textBox);

    var okButton = new Forms.Button;
    okButton.DialogResult = Forms.DialogResult.OK;
    okButton.Name = "okButton";
    okButton.Size = new Size(50, 23);
    okButton.Text = "&OK";
    okButton.Font = new System.Drawing.Font("微软雅黑", 8, FontStyle.Regular);
    okButton.Location = new Point(205, 4);
    okButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
    okButton.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255);
    okButton.FlatAppearance.BorderSize = 0; 
    okButton.TabStop = false;
    form.Controls.Add(okButton);

    var cancelButton = new Forms.Button;
    cancelButton.DialogResult = Forms.DialogResult.Cancel;
    cancelButton.Name = "cancelButton";
    cancelButton.Size = new Size(50, 23);
    cancelButton.Text = "&NO";
    cancelButton.Font = new System.Drawing.Font("微软雅黑", 8, FontStyle.Regular);
    cancelButton.Location = new Point(255, 4);
    cancelButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
    cancelButton.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255);

    cancelButton.FlatAppearance.BorderSize = 0; 
    cancelButton.TabStop = false;
    form.Controls.Add(cancelButton);

    form.AcceptButton = okButton;
    form.CancelButton = cancelButton;

    dragMoveBlind = ShowImageBindDragMove(form, form);
    dragMoveBlind = ShowImageBindDragMove(form, label);
    //dragMoveBlind = ShowImageBindDragMove(form, textBox);
    form.ShowDialog();

    input = textBox.Text;

    return input;
}


function ShowImageBindDragMove(form, item) {
    var Cursors = forms.System.Windows.Forms.Cursors;
    var ReleaseCapture = sp_config.GetType().Assembly.GetType("API").GetMethod("ReleaseCapture");

    var item_mouseMove = 
    item.MouseMove.connect(
    function (sender, e) {
        var hitResize = form.Height + form.Width - (e.X + e.Y) < 30;
        var cursor = hitResize ? Cursors.SizeNWSE : Cursors.Default;
        if (item.Cursor != cursor) item.Cursor = cursor;
        if (e.Button == MouseButtons.Left) {
            ReleaseCapture.Invoke(null, null);
            sp.WindowFromHandle(form.Handle).SendMessageObj(0x0112,
                hitResize ? 0xF008 : 0xF012, 0);
        }
    });

    return [item_mouseMove];
}

Edited by user Monday, January 3, 2022 1:36:09 PM(UTC)  | Reason: Not specified

thanks 1 user thanked soooulp for this useful post.
Rob on 12/28/2021(UTC)
soooulp  
#2 Posted : Friday, December 31, 2021 4:34:14 AM(UTC)
soooulp

Rank: Advanced Member

Reputation:

Groups: Approved
Joined: 4/23/2020(UTC)
Posts: 105
China

Thanks: 37 times
Was thanked: 6 time(s) in 5 post(s)
Compress the selected file(s) in two conditions in 7z.

1 one file or one folder
compress it to the file name zip

2 more files or file(s) with fold(s)
compress it to the MyMewZip.zip with a folder named MyNewFolder

The insufficient is that it can't create a really new subfolder into the zip in the 7z command line.

Code:
var result;
var selectedFiles;
var fullPath = ""; 
var isDesktop = false;
var success = false;
var path = "";
var fileNameNoExt;
var allFile = "";
var filepath = "";
var filespath = "";
var archivename;
var onlyNames = true; //Only include the name, not the full path and name
var includeFolders = true; //Include folders as well as files
var includeLinks = true; //Include links (shortcuts)
var n = 0;
var m = "";
var str = "C:\\Users\\Administrator\\Desktop";
var foldinfo = new System.IO.DirectoryInfo(str);
var desktopfilescount = foldinfo.EnumerateFileSystemInfos().Count();

if(action.Control.HWnd.ToInt32() == sp.DesktopWindowListView().HWnd.ToInt32()) {
    //Desktop
    selectedFiles = sp.GetSelectedFilesOnDesktop();
    isDesktop = true;
} else {
    //Not Desktop
    selectedFiles = sp.GetSelectedFilesInExplorer(action.Window.HWnd);
}

fullPath = selectedFiles[0]; //Selected file path
path = sp.GetActiveExplorerPath(wndHandle); //Window path
fileNameNoExt = System.IO.Path.GetFileNameWithoutExtension(fullPath);
allFile = sp.GetItemsInExplorer(wndHandle, onlyNames, includeFolders, includeLinks); //All the files in explorer

if(isDesktop) {
    fullPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) + "\\" + fullPath;
    path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop);
    fileNameNoExt = System.IO.Path.GetFileNameWithoutExtension(fullPath);
    
    for(i = 0; i < desktopfilescount; i++) 
    {
        if(System.IO.File.Exists(path + "\\MyNewZip" + m + ".zip") || 
            System.IO.File.Exists(path + "\\" + fileNameNoExt + m + ".zip")) {
            n = n + 1;
            m = " (" + n + ")";
            //m = "_" + n;
        }
    }
} else {
    for(i = 0; i < allFile.Length; i++)
    {
        if(("MyNewZip" + m + ".zip") == allFile[i] || 
            (fileNameNoExt + m + ".zip") == allFile[i]) {
            n = n + 1;
            m = " (" + n + ")";
            //m = "_" + n;
        }
    }
}

if(selectedFiles.Length == 1) {
    filepath = fullPath;
    archivename = path + "\\" + fileNameNoExt + m + ".zip";
    StrokesPlus.Console.Log(filepath); 
    result = sp.RunProgram('C:\\Program Files\\7-Zip\\7z.exe', 
                                       `a "${archivename}" "${filepath}"`,
                                       'open', 'hidden', false, true, true);
} else if(selectedFiles.Length > 1) {
    for(i = 0; i < selectedFiles.Length; i++)
    {
        fullPath = selectedFiles[i];

        if(isDesktop) {
            fullPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) + "\\" + fullPath;
        }
        filespath = filespath + " " + "\"" + fullPath + "\"";
    }

    archivename = path + "\\MyNewZip" + m + ".zip";
    StrokesPlus.Console.Log(filespath);
    sp.RunProgram('C:\\Program Files\\7-Zip\\7z.exe', 
                                       `a "${archivename}" ${filespath}`,
                                       'open', 'hidden', false, true, true);

    var p = new System.Diagnostics.Process();
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.FileName = "C:\\Program Files\\7-Zip\\7z.exe";
    p.StartInfo.CreateNoWindow = true;
    p.StartInfo.Arguments = "l \"" + archivename + "\""
    p.Start();
    var output = p.StandardOutput.ReadToEnd();
    p.WaitForExit();

    var lines = output.split(/\r\n|\n\r|\n|\r/);

    for(var i = 0; i < lines.length - 2; i++) {
        if(/^[0-9]{4}-/.test(lines[i])) {
            var zipfilepath = lines[i].slice(53);
            var temzipfilepath = "MyNewFolder\\" + zipfilepath;
            result = sp.RunProgram('C:\\Program Files\\7-Zip\\7z.exe', 
                                       `rn ${archivename} "${zipfilepath}" "${temzipfilepath}"`,
                                       'open', 'hidden', false, true, true); 

        }
    }
}

if(result == 0) { 
    success = true;
    sp.ShowBalloonTip("ZIP Extract", "Success", "info", 1000);
} 
if(!success) {
    sp.ShowBalloonTip("ZIP", "Failed", "error", 1000);
}






Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.