How to obtain detailed information and attributes of files (such as exe files) in windows and linux

Source: Internet
Author: User
Tags unpack

Programmers are very lazy, you know!

Recently, during project development, the exe program developed by cs needs to be automatically upgraded. The exe program is placed in linux and cannot be detected during automatic upgrade, however, the exe program on our client needs to obtain the version number of the new program on the server. Finally, I use java to implement the linux exe file version number reading function. The detailed code is as follows:

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 File = new Fi Le (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 <64) {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 (raf. getFilePointer () + optHdrSize); boolean resFound = false; for (int I = 0; I <noSections; I ++) {buffer = new byte [40]; raf. read (buffer); str = "" + (char) buffer [0] + (char) buffer [1] + (char) buffer [2] + (char) buffer [3] + (char) buffer [4]; if (". rs Rc ". 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 [] {buffer [16], buffer [17], buffer [18], buffer [19]}); int infoOff = unpack (new byte [] {buffer [20], buffer [21], buffer [22], buffer [23]}); raf. seek (infoOff); buffer = new byte [infoSize]; raf. read (buffer); int numDirs = unpack (new byte [] {buffer [14], buffer [15]}); boo Lean infoFound = false; int subOff = 0; for (int I = 0; I <numDirs; I ++) {int type = unpack (new byte [] {buffer [I * 8 + 16], buffer [I * 8 + 17], buffer [I * 8 + 18], buffer [I * 8 + 19]}); if (type = 16) {// FILEINFO resourceinfoFound = true; subOff = unpack (new byte [] {buffer [I * 8 + 20], buffer [I * 8 + 21], buffer [I * 8 + 22], buffer [I * 8 + 23]}); break ;}} if (! InfoFound) {return null;} subOff = subOff & 0x7fffffff; infoOff = unpack (new byte [] {buffer [subOff + 20], buffer [subOff + 21], buffer [subOff + 22], buffer [subOff + 23]}); // offset of first FILEINFOinfoOff = infoOff & 0x7fffffff; infoOff = unpack (new byte [] {buffer [infoOff + 20], buffer [infoOff + 21], buffer [infoOff + 22], buffer [infoOff + 23]}); // offset to dataint dataOff = unpack (new byte [] {buff Er [infoOff], buffer [infoOff + 1], buffer [infoOff + 2], buffer [infoOff + 3]}); dataOff = dataOff-infoVirt; int version1 = unpack (new byte [] {buffer [dataOff + 48], buffer [dataOff + 48 + 1]}); int version2 = unpack (new byte [] {buffer [dataOff + 48 + 2], buffer [dataOff + 48 + 3]}); int version3 = unpack (new byte [] {buffer [dataOff + 48 + 4], buffer [dataOff + 48 + 5]}); int version4 = unpack (new byte [] {Buffer [dataOff + 48 + 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. close () ;}catch (IOException e) {}}} public static int unpack (byte [] B) {int num = 0; for (int I = 0; I <B. length; I ++) {num = 256 * num + (B [B. length-1-I] & 0xff);} return num ;}}
Run (correct filePath on Windows and linux ):


Click to download a 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.