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

Notification

Icon
Error

Options
Go to last post Go to first unread
randomConstant  
#1 Posted : Thursday, December 9, 2021 6:20:40 PM(UTC)
randomConstant

Rank: Advanced Member

Reputation:

Groups: Translators, Approved
Joined: 7/17/2021(UTC)
Posts: 135

Thanks: 35 times
Was thanked: 18 time(s) in 15 post(s)
Hi all,

So this might be a little tricky to explain but I will try my best, I am looking to fulfill these requirements using S+:
  1. Search a text file(.txt) for presence of a keyword. such as a copied word.
  2. Get a search result of either a boolean value(representing whether the keyword was found or not), or line number of the found keyword.
  3. Do step 1 and 2 in the background, meaning either without opening the text file(opening using code is fine), or opening the file in the background(file opens but does not become a foreground app) so that user does not notice and gets distracted from his work.
  4. This one is tricky, let me break it down, the text file is in a zip folder, that zip folder is encrypted. File is accessed normally by opening the zip(inserting password), zip is/should not be extracted(unzipped), so
    1. Either somehow access the text file by opening the zip using password all in the background(using code)
    2. Or use an opened instance of the opened text file(meaning user puts password and opens text file in, lets say Notepad++ and that file remains opened in Np++ for this use case), to search the content.



Ideally, while surfying the web user would highlight a keyword(text) and draw a gesture which would display a popup showing whether that keyword is present in the text file or not.

I'm not sure if all of the steps/goals can be achieved but I'd appreciate a solution to as many of them as possible. Any reference code or plugin will be most helpful.

Thanks
Rob  
#2 Posted : Friday, December 10, 2021 1:23:42 AM(UTC)
Rob

Rank: Administration

Reputation:

Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 1,349
United States
Location: Tampa, FL

Thanks: 28 times
Was thanked: 416 time(s) in 354 post(s)
Yeah, it should be possible.

Get the library (DLL) from this library and put it in the plugins folder:

https://github.com/icsha...ode/SharpZipLib/releases

Once you get the text file, this example will give you some direction for finding text in a file:

https://stackoverflow.co...er-and-the-complete-line

Not exactly a complete solution, but the pieces are there. On my phone so I can't play around to build anything. But maybe tomorrow or Saturday.
thanks 1 user thanked Rob for this useful post.
randomConstant on 12/10/2021(UTC)
randomConstant  
#3 Posted : Friday, December 10, 2021 11:47:54 AM(UTC)
randomConstant

Rank: Advanced Member

Reputation:

Groups: Translators, Approved
Joined: 7/17/2021(UTC)
Posts: 135

Thanks: 35 times
Was thanked: 18 time(s) in 15 post(s)
Thanks for the quick reply Rob.

I've managed to fulfill step 1, 2, and 3 using your guide. Here is the code:
Code:
//function to search for text in a file
function searchFileForText(stringToSearch, filePath){
//open file to read
var sr = clr.System.IO.File.OpenText(filePath);
//stores current line
var line;
//stores current line number
var lineNumber = 0;
//tracks whether keyword is found or not
var foundFlag = false;

try{
    //traverse the file
    while ((line = sr.ReadLine()) != null && !foundFlag){
        //increment line counter
        lineNumber += 1;
        //check for a keyword match in line
        if ( String(line).match(stringToSearch)){
            //keyword found, set flag and do stuff...
            foundFlag = true;
            showText("Match found at line : "+String(lineNumber)+"\nContent : "+line);
        }
    }
}catch{ 
    showText("Error"); 
}
//close file
sr.Close();
sr.Dispose();
}


Unfortunately, step 4 remains unsolved for now.
I visited that library link and its providing a .nupkg file which I believe contains source code but I cannot make sense of it. Confused

Also I was looking at stackoverflow posts regarding this and I noticed almost always the zip file is somehow unzipped and extracted to a location such as in this code:
Code:
string zipFile = @"C:\Users\Fesslersoft\Desktop\ZipTest\Test.zip";
string targetDirectory = @"C:\Users\Fesslersoft\Desktop\ZipTest\";
 
using (Ionic.Zip.ZipFile zip = Ionic.Zip.ZipFile.Read(zipFile))
{
    zip.Password = "1234";
    zip.ExtractAll(targetDirectory, Ionic.Zip.ExtractExistingFileAction.DoNotOverwrite);
}
 
Console.WriteLine("Zip file has been successfully extracted.");
Console.Read();


That is not the desired outcome for this use case actually, unzipping the file would mean there is a duplicate extracted folder of the same content, it would need to be deleted after every read as its an un-encrypted version of encrypted zip.

Currently I am opening the zip file(not extracting), inserting password on prompt(this opens the zip in 7zip), opening the text file from 7zip, reading/editing the file, choosing update modified file in 7zip after editing so it replaces the old version with new one, and then closing the 7zip(this locks the zip file again)

So I would like to open the zip file and not have to extract it temporarily at another location.

Or if its not possible, having a Notepad++ instance of the text file(manually) is not a big issue for me, so if there's some way of reading that opened file it would be a great alternative as well.

This is not an urgent issue so please take your time and I would appreciate a solution later. Laugh
Rob  
#4 Posted : Friday, December 10, 2021 4:27:02 PM(UTC)
Rob

Rank: Administration

Reputation:

Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 1,349
United States
Location: Tampa, FL

Thanks: 28 times
Was thanked: 416 time(s) in 354 post(s)
Okay, first download this zip and grab the DLL out.

https://www.strokesplus....ins/DotNetZip_1.16.0.zip

( from project: https://github.com/haf/DotNetZip.Semverd )

NOTE: Don't put this in the Plugins folder (you can, but the script won't work without refactoring) - just put it anywhere, can even make a new folder under S+ for AdditionalReferences or something.

Go to Plug-Ins > Additional References > Add Reference and select the DLL file.

Then this script should work fine, at least it did for me using a standard password protected Zip file.

Code:
//function to search for text in a file
// zipFilePath: Path to encrypted Zip file
// zipPassword: Duh
// targetFile: Name of file inside Zip file
function searchFileForText(stringToSearch, zipFilePath, zipPassword, targetFile){
    //open file to read
    var zipFile = Ionic.Zip.ZipFile.Read(zipFilePath);
    zipFile.Password = zipPassword;
    var file = zipFile.Where(x => x.FileName == targetFile).FirstOrDefault();
    var stream = file.OpenReader();
    var bytes = host.newArr(System.Byte, stream.Length); 
    stream.Read(bytes, 0, stream.Length);
    stream.Close();
    var fileContents = System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length);
    var fileLines = fileContents.split("\n");

    //tracks whether keyword is found or not
    var foundFlag = false;

    try{
        //traverse the file
        for(var lineNumber = 0; lineNumber < fileLines.length; lineNumber++) {
            //check for a keyword match in line
            if ( String(fileLines[lineNumber]).match(stringToSearch)){
                //keyword found, set flag and do stuff...
                foundFlag = true;
                showText("Match found at line : "+String(lineNumber+1)+"\nContent : "+fileLines[lineNumber]);
                break;
            }            
        }
    }catch{ 
        showText("Error"); 
    }
}

Edited by user Friday, December 10, 2021 5:16:21 PM(UTC)  | Reason: Not specified

thanks 1 user thanked Rob for this useful post.
randomConstant on 12/10/2021(UTC)
randomConstant  
#5 Posted : Friday, December 10, 2021 5:11:04 PM(UTC)
randomConstant

Rank: Advanced Member

Reputation:

Groups: Translators, Approved
Joined: 7/17/2021(UTC)
Posts: 135

Thanks: 35 times
Was thanked: 18 time(s) in 15 post(s)
Thanks a lot Rob.

It is working perfectly fine. Laugh

I think we are reading the file and storing its content in a variable and then searching for keyword inside it. This is good for reading(even though it takes some memory to store lines) but I'm not sure how I can use it to write to file if needed.

I tried doing this to try and write to file:
Code:
//function to insert text in a zip file
// zipFilePath: Path to encrypted Zip file
// zipPassword: Duh
// targetFile: Name of file inside Zip file
function insertInZipFile(stringToInsert, zipFilePath, zipPassword, targetFile){
    //open file to write
    var zipFile = Ionic.Zip.ZipFile.Write(zipFilePath);
    zipFile.Password = zipPassword;
    var file = zipFile.Where(x => x.FileName == targetFile).FirstOrDefault();
    var stream = file.OpenWriter();
    stream.Write(stringToInsert);
    stream.Close();

}


But it gave me this error:
Code:
[ScriptEngine.Execute() - ScriptEngineException] TypeError: Ionic.Zip.ZipFile.Write is not a function
    at insertInZipFile (LoadScripts:226:37) ->     var zipFile = Ionic.Zip.ZipFile.Write(zipFilePath);
    at Script [24]:6:1


I feel like this is close to working but I do not know the file.OpenWriter and ZipFile.Write syntax.

Please help T_T
Rob  
#6 Posted : Friday, December 10, 2021 5:28:37 PM(UTC)
Rob

Rank: Administration

Reputation:

Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 1,349
United States
Location: Tampa, FL

Thanks: 28 times
Was thanked: 416 time(s) in 354 post(s)
Are you trying to insert a new file or append to an existing file in the zip?
randomConstant  
#7 Posted : Friday, December 10, 2021 5:31:41 PM(UTC)
randomConstant

Rank: Advanced Member

Reputation:

Groups: Translators, Approved
Joined: 7/17/2021(UTC)
Posts: 135

Thanks: 35 times
Was thanked: 18 time(s) in 15 post(s)
I am trying to append some data on the existing file.

This way the file can be searched for a keyword and further more keywords(data) can be added as well.

Edited by user Friday, December 10, 2021 5:32:30 PM(UTC)  | Reason: was -> way

randomConstant  
#8 Posted : Friday, December 10, 2021 5:34:02 PM(UTC)
randomConstant

Rank: Advanced Member

Reputation:

Groups: Translators, Approved
Joined: 7/17/2021(UTC)
Posts: 135

Thanks: 35 times
Was thanked: 18 time(s) in 15 post(s)
oh no I committed an editorial sin again, now its gone for moderation.

I was saying I am trying to write to the same file.

That way a file inside an encrypted zip can be searched for a keyword and further more keywords(data) can be added as well.
randomConstant  
#9 Posted : Friday, December 10, 2021 5:43:43 PM(UTC)
randomConstant

Rank: Advanced Member

Reputation:

Groups: Translators, Approved
Joined: 7/17/2021(UTC)
Posts: 135

Thanks: 35 times
Was thanked: 18 time(s) in 15 post(s)
Also this might be a nice opportunity to ask this..

In which language should a novice programmer like myself search for solutions when trying to work with S+ scripts?

I've heard you mention clearscript, I've seen javascript like codes, I know S+ works with .NET.

I'm not sure if JS allows opening file streams and the syntax is always a bit different when I search for solutions online. Again, I am confused because I do not actually know which type of code(such as opening and closing files in this case) is supported by which language because I am new. Crying
Rob  
#10 Posted : Friday, December 10, 2021 6:29:07 PM(UTC)
Rob

Rank: Administration

Reputation:

Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 1,349
United States
Location: Tampa, FL

Thanks: 28 times
Was thanked: 416 time(s) in 354 post(s)
This worked for me - note that I'm adding a new line character to append at the end of the file:
Code:
//function to append text in a zip file
// stringToAppend: Text to append to end of targetFile
// zipFilePath: Path to encrypted Zip file
// zipPassword: Duh
// targetFile: Name of file inside Zip file
function appendStringInZipFile(stringToAppend, zipFilePath, zipPassword, targetFile){
    //open zip file 
    var zipFile = Ionic.Zip.ZipFile.Read(zipFilePath);
    zipFile.Password = zipPassword;
    var file = zipFile.Where(x => x.FileName == targetFile).FirstOrDefault();
    var stream = file.OpenReader();
    var bytes = host.newArr(System.Byte, stream.Length); 
    stream.Read(bytes, 0, stream.Length);
    stream.Close();
    var fileContents = System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length);
    fileContents = fileContents + "\n" + stringToAppend
    zipFile.RemoveEntry(file);
    zipFile.AddEntry(targetFile, fileContents);
    zipFile.Save();
}


Regarding the language, ClearScript is a unique environment - especially with how much of the .NET Framework is being exposed to the engine.

So the script engine itself is the Chrome V8 JavaScript engine. ClearScript creates a bridge between the JavaScript and .NET/S+ code, so it's a hybrid environment where normal JavaScript works and S+/.NET code/objects are wrapped by ClearScript to be accessible within JavaScript.

Recently I changed the namespacing in the script engine so all things can be fully qualified, e.g. System.Text instead of clr.System.Text to make it a little more intuitive and natural. But still, it can be a little confusing to take some C# example code and refactor (where needed) to work in the ClearScript/JavaScript environment.

Like the byte array:
Code:
var bytes = host.newArr(System.Byte, stream.Length); 

In C# it would just be:
Code:
var bytes = new byte[stream.Length];

But that's not valid in JavaScript, so you call the host object - which are functions exposed by ClearScript ( https://microsoft.github...Script_HostFunctions.htm ) to have it create a .NET type, especially since the stream.Read(...) call expects a .NET System.Byte[] parameter, so you can't pass a JavaScript type to it.

Now behind the scenes, ClearScript will handle type mapping automatically for base types, like numbers/strings/etc - but not a byte array.

Hopefully this helps a little bit! But generally, if you find some C# code, it can usually be ported to S+ script.

Edited by user Friday, December 10, 2021 8:30:18 PM(UTC)  | Reason: Not specified

thanks 1 user thanked Rob for this useful post.
randomConstant on 12/10/2021(UTC)
randomConstant  
#11 Posted : Friday, December 10, 2021 6:47:44 PM(UTC)
randomConstant

Rank: Advanced Member

Reputation:

Groups: Translators, Approved
Joined: 7/17/2021(UTC)
Posts: 135

Thanks: 35 times
Was thanked: 18 time(s) in 15 post(s)
Thanks a lot Rob.

It is working perfectly fine and yes what you said makes much sense now.

Thanks Laugh
randomConstant  
#12 Posted : Friday, December 10, 2021 8:06:45 PM(UTC)
randomConstant

Rank: Advanced Member

Reputation:

Groups: Translators, Approved
Joined: 7/17/2021(UTC)
Posts: 135

Thanks: 35 times
Was thanked: 18 time(s) in 15 post(s)
Actually I've encountered one small issue..

Whenever I search or append data from/to file using scripts, and then open zip manually and try to save its modified version manually it gives me an error saying file is already in use.
And when I reload S+ and repeat same steps it saves without any problem.

I'm guessing there is a way to close access to zip file which is missing.. similar to how we close file reading/writing streams.
randomConstant  
#13 Posted : Friday, December 10, 2021 8:17:35 PM(UTC)
randomConstant

Rank: Advanced Member

Reputation:

Groups: Translators, Approved
Joined: 7/17/2021(UTC)
Posts: 135

Thanks: 35 times
Was thanked: 18 time(s) in 15 post(s)
Originally Posted by: Rob Go to Quoted Post
This worked for me - note that I'm adding a new line character to append at the end of the file:
Code:
//function to append text in a zip file
// stringToAppend: Text to append to end of targetFile
// zipFilePath: Path to encrypted Zip file
// zipPassword: Duh
// targetFile: Name of file inside Zip file
function appendStringInZipFile(stringToAppend, zipFilePath, zipPassword, targetFile){
    //open zip file 
    var zipFile = Ionic.Zip.ZipFile.Read(zipFilePath);
    zipFile.Password = zipPassword;
    var file = zipFile.Where(x => x.FileName == targetFile).FirstOrDefault();
    var stream = file.OpenReader();
    var bytes = host.newArr(System.Byte, stream.Length); 
    stream.Read(bytes, 0, stream.Length);
    stream.Close();
    var fileContents = System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length);
    fileContents += fileContents + "\n" + stringToAppend
    zipFile.RemoveEntry(file)
    zipFile.AddEntry(targetFile, fileContents);
    zipFile.Save();
}


Regarding the language, ClearScript is a unique environment - especially with how much of the .NET Framework is being exposed to the engine.

So the script engine itself is the Chrome V8 JavaScript engine. ClearScript creates a bridge between the JavaScript and .NET/S+ code, so it's a hybrid environment where normal JavaScript works and S+/.NET code/objects are wrapped by ClearScript to be accessible within JavaScript.

Recently I changed the namespacing in the script engine so all things can be fully qualified, e.g. System.Text instead of clr.System.Text to make it a little more intuitive and natural. But still, it can be a little confusing to take some C# example code and refactor (where needed) to work in the ClearScript/JavaScript environment.

Like the byte array:
Code:
var bytes = host.newArr(System.Byte, stream.Length); 

In C# it would just be:
Code:
var bytes = new byte[stream.Length];

But that's not valid in JavaScript, so you call the host object - which are functions exposed by ClearScript ( https://microsoft.github...Script_HostFunctions.htm ) to have it create a .NET type, especially since the stream.Read(...) call expects a .NET System.Byte[] parameter, so you can't pass a JavaScript type to it.

Now behind the scenes, ClearScript will handle type mapping automatically for base types, like numbers/strings/etc - but not a byte array.

Hopefully this helps a little bit! But generally, if you find some C# code, it can usually be ported to S+ script.


Small correction of a typo for anyone using this code, replace:
Code:
fileContents += fileContents + "\n" + stringToAppend

with:
Code:
fileContents = fileContents + "\n" + stringToAppend;

or:
Code:
fileContents += "\n" + stringToAppend;


otherwise it would multiply file's previous content(and thus size) by 2 every time data is appended.

also a missing semicolon here:
Code:
zipFile.RemoveEntry(file);


Rob  
#14 Posted : Friday, December 10, 2021 8:29:35 PM(UTC)
Rob

Rank: Administration

Reputation:

Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 1,349
United States
Location: Tampa, FL

Thanks: 28 times
Was thanked: 416 time(s) in 354 post(s)
Oops!
soooulp  
#15 Posted : Wednesday, December 22, 2021 10:15:20 AM(UTC)
soooulp

Rank: Advanced Member

Reputation:

Groups: Moderators, Approved
Joined: 4/23/2020(UTC)
Posts: 161
China

Thanks: 46 times
Was thanked: 23 time(s) in 17 post(s)
Originally Posted by: randomConstant Go to Quoted Post
Originally Posted by: Rob Go to Quoted Post
This worked for me - note that I'm adding a new line character to append at the end of the file:


Small correction of a typo for anyone using this code, replace:
Code:
fileContents += fileContents + "\n" + stringToAppend

with:
Code:
fileContents = fileContents + "\n" + stringToAppend;

or:
Code:
fileContents += "\n" + stringToAppend;


otherwise it would multiply file's previous content(and thus size) by 2 every time data is appended.

also a missing semicolon here:
Code:
zipFile.RemoveEntry(file);




Thank you for your this kind nice discussion.

Do you know how to count the files in the zip? I want the result to judge that is there only one fold in the zip or some files in the zip?
Do you know
Rob  
#16 Posted : Wednesday, December 22, 2021 11:44:38 AM(UTC)
Rob

Rank: Administration

Reputation:

Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 1,349
United States
Location: Tampa, FL

Thanks: 28 times
Was thanked: 416 time(s) in 354 post(s)
On my phone so I can't test, but it's probably.

zipFile.Count

Or

zipFile.Count()
thanks 1 user thanked Rob for this useful post.
randomConstant on 12/22/2021(UTC)
randomConstant  
#17 Posted : Wednesday, December 22, 2021 12:33:07 PM(UTC)
randomConstant

Rank: Advanced Member

Reputation:

Groups: Translators, Approved
Joined: 7/17/2021(UTC)
Posts: 135

Thanks: 35 times
Was thanked: 18 time(s) in 15 post(s)
Originally Posted by: Rob Go to Quoted Post
On my phone so I can't test, but it's probably.

zipFile.Count

Or

zipFile.Count()


Tested. zipFile.Count seem to be working fine. ThumpUp

soooulp  
#18 Posted : Wednesday, December 22, 2021 1:05:08 PM(UTC)
soooulp

Rank: Advanced Member

Reputation:

Groups: Moderators, Approved
Joined: 4/23/2020(UTC)
Posts: 161
China

Thanks: 46 times
Was thanked: 23 time(s) in 17 post(s)
Emmm, it shows an Error 'Ionic is not defined', but I added the DLL file into the Plug-in as Rob said.

And how to judge that is there only one fold or one file or are there some files in the zip?

UserPostedImage
randomConstant  
#19 Posted : Wednesday, December 22, 2021 2:52:44 PM(UTC)
randomConstant

Rank: Advanced Member

Reputation:

Groups: Translators, Approved
Joined: 7/17/2021(UTC)
Posts: 135

Thanks: 35 times
Was thanked: 18 time(s) in 15 post(s)
hmm try this code, its a function that takes zip file path and optional password as input and returns total number of files+folders inside it.

Code:
//count number of files in a zip file
// zipFilePath: Path to encrypted Zip file
// zipPassword: Duh
function countFilesInZipFile(zipFilePath, zipPassword = ""){
//open file to read
var zipFile = Ionic.Zip.ZipFile.Read(zipFilePath);
zipFile.Password = zipPassword;
return zipFile.Count
}


zip files actually show files and folders names, and their sizes when encrypted (7zip does not) so you don't actually need password.

Hope it helps. Laugh
randomConstant  
#20 Posted : Wednesday, December 22, 2021 2:54:24 PM(UTC)
randomConstant

Rank: Advanced Member

Reputation:

Groups: Translators, Approved
Joined: 7/17/2021(UTC)
Posts: 135

Thanks: 35 times
Was thanked: 18 time(s) in 15 post(s)
oh and put the contents of function in a try-catch block because it can throw a runtime error if file path is incorrect.
randomConstant  
#21 Posted : Wednesday, December 22, 2021 2:57:28 PM(UTC)
randomConstant

Rank: Advanced Member

Reputation:

Groups: Translators, Approved
Joined: 7/17/2021(UTC)
Posts: 135

Thanks: 35 times
Was thanked: 18 time(s) in 15 post(s)
and put the DLL as an additional reference from the Plugins section of S+, maybe you mistakenly added it as a plugin.
thanks 1 user thanked randomConstant for this useful post.
soooulp on 12/22/2021(UTC)
soooulp  
#22 Posted : Wednesday, December 22, 2021 3:21:59 PM(UTC)
soooulp

Rank: Advanced Member

Reputation:

Groups: Moderators, Approved
Joined: 4/23/2020(UTC)
Posts: 161
China

Thanks: 46 times
Was thanked: 23 time(s) in 17 post(s)
Originally Posted by: randomConstant Go to Quoted Post
and put the DLL as an additional reference from the Plugins section of S+, maybe you mistakenly added it as a plugin.


Ohhhhhh, I see, and I really miss the AdditionalReferences. Now it is normal. Thank you randomConstant.

I took a look at the Github page just now, and I didn't find how to get the name or type of the file(s) in the zip.

In another post, Rob helped to the code that unzips the file to the current fold and adds a new fold or just the current fold.

I want to judge the file count and the type of the file in the zip to achieve to unzip in the following three conditions, so now I need the type of the file(s) in the zip

1 Only one file
a, Unzip to the current fold
b, If the file exists, then rename it by the name add(1)

2 Only one fold
a, Unzip to the current fold directly
b, If the fold exists, the new fold name add(1)

3 Some files with fold(s)
a, Unzip to the new fold with the zip file name
b, If the fold exists, the new fold name add(1)
randomConstant  
#23 Posted : Wednesday, December 22, 2021 3:32:56 PM(UTC)
randomConstant

Rank: Advanced Member

Reputation:

Groups: Translators, Approved
Joined: 7/17/2021(UTC)
Posts: 135

Thanks: 35 times
Was thanked: 18 time(s) in 15 post(s)
I would like to mention zipFile.Count returns count of all the files and folders in the zip, so if zip contains a folder and then 2 files and 2 folders inside that folder, you will get a total count of 5.

I'm not sure about file type detection using this plugin though so Rob will probably help you out. Laugh
soooulp  
#24 Posted : Thursday, December 23, 2021 9:42:10 AM(UTC)
soooulp

Rank: Advanced Member

Reputation:

Groups: Moderators, Approved
Joined: 4/23/2020(UTC)
Posts: 161
China

Thanks: 46 times
Was thanked: 23 time(s) in 17 post(s)
I find some codes to identify the file(s) that is a fold or not in the zip by C#.



I try to like this in S+net, and it shows the filenames and IsDirectory.

Code:
var wnd = sp.ForegroundWindow();
var selectedFiles = sp.GetSelectedFilesInExplorer(wnd.HWnd);

var fullPath = selectedFiles[0];

var zipFile = Ionic.Zip.ZipFile.Read(fullPath);
//var entry = zipFile.Entries;
var s;
var n = zipFile.Count
var str = "";
var bool = "";
var delim = "";

//Loop through each item
zipFile.forEach(function(s)
{
str = str + delim + entry.FileName;
bool = bool + delim + entry.IsDirectory;
 delim = "\r\n"; 
});
sp.MessageBox(str + bool, 'Message');

Edited by user Thursday, December 23, 2021 11:20:39 AM(UTC)  | Reason: Not specified

soooulp  
#25 Posted : Thursday, December 23, 2021 3:41:38 PM(UTC)
soooulp

Rank: Advanced Member

Reputation:

Groups: Moderators, Approved
Joined: 4/23/2020(UTC)
Posts: 161
China

Thanks: 46 times
Was thanked: 23 time(s) in 17 post(s)
After the surprise for getting the count and name list of the zip archive, but it is just only for the ZIP instead of RAR.

I find there is a command '7z.exe l archive.zip' to get the file list of the archive file.

So, could it use on the S+ to get the entry count and name list of the archive file?

There is a PHP code from StackOverflow, and I haven't an environment to test it.

https://stackoverflow.com/questions/38510771/list-files-in-7z-rar-and-tar-archives-using-php

and this may be for reference.

https://stackoverflow.com/questions/59450789/how-to-rename-files-and-folder-in-rar-7z-tar-zip-using-c-sharp

Code:
<?php

$x = exec("7z l ./test.zip | awk '/^[0-9]{4}-/{print}'", $l);
foreach($l as $r)
{
    $e = explode(" ", $r);
    $c = count($e)-1;
    echo $e[$c]."\n";
}
?>


Rob  
#26 Posted : Thursday, December 23, 2021 6:05:16 PM(UTC)
Rob

Rank: Administration

Reputation:

Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 1,349
United States
Location: Tampa, FL

Thanks: 28 times
Was thanked: 416 time(s) in 354 post(s)
Here's an example to get you started:
Code:
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 C:\\Temp\\test.rar"
p.Start();
var output = p.StandardOutput.ReadToEnd();
p.WaitForExit();

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

// skipping last line to ignore the summary line
// but yeah,  tweak as needed
for(var i = 0; i < lines.length - 2; i++) {
    if(/^[0-9]{4}-/.test(lines[i])) {
        StrokesPlus.Console.Log(lines[i]);
    }
}
thanks 2 users thanked Rob for this useful post.
soooulp on 12/24/2021(UTC), randomConstant on 4/14/2022(UTC)
soooulp  
#27 Posted : Friday, December 24, 2021 4:28:31 PM(UTC)
soooulp

Rank: Advanced Member

Reputation:

Groups: Moderators, Approved
Joined: 4/23/2020(UTC)
Posts: 161
China

Thanks: 46 times
Was thanked: 23 time(s) in 17 post(s)
Thank you so so so much, Rob. Now it is normal.

Code:
var wndHandle = sp.ForegroundWindow().HWnd;
var desktopHandle = sp.DesktopWindowListView().HWnd;
var fullPath = "";
var selectedFiles;
var isDesktop = false;

//if(wndHandle.ToInt32() == desktopHandle.ToInt32() || sp.LastFocusControl().HWnd.ToInt32() == desktopHandle.ToInt32()) {
if(wndHandle.ToInt32() == desktopHandle.ToInt32() || sp.LastFocusControl().HWnd.ToInt32() == desktopHandle.ToInt32()) {
    //Desktop
    selectedFiles = sp.GetSelectedFilesOnDesktop();
    isDesktop = true;
} else {
    //Not Desktop
    selectedFiles = sp.GetSelectedFilesInExplorer(wndHandle);
}

fullPath = selectedFiles[0];

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

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 + "\""
p.Start();
var output = p.StandardOutput.ReadToEnd();
p.WaitForExit();

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

// skipping last line to ignore the summary line
// but yeah,  tweak as needed
for(var i = 0; i < lines.length - 1; i++) {
    if(/^[0-9]{4}-/.test(lines[i])) {
        StrokesPlus.Console.Log(lines[i]);
    }
}
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.