I want to use the command line to unzip the archive in different conditions.
If there is only one file in the archive, then unzip to the folder with the same name of the archive, and if there is only a folder, then unzip directly to the path without adding a folder.
So I need to get the attribute of an entry in the archive. How to identify the entry of an archive as a file or a folder no matter the archive is .zip .rar, .7z
Until now, I use the 7z command line 7z l archive.zip to get the list of the entry in the archive, and I don't know whether is there a better way to Read the archive for the identify result of the entry.
Date Time Attr Size Compressed Name
--------- --------------- ------------ ------------ -----------------------
2021-12-28 09:20:20 D.... 0 0 MyNewFold
2021-12-28 09:20:06 ....A 0 0 MyNewFold\My New Document (2).txt
2021-12-28 09:19:53 ....A 0 0 MyNewFold\My New Document.txt
2021-12-28 09:36:02 ....A 0 0 My New Document.txt
2021-12-28 09:36:02 0 0 3 files, 1 folders
Through the result, I can slice the Attr String 'D' or 'A' with the entry name to identify it is a file or a folder temporarily.
1
I searched the keywords in Google, but there were many said that use ZipFile or ZipArchive through .NET API System.IO.Compression.
After getting the entry in the archive, identify if there is '\' at the end of the full path.
May this only suit for .zip, and I need it can check the entry is a file or folder in both .zip .rar .7z
2
Others using System.IO.Directory.Exists(str) to identify an entry path is a directory or file, may this not suit for the path of an entry in the archive.
There is a folder named MyNewFold in the archive.zip or archive.rar, and I test this, the result is 'None', instead of 'Folder'
var str = "C:\\Users\\Administrator\\Desktop\\archive.zip\\MyNewFold";
if(System.IO.Directory.Exists(str)) {
Console.WriteLine("Folder")
} else {
Console.WriteLine("None")
}