【问题标题】:How to convert binary text into useable file如何将二进制文本转换为可用文件
【发布时间】:2013-12-25 22:39:48
【问题描述】:

所以我使用以下方法

(文件通过'convertFileToByteArray()'转换为字节数组,然后通过'convertByteArrayToBitTextFile()'写入.txt文件

将任何类型的文件转换为二进制文本文件(我的意思是只有人类可读形式的 1 和 0。)

public static byte[] convertFileToByteArray(String path) throws IOException
{
    File file = new File(path);
    byte[] fileData;
    fileData = new byte[(int)file.length()];
    FileInputStream in = new FileInputStream(file);
    in.read(fileData);
    in.close();
    return fileData;
}

public static boolean convertByteArrayToBitTextFile(String path, byte[] bytes)
{
    String content = convertByteArrayToBitString(bytes);
    try
    {
        PrintWriter out = new PrintWriter(path);
        out.println(content);
        out.close();
        return true;
    }
    catch (FileNotFoundException e)
    {
        return false;
    }
}

public static String convertByteArrayToBitString(byte[] bytes)
{
    String content = "";
    for (int i = 0; i < bytes.length; i++)
    {
        content += String.format("%8s", Integer.toBinaryString(bytes[i] & 0xFF)).replace(' ', '0');
    }
    return content;
}

编辑:附加代码:

public static byte[] convertFileToByteArray(String path) throws IOException
{
    File file = new File(path);
    byte[] fileData;
    fileData = new byte[(int)file.length()];
    FileInputStream in = new FileInputStream(file);
    in.read(fileData);
    in.close();
    return fileData;
}

public static boolean convertByteArrayToBitTextFile(String path, byte[] bytes)
{
    try
    {
        PrintWriter out = new PrintWriter(path);
        for (int i = 0; i < bytes.length; i++)
        {
            out.print(String.format("%8s", Integer.toBinaryString(bytes[i] & 0xFF)).replace(' ', '0'));
        }
        out.close();
        return true;
    }
    catch (FileNotFoundException e)
    {
        return false;
    }
}

public static boolean convertByteArrayToByteTextFile(String path, byte[] bytes)
{
    try
    {
        PrintWriter out = new PrintWriter(path);
        for(int i = 0; i < bytes.length; i++)
        {
            out.print(bytes[i]);
        }
        out.close();
        return true;
    }
    catch (FileNotFoundException e)
    {
        return false;
    }
}

public static boolean convertByteArrayToRegularFile(String path, byte[] bytes)
{
    try
    {
        PrintWriter out = new PrintWriter(path);
        for(int i = 0; i < bytes.length; i++)
        {
            out.write(bytes[i]);
        }
        out.close();
        return true;
    }
    catch (FileNotFoundException e)
    {
        return false;
    }
}

public static boolean convertBitFileToByteTextFile(String path) 
{
    try
    {
        byte[] b = convertFileToByteArray(path);
        convertByteArrayToByteTextFile(path, b);
        return true;
    }
    catch (IOException e)
    {
        return false;
    }

}

我这样做是为了在一个非常基础的层面上尝试压缩方法,所以我们不要讨论为什么要使用人类可读的形式。

到目前为止,这工作得很好,但是我遇到了两个问题。

1) 它需要永远(> 20 分钟将 230KB 转换为二进制文本)。这只是相对复杂的转换的副产品,还是有其他方法可以更快地做到这一点?

2) 主要问题: 我不知道如何将文件转换回原来的样子。从 .txt 重命名为 .exe 不起作用(不足为奇,因为生成的文件比原始文件大两倍)

这是否仍然可能,或者我是否通过将文件转换为人类可读的文本文件而丢失了有关文件应该代表什么的信息? 如果是这样,您知道有什么替代方法可以防止这种情况发生吗?

感谢任何帮助。

【问题讨论】:

    标签: java binary file-conversion


    【解决方案1】:

    花费你最多时间的是构造一个不断增长的字符串。更好的方法是在获得数据后立即写入数据。

    另一个问题很简单。您知道每个八个字符(“0”或“1”)的序列都是由一个字节组成的。因此,您知道 8 字符块中每个字符的值:

    01001010
           ^----- 0*1
          ^------ 1*2
         ^------- 0*4
        ^-------- 1*8
       ^--------- 0*16
      ^---------- 0*32
     ^----------- 1*64
    ^------------ 0*128
                  -----
                  64+8+2 = 74
    

    您只需要添加存在“1”的值。

    你可以像这样在 Java 中做到这一点,甚至不知道各个位的值:

    String sbyte = "01001010";
    int bytevalue = 0;
    for (i=0; i<8; i++) {
        bytevalue *= 2;           // shifts the bit pattern to the left 1 position
        if (sbyte.charAt(i) == '1') bytevalue += 1;
    }
    

    【讨论】:

    • 哇,将它缩短到 2 秒。非常感谢!
    • 关于2):感谢您的详尽解释!但是,如果我将可执行文件更改为位文本,然后使用您的方法将过程反转(位到字节)并将其保存为 oldname.exe,它仍然不可执行(基本上只包含字节而不是 cleantext 中的位)。有什么想法吗?
    • @JonasBartkowski 可能您使用了一些打印方法?您应该使用某些 FileOutputStream 的 write(int) 方法。
    • 谢谢!但仍然没有:/我目前使用 FileInputStream in.read((int) File.length) 从文件中读取字节 (byte[]),然后遍历该数组并使用 FileOutputStream out.write(byte[i] ) 写入新文件。但不知何故,这并没有改变。 (仍然是干净的文本字节和双倍大小)
    • 双倍尺寸让我觉得肯定有其他问题 - 你应该有 exactly 8 倍尺寸。再次检查,如果所有其他方法都失败,请发布重建原始文件的代码,我明天会研究它。
    【解决方案2】:
    1. 使用StringBuilder 避免生成大量未使用的String 实例。
      更好的是,直接写入PrintWriter,而不是在内存中构建它。

    2. 遍历每个 8 个字符的子序列并调用 Byte.parseByte(text, 2) 将其解析回一个字节。

    【讨论】:

    • 也非常感谢您对 1) 的回答。我目前正在尝试您的解决方案 2) (:
    • 我遇到了与 Ingo 方法相同的问题。您知道为什么重新转换后它无法执行吗?
    猜你喜欢
    • 1970-01-01
    • 2016-03-30
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-01
    • 2023-03-30
    • 2015-11-07
    相关资源
    最近更新 更多