How to get the details and properties of files (such as EXE files) under Windows and Linux

Source: Internet
Author: User
Tags unpack

Programmers are lazy, you know!

Recently in the project development, the EXE developed by the CS program, need to automatically upgrade, the EXE program is placed under Linux, the automatic upgrade will not detect the version number of the EXE program, but our client's EXE needs to get the new program version number information on the server. Finally, I use Java to implement the Linux EXE file version number read function. Here is the detailed code:

Package Com.herman.utils;import Java.io.file;import Java.io.filenotfoundexception;import java.io.IOException;  Import java.io.randomaccessfile;/** * @see Tool class for obtaining file information * @author Herman.xiong * @date May 12, 2014 15:01:26 * @version V1.0 *  @since Tomcat 6.0, JDK 1.6 */public class Fileutil {/** * @see get version information * @param filePath * @return */public static String GetVersion (String filePath) {File File = new file (FilePath); Randomaccessfile RAF = null;byte[] buffer;  String Str;try {RAF = new Randomaccessfile (file, "R"), buffer = new Byte[64];raf.read (buffer), str = "" + (char) buffer[0] + (char) buffer[1];if (! " MZ ". Equals (str)) {return null;} int peoffset = unpack (new byte[] {buffer[60], buffer[61],buffer[62], buffer[63]}); if (Peoffset <) {return null;} Raf.seek (peoffset), buffer = new Byte[24];raf.read (buffer), str = "" + (char) buffer[0] + (char) buffer[1];if (!) PE ". Equals (str)) {return null;} int machine = unpack (new byte[] {buffer[4], buffer[5]}); if (machine! = 332) {return null;} int Nosections = Unpack (new byte[] {buffer[6], buffer[7]}), int opthdrsize = unpack (new byte[] {buffer[20], buffer[21]}); Raf.seek (RA F.getfilepointer () + opthdrsize); Boolean resfound = false;for (int i = 0; i < nosections; i++) {buffer = new Byte[40];r Af.read (buffer), str = "" + (char) buffer[0] + (char) buffer[1]+ (char) buffer[2] + (char) buffer[3]+ (char) buffer[4];if ( ". rsrc". Equals (str)) {resfound = True;break;}} if (!resfound) {return null;} int infovirt = unpack (new byte[] {buffer[12], buffer[13],buffer[14], buffer[15]}); int infosize = unpack (new byte[] {BUF FER[16], buffer[17],buffer[18], buffer[19]}); int infooff = unpack (new byte[] {buffer[20], buffer[21],buffer[22], buffer Raf.seek (infooff); buffer = new Byte[infosize];raf.read (buffer); int numdirs = unpack (new byte[] {buffer[14], buff ER[15]}); Boolean infofound = False;int Suboff = 0;for (int i = 0; i < numdirs; i++) {int type = unpack (new byte[] {BU Ffer[i * 8 + 16],buffer[i * 8 + +], Buffer[i * 8 + 18],buffer[i * 8 + 19]}), if (type = =) {//FILEINFO Resourceinfofound = True;suboff = Unpack (new byte[] {Buffer[i * 8 + 20],buffer[i * 8 + Buffer[i * 8 + 22],buffer[i * 8 +]}); break;}} if (!infofound) {return null;} Suboff = suboff & 0x7fffffff;infooff = unpack (new byte[] {Buffer[suboff + 20],buffer[suboff + +], Buffer[suboff + 22 ],buffer[suboff + 23]}); Offset of First Fileinfoinfooff = Infooff & 0x7fffffff;infooff = unpack (new byte[] {Buffer[infooff + 20],buffer[in Fooff +], Buffer[infooff + 22],buffer[infooff + 23]});  Offset to Dataint Dataoff = unpack (new byte[] {Buffer[infooff],buffer[infooff + 1], Buffer[infooff + 2],buffer[infooff + 3]});d Ataoff = Dataoff-infovirt;int Version1 = unpack (new byte[] {Buffer[dataoff + 48],buffer[dataoff + 48 + 1]});  int version2 = unpack (new byte[] {Buffer[dataoff + + + 2],buffer[dataoff + 3]}); int version3 = unpack (new byte[] { Buffer[dataoff + + + 4],buffer[dataoff + 5]}); int version4 = unpack (new byte[] {Buffer[daTaoff + + + 6],buffer[dataoff + 48 + 7]}); System.out.println (Version2 + "." + Version1 + "." + Version4 + "." + Version3); return Version2 + "." + Version1 + "." + Version4 + "." + Version3;} catch (FileNotFoundException e) {return null;} catch (IOException e) {return null;} finally {if (RAF! = null) {try {raf.cl OSE ();} catch (IOException e) {}}}}public static int unpack (byte[] b) {int num = 0;for (int i = 0; i < b.length; i++) {num = 25 6 * num + (b[b.length-1-i] & 0xff);} return num;}}
Run (Windows and Linux filepath to correct themselves correctly):


Click to download the detailed demo project:http://download.csdn.net/detail/xmt1139057136/7335155

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.