Java 二进制文件提取库中的通用纯文本
我在寻找一个通用的非专业纯文本文件提取器 首先,在人们大喊之前,看看ApacheTika——我的回答是,它只支持一些流行的二进制文件格式,如Office、BMPs等Java 二进制文件提取库中的通用纯文本,java,text-extraction,Java,Text Extraction,我在寻找一个通用的非专业纯文本文件提取器 首先,在人们大喊之前,看看ApacheTika——我的回答是,它只支持一些流行的二进制文件格式,如Office、BMPs等 回到问题上来——许多二进制文件中都嵌入了文本字符串,我希望在没有二进制字节噪声的情况下提取这些字符串。这意味着它可以在EXE等文件中找到简单的文本字符串序列,结果只包含ascii字。我试着在谷歌上搜索,但找不到任何能做到这一点的东西。我的基本想法是,如果一个文件不是由TIKA处理的,这个简单的二进制文件处理程序将尽力查找这些文本字符
回到问题上来——许多二进制文件中都嵌入了文本字符串,我希望在没有二进制字节噪声的情况下提取这些字符串。这意味着它可以在EXE等文件中找到简单的文本字符串序列,结果只包含ascii字。我试着在谷歌上搜索,但找不到任何能做到这一点的东西。我的基本想法是,如果一个文件不是由TIKA处理的,这个简单的二进制文件处理程序将尽力查找这些文本字符串。下面的代码过滤不可打印的ASCII字符
package sandbox;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author yan-cheng.cheok
*/
public class Main {
// Returns the contents of the file in a byte array.
public static byte[] getBytesFromFile(File file) throws IOException {
InputStream is = new FileInputStream(file);
// Get the size of the file
long length = file.length();
// You cannot create an array using a long type.
// It needs to be an int type.
// Before converting to an int type, check
// to ensure that file is not larger than Integer.MAX_VALUE.
if (length > Integer.MAX_VALUE) {
// File is too large
}
// Create the byte array to hold the data
byte[] bytes = new byte[(int)length];
// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file "+file.getName());
}
// Close the input stream and return bytes
is.close();
return bytes;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
File f = new File("c:\\jstock.exe");
byte[] bs = getBytesFromFile(f);
List<Byte> list = new ArrayList<Byte>();
for (byte b : bs) {
if (b >= 0) {
// Printable ASCII code.
list.add(b);
}
}
byte[] output = new byte[list.size()];
for (int i = 0, size = list.size(); i < size; i++) {
output[i] = list.get(i);
}
System.out.println(new String(output));
}
}
包沙盒;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.util.ArrayList;
导入java.util.List;
/**
*
*@author-yan-cheng.cheok
*/
公共班机{
//以字节数组的形式返回文件的内容。
公共静态字节[]getBytesFromFile(文件文件)引发IOException{
InputStream is=新文件InputStream(文件);
//获取文件的大小
long length=file.length();
//不能使用长类型创建数组。
//它必须是int类型。
//在转换为int类型之前,请检查
//以确保文件不大于Integer.MAX_值。
if(长度>整数最大值){
//文件太大
}
//创建字节数组以保存数据
字节[]字节=新字节[(int)长度];
//读入字节
整数偏移=0;
int numRead=0;
while(偏移量=0){
偏移量+=numRead;
}
//确保已读入所有字节
if(偏移量<字节长度){
抛出新IOException(“无法完全读取文件”+file.getName());
}
//关闭输入流并返回字节
is.close();
返回字节;
}
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args)引发异常{
文件f=新文件(“c:\\jstock.exe”);
字节[]bs=getBytesFromFile(f);
列表=新的ArrayList();
for(字节b:bs){
如果(b>=0){
//可打印的ASCII码。
列表.添加(b);
}
}
字节[]输出=新字节[list.size()];
for(int i=0,size=list.size();i
我最终编写了代码类来解决我的问题
重要特点/注意事项
- 仅接受cr、nl、制表符、空格-char127-
- 忽略所有非ascii字符
- 如果文件包含unicode,则会带来厄运
- 忽略少于几个字符的字符序列(可配置)。
- 这意味着忽略由其他非ASCII值包围的单个字母的字节
- 在字符序列之间插入空格
- 这意味着一个字符串、一些字节和另一个字符串在结果中显示为两个由字符串分隔的单词,而不是单个长单词