java读取文件夹快捷方式_Java 解析 lnk 快捷方式文件的方法(转)

packagefile.extendsion;importjava.io.ByteArrayOutputStream;importjava.io.File;importjava.io.FileInputStream;public classLnkParser {// http://www.oschina.net/code/snippet_12_274

public static void main(String[] args) throwsException {new LnkParser(new File("e:/eclipse.lnk"));

}public LnkParser(File f) throwsException {

parse(f);

}private booleanis_dir;public booleanisDirectory() {returnis_dir;

}privateString real_file;publicString getRealFilename() {returnreal_file;

}public void parse(File f) throwsException {//read the entire file into a byte buffer

FileInputStream fin = newFileInputStream(f);

ByteArrayOutputStream bout= newByteArrayOutputStream();byte[] buff = new byte[256];while (true) {int n =fin.read(buff);if (n == -1) {break;

}

bout.write(buff,0, n);

}

fin.close();byte[] link =bout.toByteArray();//get the flags byte

byte flags = link[0x14];//get the file attributes byte

final int file_atts_offset = 0x18;byte fileatts =link[file_atts_offset];byte is_dir_mask = (byte) 0x10;if ((fileatts & is_dir_mask) > 0) {

is_dir= true;

}else{

is_dir= false;

}//if the shell settings are present, skip them

final int shell_offset = 0x4c;int shell_len = 0;if ((flags & 0x1) > 0) {//the plus 2 accounts for the length marker itself

shell_len = bytes2short(link, shell_offset) + 2;

}//get to the file settings

int file_start = 0x4c +shell_len;//get the local volume and local system values

int local_sys_off = link[file_start + 0x10] +file_start;

real_file=getNullDelimitedString(link, local_sys_off);

p("real filename = " +real_file);

}static String getNullDelimitedString(byte[] bytes, intoff) {int len = 0;//count bytes until the null character (0)

while (true) {if (bytes[off + len] == 0) {break;

}

len++;

}return newString(bytes, off, len);

}//convert two bytes into a short//note, this is little endian because//it's for an//Intel only OS.

static int bytes2short(byte[] bytes, intoff) {return bytes[off] | (bytes[off + 1] << 8);

}/** static int norm(byte b) { if(b < 0) { b+=128; } return b; } static int

* bytes2int(byte[] arr, int off) { int b1 = norm(arr[off]); int b2 =

* norm(arr[off+1]); int b3 = norm(arr[off+2]); int b4 = norm(arr[off+3]);

* int val = ( (b1 << 0) | (b2 << 8) | (b3 << 16) | (b4 << 24) );

* //p("bytes2int: " + b1 + " " + b2 + " " + b3 + " " + b4); return val; }

*

*

* static NumberFormat num_format = new DecimalFormat(" 000;-000");

*

* public static String padd(String str, int len) { while(str.length() <

* len) { str = " " + str; } return str; }

*

* public static void pw(byte[] arr, int off) { StringBuffer top = new

* StringBuffer(); StringBuffer mid = new StringBuffer(); StringBuffer bot =

* new StringBuffer(); top.append("--"); mid.append(" "); bot.append(" ");

*

* for(int i=0; i<16; i++) { int val = arr[off+i]; String str =

* Integer.toHexString(off+i); top.append(padd(str,5));

* mid.append(padd(""+val,5)); if(val < 0) { val += 128; } if(val >= ' ' &&

* val <= '~') { str = "" + (char)val; } else { str =

* Integer.toHexString(val); } str = padd(str,5); bot.append(str);

* if(i%4==3) { top.append(" "); mid.append(" "); bot.append(" ");

* } } p(top.toString()); p(mid.toString()); p(bot.toString()); } public

* static void pbits(byte bt) { p("byte = " + bt + " " +

* Integer.toHexString(bt) + " " + Integer.toBinaryString(bt)); }*/

public static voidp(String str) {

System.out.println(str);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值