1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
public class WindowsShortcut
{
private String filename = null;
private boolean hasRelativePath = false;
private boolean hasDescription = false;
private boolean hasWorkingDirectory = false;
private boolean hasCommandLineArgument = false;
private boolean hasAnIcon = false;
private boolean shellItemPresent = false;
private boolean fileOnLocalDisk = false;
private int volumeTableOffset = 0;
private int pathOffset = 0;
private int networkTableOffset = 0;
private int finalPathOffset = 0;
private String path = null;
private String finalPath = null;
private String description = null;
private String relativePath = null;
private String workingDirectory = null;
private String arguments = null;
/**
* Initialise l'objet SHORTCUT
* @param filename le fichier .lnk dont on veut récupérer les informations
*/
public WindowsShortcut(String filename) {
this.filename = filename;
}
/**
* Effectue la lecture du fichier .lnk passé au constructeur
* @throws IOException si le fichier ne peut etre lu
*/
public void processShortcut() throws IOException {
DataInputStream in = new DataInputStream(new FileInputStream(filename));
// Lecture du header
processHeader(in);
// Si la liste des items est présente, on s'en occupe
if (shellItemPresent) {
processShellItem(in);
}
// Lecture des informations sur la location du fichier
processFileLocationInfo(in);
// Si le fichier est en local, on traite la table de volume local
if (fileOnLocalDisk) {
processLocalVolumeTable(in);
}
// Sinon, on s'occupe de celle réseau
else {
processNetworkVolumeTable(in);
}
// On traite le chemin de base
processPath(in);
// On traite le chemin final
processfinalPath(in);
// Si le raccourci a une description, on s'en occupe
if (hasDescription) {
description = processString(in);
}
// Si le raccourci a un chemin relatif, on s'en occupe
if (hasRelativePath) {
relativePath = processString(in);
}
// Si le raccourci a un répertoire de travail, on s'en occupe
if (hasWorkingDirectory) {
workingDirectory = processString(in);
// Cas particulier pour traiter le répertoire de travail c:\
if (workingDirectory.equals("%HOMEDRIVE%%HOMEPATH%")) {
workingDirectory = "C:\\";
}
}
// Si il y a des arguments, on les récupère
if (hasCommandLineArgument) {
arguments = processString(in);
}
// Fermeture du flux vers le fichier .lnk
in.close();
}
private String processString(DataInputStream in) throws IOException {
int length = mYReadShort(in);
StringBuffer buf = new StringBuffer(length);
byte temp = 1;
for (int i = 0; i < length; i++) {
buf.append( (char) (mYReadShort(in)));
}
return buf.toString();
}
private void processfinalPath(DataInputStream in) throws IOException {
StringBuffer buf = new StringBuffer();
byte temp = 1;
while ( (temp = in.readByte()) != 0) {
buf.append( (char) temp);
}
finalPath = buf.toString();
}
private void processPath(DataInputStream in) throws IOException {
StringBuffer buf = new StringBuffer();
byte temp = 1;
while ( (temp = in.readByte()) != 0) {
buf.append( (char) temp);
}
path = buf.toString();
}
private void processLocalVolumeTable(DataInputStream in) throws IOException {
int structureLocalVolumeLenght = mYReadInt(in);
int driveType = mYReadInt(in);;
// Volume Serial Number
in.readInt();
// Volume name offset (always 0x10)
in.readInt();
in.skipBytes(structureLocalVolumeLenght - 16);
}
private void processNetworkVolumeTable(DataInputStream in) throws IOException {
int structureLocalVolumeLenght = mYReadInt(in);;
int driveType = mYReadInt(in);;
// Volume Serial Number
in.readInt();
// Volume name offset (always 0x10)
in.readInt();
in.skipBytes(structureLocalVolumeLenght - 16);
}
private void processFileLocationInfo(DataInputStream in) throws IOException {
int dataStructureInfo = mYReadInt(in);;
int dataStructureEndOffset = mYReadInt(in);;
fileOnLocalDisk = ( mYReadInt(in) == 1 ? true : false);
volumeTableOffset = mYReadInt(in);
pathOffset = mYReadInt(in);
networkTableOffset = mYReadInt(in);
finalPathOffset = mYReadInt(in);
}
private void processHeader(DataInputStream in) throws IOException {
//read first 4 bytes, can test if they are 4C 00 00 00
mYReadInt(in);
// GUID 16 bytes, current is 01 14 02 00 00 00 00 00 C0 00 00 00 00 00 46
mYReadLong(in);
mYReadLong(in);
// read shortcut flags 1 dword (double word -> 4 bytes -> 1 Int)
processFlag(mYReadInt(in));
// read target file flags 1 dword (idem)
mYReadInt(in);
// Read dates creation, modification, last access
mYReadLong(in);
mYReadLong(in);
mYReadLong(in);
// Read the length of the file
mYReadInt(in);
// Read the index of the icon, 0 if no custom icon
mYReadInt(in);
// Read the state of the program windows (1 : normal, 2 mini, 3 max)
mYReadInt(in);
// Read the Hot Key
mYReadInt(in);
// Read the 2 last dword of the header
mYReadLong(in);
}
private void processShellItem(DataInputStream in) throws IOException {
int itemListLength = mYReadShort(in);
in.skipBytes(itemListLength);
}
private void processFlag(int flag) {
if ( (flag & 1) == 1) {
shellItemPresent = true;
}
if ( (flag & 4) == 4) {
hasDescription = true;
}
if ( (flag & 8) == 8) {
hasRelativePath = true;
}
if ( (flag & 16) == 16) {
hasWorkingDirectory = true;
}
if ( (flag & 32) == 32) {
hasCommandLineArgument = true;
}
if ( (flag & 64) == 64) {
hasAnIcon = true;
}
}
public String toString() {
return "Raccourci du fichier " + filename + " : \n" + "Une description : " +
(hasDescription ? "OUI\n" : "NON\n") +
"Un répertoire de travail : " +
(hasWorkingDirectory ? "OUI\n" : "NON\n") +
"Des arguments : " + (hasCommandLineArgument ? "OUI\n" : "NON\n") +
"Une chemin relatif : " + (hasRelativePath ? "OUI\n" : "NON\n") +
"une icone : " + (hasAnIcon ? "OUI\n" : "NON\n") +
"Item : " + (shellItemPresent ? "OUI\n" : "NON\n") +
"LE CHEMIN D'ACCES : " + path +
"\nLE CHEMIN FINAL : " + finalPath +
"\nLA DESCRIPTION : " + description +
"\nCHEMIN RELATIF : " + relativePath +
"\nREPERTOIRE DE TRAVAIL : " + workingDirectory +
"\nARGUMENTS : " + arguments;
}
public String getWorkingDirectory() {
return workingDirectory;
}
public String getPath() {
return path;
}
public String getDescription() {
return description;
}
public String getArguments() {
return arguments;
}
private int mYReadInt(DataInputStream in) throws IOException {
byte a = in.readByte();
byte b = in.readByte();
byte c = in.readByte();
byte d = in.readByte();
return ( (d & 0xFF) << 24) | ( (c & 0xFF) << 16) | ( (b & 0xFF) << 8) |
(a & 0xFF);
}
private int mYReadShort(DataInputStream in) throws IOException {
byte a = in.readByte();
byte b = in.readByte();
int ret = 0;
return ret | ( (b & 0xFF) << 8) | (a & 0xFF);
}
private long mYReadLong(DataInputStream in) throws IOException {
byte a = in.readByte();
byte b = in.readByte();
byte c = in.readByte();
byte d = in.readByte();
byte e = in.readByte();
byte f = in.readByte();
byte g = in.readByte();
byte h = in.readByte();
return ( (h & 0xFF) << 56) | ( (g & 0xFF) << 48) | ( (f & 0xFF) << 40) |
( (e & 0xFF) << 32) |
( (d & 0xFF) << 24) | ( (c & 0xFF) << 16) | ( (b & 0xFF) << 8) |
(a & 0xFF);
}
public static void main(String[] args) {
WindowsShortcut test = new WindowsShortcut(
"C:\\Documents and Settings\\Administrator\\Desktop\\Scrabble® 2003 Edition.lnk");
try {
test.processShortcut();
System.out.println(test);
}
catch (IOException e) {
e.printStackTrace();
}
}
} |
Partager