import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* 网络快捷方式读取目标文件地址
* @author RuiLin.Xie - xKF24276
*
*/
public class NetLink
{
public static void main(String[] args) throws IOException
{
File file = new File(
"C:\\快捷方式 到 VSS 在 192.168.1.5 上.lnk");
System.out.println(getLnkFile(file));
}
public static String getLnkFile(File lnkFile)
{
RandomAccessFile r = null;
ByteArrayOutputStream bos = null;
String filename = null;
try
{
r = new RandomAccessFile(lnkFile, "r");
byte[] bys = new byte[4];
// 定位到 Shell item ID list 段的长度定义起始地址
// 以便于计算下一段(即文件位置信息段的起始地址)
r.seek(0x4c);
r.read(bys, 0, 2);
int offset = bytes2Int(bys, 0, 2);
// 获得文件位置信息段的起始地址
int fileLocationInfoSagement = offset + 0x4e;
// 获得网络路径信息段的起始地址(此处与读取本地不同)
int filePathInfoSagement = fileLocationInfoSagement + 0x30;
// 定位到网络路径信息段,以便获得网络路径信息的偏移地址
r.seek(filePathInfoSagement);
bos = new ByteArrayOutputStream();
// for (int i = 0; i < (r.length() - filePathInfoSagement - 0x6); i++)
// {
// byte b = r.readByte();
// bos.write(b);
// }
//因为遇到中文时中间会有空格,所以需要将空格转为"\"后再读一次
for (byte b = 0; (b = r.readByte()) != 0;)
{
bos.write(b);
}
//前面将0去掉,现加入"\"
bos.write(92);
for (byte b = 0; (b = r.readByte()) != 0;)
{
bos.write(b);
}
// 将读出路径信息字节存入 byte 数组中
bys = bos.toByteArray();
// 采用本地编码获得路径信息文件名称
filename = new String(bys);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (bos != null)
{
try
{
bos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
if (r != null)
{
try
{
r.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
return filename;
}
public static int bytes2Int(byte[] bys, int start, int len)
{
int n = 0;
for (int i = start, k = start + len % 5; i < k; i++)
{
n += (bys[i] & 0xff) << (i * 8);
}
return n;
}
}
网络快捷方式读取目标文件地址
最新推荐文章于 2022-02-21 10:23:56 发布