1 package org.stackoverflowusers.file;
3 import java.io.ByteArrayOutputStream;
5 import java.io.FileInputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.text.ParseException;
9 import java.util.Locale;
12 * Represents a Windows shortcut (typically visible to Java only as a '.lnk'
15 * Retrieved 2011-09-23 from
16 * http://stackoverflow.com/questions/309495/windows-shortcut-lnk-parser-in-java/672775#672775
17 * Originally called LnkParser
19 * Written by: (the stack overflow users, obviously!) Apache Commons VFS
20 * dependency removed by crysxd (why were we using that!?)
21 * https://github.com/crysxd Headerified, refactored and commented by Code Bling
22 * http://stackoverflow.com/users/675721/code-bling Network file support added
23 * by Stefan Cordes http://stackoverflow.com/users/81330/stefan-cordes Adapted
24 * by Sam Brightman http://stackoverflow.com/users/2492/sam-brightman Based on
25 * information in 'The Windows Shortcut File Format' by Jesse Hager
26 * <jessehager@iname.com> And somewhat based on code from the book 'Swing
27 * Hacks: Tips and Tools for Killer GUIs' by Joshua Marinacci and Chris Adamson
28 * ISBN: 0-596-00907-0 http://www.oreilly.com/catalog/swinghks/
30 public class WindowsShortcut
32 private boolean isDirectory;
34 private boolean isLocal;
36 private String real_file;
39 * Provides a quick test to see if this could be a valid link ! If you try to
40 * instantiate a new WindowShortcut and the link is not valid, Exceptions may
41 * be thrown and Exceptions are extremely slow to generate, therefore any code
42 * needing to loop through several files should first check this.
46 * @return true if may be a link, false otherwise
48 * if an IOException is thrown while reading from the file
50 public static boolean isPotentialValidLink(File file) throws IOException
52 final int minimum_length = 0x64;
53 InputStream fis = new FileInputStream(file);
54 boolean isPotentiallyValid = false;
57 isPotentiallyValid = file.isFile()
58 && file.getName().toLowerCase(Locale.ROOT).endsWith(".lnk")
59 && fis.available() >= minimum_length
60 && isMagicPresent(getBytes(fis, 32));
65 return isPotentiallyValid;
68 public WindowsShortcut(File file) throws IOException, ParseException
70 InputStream in = new FileInputStream(file);
73 parseLink(getBytes(in));
81 * @return the name of the filesystem object pointed to by this shortcut
83 public String getRealFilename()
89 * Tests if the shortcut points to a local resource.
91 * @return true if the 'local' bit is set in this shortcut, false otherwise
93 public boolean isLocal()
99 * Tests if the shortcut points to a directory.
101 * @return true if the 'directory' bit is set in this shortcut, false
104 public boolean isDirectory()
110 * Gets all the bytes from an InputStream
113 * the InputStream from which to read bytes
114 * @return array of all the bytes contained in 'in'
115 * @throws IOException
116 * if an IOException is encountered while reading the data from the
119 private static byte[] getBytes(InputStream in) throws IOException
121 return getBytes(in, null);
125 * Gets up to max bytes from an InputStream
128 * the InputStream from which to read bytes
130 * maximum number of bytes to read
131 * @return array of all the bytes contained in 'in'
132 * @throws IOException
133 * if an IOException is encountered while reading the data from the
136 private static byte[] getBytes(InputStream in, Integer max)
139 // read the entire file into a byte buffer
140 ByteArrayOutputStream bout = new ByteArrayOutputStream();
141 byte[] buff = new byte[256];
142 while (max == null || max > 0)
144 int n = in.read(buff);
149 bout.write(buff, 0, n);
154 return bout.toByteArray();
157 private static boolean isMagicPresent(byte[] link)
159 final int magic = 0x0000004C;
160 final int magic_offset = 0x00;
161 return link.length >= 32 && bytesToDword(link, magic_offset) == magic;
165 * Gobbles up link data by parsing it and storing info in member fields
168 * all the bytes from the .lnk file
170 private void parseLink(byte[] link) throws ParseException
174 if (!isMagicPresent(link))
175 throw new ParseException("Invalid shortcut; magic is missing", 0);
177 // get the flags byte
178 byte flags = link[0x14];
180 // get the file attributes byte
181 final int file_atts_offset = 0x18;
182 byte file_atts = link[file_atts_offset];
183 byte is_dir_mask = (byte) 0x10;
184 if ((file_atts & is_dir_mask) > 0)
193 // if the shell settings are present, skip them
194 final int shell_offset = 0x4c;
195 final byte has_shell_mask = (byte) 0x01;
197 if ((flags & has_shell_mask) > 0)
199 // the plus 2 accounts for the length marker itself
200 shell_len = bytesToWord(link, shell_offset) + 2;
203 // get to the file settings
204 int file_start = 0x4c + shell_len;
206 final int file_location_info_flag_offset_offset = 0x08;
207 int file_location_info_flag = link[file_start
208 + file_location_info_flag_offset_offset];
209 isLocal = (file_location_info_flag & 2) == 0;
210 // get the local volume and local system values
211 // final int localVolumeTable_offset_offset = 0x0C;
212 final int basename_offset_offset = 0x10;
213 final int networkVolumeTable_offset_offset = 0x14;
214 final int finalname_offset_offset = 0x18;
215 int finalname_offset = link[file_start + finalname_offset_offset]
217 String finalname = getNullDelimitedString(link, finalname_offset);
220 int basename_offset = link[file_start + basename_offset_offset]
222 String basename = getNullDelimitedString(link, basename_offset);
223 real_file = basename + finalname;
227 int networkVolumeTable_offset = link[file_start
228 + networkVolumeTable_offset_offset] + file_start;
229 int shareName_offset_offset = 0x08;
230 int shareName_offset = link[networkVolumeTable_offset
231 + shareName_offset_offset] + networkVolumeTable_offset;
232 String shareName = getNullDelimitedString(link, shareName_offset);
233 real_file = shareName + "\\" + finalname;
235 } catch (ArrayIndexOutOfBoundsException e)
237 throw new ParseException(
238 "Could not be parsed, probably not a valid WindowsShortcut",
243 private static String getNullDelimitedString(byte[] bytes, int off)
246 // count bytes until the null character (0)
249 if (bytes[off + len] == 0)
255 return new String(bytes, off, len);
259 * convert two bytes into a short note, this is little endian because it's
260 * for an Intel only OS.
262 private static int bytesToWord(byte[] bytes, int off)
264 return ((bytes[off + 1] & 0xff) << 8) | (bytes[off] & 0xff);
267 private static int bytesToDword(byte[] bytes, int off)
269 return (bytesToWord(bytes, off + 2) << 16) | bytesToWord(bytes, off);