引入jar包:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
<dependency>
<groupId>com.kichik.pecoff4j</groupId>
<artifactId>pecoff4j</artifactId>
<version>0.3.1</version>
</dependency>
代码实现:
import com.alibaba.fastjson.JSONObject;
import com.kichik.pecoff4j.PE;
import com.kichik.pecoff4j.ResourceDirectory;
import com.kichik.pecoff4j.ResourceEntry;
import com.kichik.pecoff4j.constant.ResourceType;
import com.kichik.pecoff4j.io.PEParser;
import com.kichik.pecoff4j.io.ResourceParser;
import com.kichik.pecoff4j.resources.StringFileInfo;
import com.kichik.pecoff4j.resources.StringTable;
import com.kichik.pecoff4j.resources.VersionInfo;
import com.kichik.pecoff4j.util.ResourceHelper;
import java.io.IOException;
public class ExeFileDetailInfo {
public static JSONObject getExeProductInfo(String filePath) throws IOException {
JSONObject jObj = new JSONObject();
PE pe = PEParser.parse(filePath);
ResourceDirectory rd = pe.getImageData().getResourceTable();
ResourceEntry[] entries = ResourceHelper.findResources(rd, ResourceType.VERSION_INFO);
for (int i = 0; i < entries.length; i++) {
byte[] data = entries[i].getData();
VersionInfo version = ResourceParser.readVersionInfo(data);
StringFileInfo strings = version.getStringFileInfo();
StringTable table = strings.getTable(0);
for (int j = 0; j < table.getCount(); j++) {
String key = table.getString(j).getKey();
String value = table.getString(j).getValue();
System.out.println(key + " = " + value);
jObj.put(key,value);
}
}
return jObj;
}
public static void main(String[] args) throws IOException {
String filePath = "D:\\ChromeStandaloneSetup64.exe";
JSONObject tmp = getExeProductInfo(filePath);
System.out.println("详细信息:" + tmp);
System.out.println("产品名称:" + tmp.getString("ProductName"));
System.out.println("产品版本:" + tmp.getString("ProductVersion"));
}
}
结果输出:
- CompanyName = Google LLC
- FileDescription = Google Update Setup
- FileVersion = 1.3.35.301
- InternalName = Google Update Setup
- LegalCopyright = Copyright 2018 Google LLC
- OriginalFilename = GoogleUpdateSetup.exe
- ProductName = Google Update
- ProductVersion = 1.3.35.301
- LanguageId = en
- 详细信息:{"CompanyName":"Google LLC","FileDescription":"Google Update Setup","LegalCopyright":"Copyright 2018 Google LLC","OriginalFilename":"GoogleUpdateSetup.exe","ProductName":"Google Update","FileVersion":"1.3.35.301","ProductVersion":"1.3.35.301","InternalName":"Google Update Setup","LanguageId":"en"}
- 产品名称:Google Update
- 产品版本:1.3.35.301