0

We have one SFTP server where we placed one file present in ./test/directory1/test.csv.
We added one custom attribute to this file before placing it to this directory , attribute name "user.customchecksum", below is code which we used to add attribute

Path file = Paths.get("C:/Files/test.csv");
            UserDefinedFileAttributeView view = Files.getFileAttributeView(file,
                    UserDefinedFileAttributeView.class);
            view.write("user.customchecksum", Charset.defaultCharset().encode("XDRSECTVVTEST"));

Now before downloading this file we want to check that this attribute value and after that only we download this file.

Note::
For connection we are using JSCH and able to get File Inputstream without downloading the file.
Below method we are using to read file attribute value after file is downloaded.
But now we want to read this attribute before downloading file.

    private void getCustomAttributes(String filePath) {
    try {
        Path file = Paths.get(filePath);
        UserDefinedFileAttributeView view = Files.getFileAttributeView(file, UserDefinedFileAttributeView.class);
        String name = "user.customchecksum";
        ByteBuffer buf = ByteBuffer.allocate(view.size(name));

        view.read(name, buf);
        buf.flip();
        String value = Charset.defaultCharset().decode(buf).toString();

    } catch (IOException e) {
        e.printStackTrace();
    }

}

We are able to read file InputStream before downloading file, can this be used to read added attributes? if yes what is the way to do this?

  • What "custom attribute"? Do you mean something like this https://en.wikipedia.org/wiki/Extended_file_attributes ? – Martin Prikryl Feb 23 '23 at 09:22
  • Yes, we added one attribute by using this code: Path file = Paths.get(filePath); UserDefinedFileAttributeView view = Files.getFileAttributeView(file, UserDefinedFileAttributeView.class); view.write("user.customchecksum", Charset.defaultCharset().encode("XDRSECTVVTEST")); – user19996345 Feb 23 '23 at 09:26
  • I updated my question as well the way we are adding attributes to a file. – user19996345 Feb 23 '23 at 09:30
  • If I understand correctly you use something like [`ChannelSftp.get()`](https://javadoc.io/static/com.jcraft/jsch/0.1.55/com/jcraft/jsch/ChannelSftp.html#get(java.lang.String)) to get the file content? It seems to me that simply using [the `lstat` method](https://javadoc.io/static/com.jcraft/jsch/0.1.55/com/jcraft/jsch/ChannelSftp.html#lstat(java.lang.String)) of that same class should give you access to what you need. – Joachim Sauer Feb 23 '23 at 09:46
  • 1
    See [Read file extended attributes of files on SFTP server with JSch](https://stackoverflow.com/q/58301307/850848). – Martin Prikryl Feb 23 '23 at 09:51
  • @MartinPrikryl so is this not possible ? – user19996345 Feb 23 '23 at 09:59
  • I believe my answer to the linked question answers this comprehensively. TL;DR: In most cases, it's imo not possible using SFTP. It still might be possible "using JSch", if you resort to using shell commands (and know what shell commands to use, but that's not a programing question anymore, imo). – Martin Prikryl Feb 23 '23 at 11:23

0 Answers0