Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 316|回复: 0

[JavaIO学习]Java 解析windows 快捷方式文件的方法

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-11-3 23:58:31 | 显示全部楼层 |阅读模式
    .lnk文件是 Windows 系统中的快捷方式文件,本例子是 java 对这种文件的解析。

    1. import java.io.ByteArrayOutputStream;
    2. import java.io.File;
    3. import java.io.FileInputStream;
    4. import java.text.DecimalFormat;
    5. import java.text.NumberFormat;
    6.          
    7. public class LnkParser {
    8.          
    9.     public LnkParser(File f) throws Exception {
    10.         parse(f);
    11.     }
    12.          
    13.     private boolean is_dir;
    14.     public boolean isDirectory() {
    15.         return is_dir;
    16.    }
    17.          
    18.     private String real_file;
    19.     public String getRealFilename() {
    20.         return real_file;
    21.     }
    22.          
    23.    public void parse(File f) throws Exception {
    24.         // read the entire file into a byte buffer
    25.         FileInputStream fin = new FileInputStream(f);
    26.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
    27.         byte[] buff = new byte[256];
    28.         while(true) {
    29.             int n = fin.read(buff);
    30.             if(n == -1) { break; }
    31.             bout.write(buff,0,n);
    32.         }
    33.         fin.close();
    34.         byte[] link = bout.toByteArray();


    35.         // get the flags byte
    36.         byte flags = link[0x14];

    37.         // get the file attributes byte
    38.         final int file_atts_offset = 0x18;
    39.         byte fileatts = link[file_atts_offset];
    40.         byte is_dir_mask = (byte)0x10;
    41.         if((fileatts & is_dir_mask) > 0) {
    42.             is_dir = true;
    43.         } else {
    44.             is_dir = false;
    45.         }
    46.          
    47.         // if the shell settings are present, skip them
    48.         final int shell_offset = 0x4c;
    49.         int shell_len = 0;
    50.         if((flags & 0x1) > 0) {
    51.             // the plus 2 accounts for the length marker itself
    52.             shell_len = bytes2short(link,shell_offset) + 2;
    53.         }
    54.          
    55.         // get to the file settings
    56.         int file_start = 0x4c + shell_len;
    57.          
    58.         // get the local volume and local system values
    59.         int local_sys_off = link[file_start+0x10] + file_start;
    60.         real_file = getNullDelimitedString(link,local_sys_off);
    61.         p("real filename = " + real_file);
    62.     }
    63.          
    64.     static String getNullDelimitedString(byte[] bytes, int off) {
    65.         int len = 0;
    66.         // count bytes until the null character (0)
    67.         while(true) {
    68.             if(bytes[off+len] == 0) {
    69.                 break;
    70.             }
    71.             len++;
    72.         }
    73.         return new String(bytes,off,len);
    74.     }
    75.          
    76.     // convert two bytes into a short
    77.     // note, this is little endian because it"s for an
    78.     // Intel only OS.
    79.     static int bytes2short(byte[] bytes, int off) {
    80.         return bytes[off] | (bytes[off+1] << 8);
    81.     }
    82.          
    83.             /*
    84.             static int norm(byte b) {
    85.                 if(b < 0) {
    86.                     b+=128;
    87.                 }
    88.                 return b;
    89.             }
    90.             static int bytes2int(byte[] arr, int off) {
    91.                 int b1 = norm(arr[off]);
    92.                 int b2 = norm(arr[off+1]);
    93.                 int b3 = norm(arr[off+2]);
    94.                 int b4 = norm(arr[off+3]);
    95.                 int val =  (
    96.                             (b1  << 0) |
    97.                             (b2  << 8) |
    98.                             (b3  << 16) |
    99.                             (b4  << 24)
    100.                             );
    101.                 //p("bytes2int: " + b1 + " " + b2 + " " + b3 + " " + b4);
    102.                 return val;
    103.             }
    104.          
    105.          
    106.             static NumberFormat num_format = new DecimalFormat(" 000;-000");

    107.             public static String padd(String str, int len) {
    108.                 while(str.length() < len) {
    109.                     str = " " + str;
    110.                 }
    111.                 return str;
    112.             }
    113.          
    114.             public static void pw(byte[] arr, int off) {
    115.                 StringBuffer top = new StringBuffer();
    116.                 StringBuffer mid = new StringBuffer();
    117.                 StringBuffer bot = new StringBuffer();
    118.                 top.append("--");
    119.                 mid.append("  ");
    120.                 bot.append("  ");
    121.          
    122.                 for(int i=0; i< 16; i++) {
    123.                     int val = arr[off+i];
    124.                     String str = Integer.toHexString(off+i);
    125.                     top.append(padd(str,5));
    126.                     mid.append(padd(""+val,5));
    127.                     if(val < 0) {
    128.                         val += 128;
    129.                     }
    130.                     if(val >= " " && val <= "~") {
    131.                         str = "" + (char)val;
    132.                     } else {
    133.                         str = Integer.toHexString(val);
    134.                     }
    135.                     str = padd(str,5);
    136.                     bot.append(str);
    137.                     if(i%4==3) {
    138.                         top.append("    ");
    139.                         mid.append("    ");
    140.                         bot.append("    ");
    141.                     }
    142.                 }
    143.                 p(top.toString());
    144.                 p(mid.toString());
    145.                 p(bot.toString());
    146.             }
    147.             public static void pbits(byte bt) {
    148.                 p("byte = " + bt + " " + Integer.toHexString(bt) + " " + Integer.toBinaryString(bt));
    149.             }*/
    150.          
    151.     public static void p(String str) {
    152.         System.out.println(str);
    153.     }
    154.    public static void main(String args[]) throws Exception {
    155.        File f=new File(args[0]);
    156.        new LnkParser(f);
    157.    }
    158. }
    复制代码

       
         
         
          
          

            
          

            
          
         
       

      


    源码下载:http://file.javaxxz.com/2014/11/3/235831375.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-25 14:31 , Processed in 0.327887 second(s), 34 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表