如何使用 Java 的 IO 读取 Windows NTFS 的备用数据流?

Pt.*_*erk 6 java io ntfs java-io alternate-data-stream

我试图让我的 Java 应用程序读取给定路径中的所有数据。文件、目录、元数据等。这还包括 NTFS 称为备用数据流 (ADS) 的一个奇怪的东西。

显然,它就像目录或文件中的第二层数据。您可以打开命令提示符并使用“:”在ADS中创建文件,例如:

C:\ADSTest> echo test>:ads.txt
Run Code Online (Sandbox Code Playgroud)

所以,

C:\ADSTest> notepad :ads.txt
Run Code Online (Sandbox Code Playgroud)

应该打开一个记事本,其中包含字符串“test”。但是,如果您这样做:

C:\ADSTest> dir
Run Code Online (Sandbox Code Playgroud)

您将无法看到 ads.txt。但是,如果您使用显示 ADS 数据的 dir 选项,您将能够看到它:

C:\ADSTest> dir /r
MM/dd/yyyy hh:mm            .:ads.txt
Run Code Online (Sandbox Code Playgroud)

现在,我知道Java IO具有读取ADS的能力。我怎么知道呢?嗯,Oracle 的文档明确指出了这一点

如果文件系统实现支持的文件属性不足以满足您的需求,您可以使用 UserDefinedAttributeView 创建和跟踪您自己的文件属性。

一些实现将此概念映射到 NTFS 替代数据流等功能以及文件系统(如 ext3 和 ZFS)上的扩展属性。

另外,随机论坛上的随机帖子:D

数据存储在 NTFS 备用数据流 (ADS) 中,可通过 Java IO 读取(我已经测试过)。

问题是,我找不到任何可以解析ADS的预先编写的文件属性查看器,而且我不明白如何编写自己的ADS解析器。我是一名初学者程序员,所以我觉得这超出了我的能力范围。有人可以帮助我或指出我正确的方向吗?

解决方案

编辑:在@knosrtum的帮助下,我能够编写一种方法,该方法将从给定路径返回所有已解析的ADS信息作为字符串ArrayList(它也可以轻松编辑为文件ArrayList)。以下是任何可能需要它的人的代码:

public class ADSReader {

    public static ArrayList<String> start(Path toParse) {

        String path = toParse.toString();
        ArrayList<String> parsedADS = new ArrayList<>();

        final String command = "cmd.exe /c dir " + path + " /r"; // listing of given Path.

        final Pattern pattern = Pattern.compile(
                "\\s*"                 // any amount of whitespace
                        + "[0123456789,]+\\s*"   // digits (with possible comma), whitespace
                        + "([^:]+:"    // group 1 = file name, then colon,
                        + "[^:]+:"     // then ADS, then colon,
                        + ".+)");      // then everything else.

        try {
            Process process = Runtime.getRuntime().exec(command);
            process.waitFor();
            try (BufferedReader br = new BufferedReader(
                    new InputStreamReader(process.getInputStream()))) {
                String line;

                while ((line = br.readLine()) != null) {
                    Matcher matcher = pattern.matcher(line);
                    if (matcher.matches()) {
                        parsedADS.add((matcher.group(1)));
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        for (int z = 0; z<parsedADS.size(); z++)
            System.out.println(parsedADS.get(z));

        return parsedADS;

    }
}
Run Code Online (Sandbox Code Playgroud)

ksn*_*tum 4

我只需使用语法“file_name:stream_name”打开文件即可读取文件的 ADS。所以如果你已经这样做了:

C:>echo Hidden text > test.txt:hidden
Run Code Online (Sandbox Code Playgroud)

那么你应该能够这样做:

package net.snortum.play;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class AdsPlay {
    public static void main(String[] args) {
        new AdsPlay().start();
    }

    private void start() {
        File file = new File("test.txt:hidden");
        try (BufferedReader bf = new BufferedReader( new FileReader(file))) {
            String hidden = bf.readLine();
            System.out.println(hidden);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果你想从dir /r命令中获取ADS数据,我认为你只需要执行一个shell并捕获输出:

package net.snortum.play;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ExecPlay {

    public static void main(String[] args) {
        new ExecPlay().start();
    }

    private void start() {
        String fileName = "not found";
        String ads = "not found";
        final String command = "cmd.exe /c dir /r"; // listing of current directory

        final Pattern pattern = Pattern.compile(
                  "\\s*"                 // any amount of whitespace
                + "[0123456789,]+\\s*"   // digits (with possible comma), whitespace
                + "([^:]+):"             // group 1 = file name, then colon
                + "([^:]+):"             // group 2 = ADS, then colon
                + ".+");                 // everything else

        try {
            Process process = Runtime.getRuntime().exec(command);
            process.waitFor();
            try (BufferedReader br = new BufferedReader(
                    new InputStreamReader(process.getInputStream()))) {
                String line;

                while ((line = br.readLine()) != null) {
                    Matcher matcher = pattern.matcher(line);
                    if (matcher.matches()) {
                        fileName = matcher.group(1);
                        ads = matcher.group(2);
                        break;
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(fileName + ", " + ads);

    }
}
Run Code Online (Sandbox Code Playgroud)

现在您可以使用第一个代码清单来打印 ADS 数据。