{"id":15752647,"url":"https://github.com/mhewedy/sftp-utils","last_synced_at":"2025-04-30T13:21:28.654Z","repository":{"id":57721445,"uuid":"109397693","full_name":"mhewedy/sftp-utils","owner":"mhewedy","description":"Utilities for access sftp via  jsch java library","archived":false,"fork":false,"pushed_at":"2018-05-11T20:31:07.000Z","size":29,"stargazers_count":4,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-26T13:08:14.926Z","etag":null,"topics":["jsch","sftp"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mhewedy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-11-03T13:18:04.000Z","updated_at":"2024-07-07T22:41:28.000Z","dependencies_parsed_at":"2022-09-26T21:41:32.568Z","dependency_job_id":null,"html_url":"https://github.com/mhewedy/sftp-utils","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mhewedy%2Fsftp-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mhewedy%2Fsftp-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mhewedy%2Fsftp-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mhewedy%2Fsftp-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mhewedy","download_url":"https://codeload.github.com/mhewedy/sftp-utils/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229506364,"owners_count":18083722,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["jsch","sftp"],"created_at":"2024-10-04T07:03:41.019Z","updated_at":"2024-12-13T07:22:21.013Z","avatar_url":"https://github.com/mhewedy.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# sftp-utils\n\n ### execute   \n Execute sftp commands giving a managed ChannelSftp\n \n Complete example:\n ```java\n \n // Create SessionFactory instance using supplied SimpleSessionFactory class\n \nProperties properties = new Properties();\nproperties.setProperty(\"username\", \"mhewedy\");\nproperties.setProperty(\"host\", \"192.168.1.10\");\nproperties.setProperty(\"port\", \"22\");\nproperties.setProperty(\"password\", \"system\");\n\n//Thread safe class, better to cache instance of it. (e.g. as a Spring Bean)\nSessionFactory sessionFactory = new SessionFactory.SimpleSessionFactory(properties);\n\nString fileDir = \"test/files/goes/here\";\nbyte[] bytes = \"Test me\\n\".getBytes(\"utf8\");\n\n// create dir recursive:\n\nSftpUtils.execute(sessionFactory, channel -\u003e {\n    SftpUtils.mkdirp(channel, fileDir);\n});\n\n// write a file:\n\nString filePath = SftpUtils.execute(sessionFactory, channel -\u003e {\n    String path = String.format(\"%s/%s.%s\", fileDir, System.currentTimeMillis(), \"txt\");\n    SftpUtils.mkdirp(channel, fileDir);\n    channel.put(new ByteArrayInputStream(bytes), path);\n    return path;\n});\n\nSystem.out.println(\"file written at: \" + filePath);\n\n// download the file:\n\nString fileContents = SftpUtils.execute(sessionFactory, channel -\u003e {\n    ByteArrayOutputStream baos = new ByteArrayOutputStream();\n    channel.get(filePath, baos);\n    return new String(baos.toByteArray());\n});\n\nSystem.out.println(\"file contents from the server: \" + fileContents);\n\n// delete the file:\n\nSftpUtils.execute(sessionFactory, channel -\u003e {\n    channel.rm(filePath);\n});\n\n// delete the empty directory:\n\nSftpUtils.execute(sessionFactory, channel -\u003e {\n    SftpUtils.rmr(channel, fileDir.split(\"/\")[0]);\n});\n\n ```\n\nCheck file is found:\n```java\nSftpUtils.execute(sessionFactory, channel -\u003e {\n    SftpUtils.mkdirp(channel, \"path/to/new/file\");\n});\n\nboolean findFound = SftpUtils.execute(sessionFactory, channel -\u003e {\n    try{\n        channel.lstat(\"non_found_find\");\n    }catch (SftpException ex){  // only handel exception when needed\n        if (ex.id == ChannelSftp.SSH_FX_NO_SUCH_FILE){\n            return false;\n        }\n    }\n    return true;\n});\nSystem.out.println(\"find found: \" + findFound);\n```\n\nCopy File and print its content from SFTP after copy:\n```java\nInputStream transfer = Files.newInputStream(Paths.get(\"/Users/mhewedy/Work/Code/sftp-utils/README.md\"));\nString ftpPath = \"path/to/new/file/\";\nString ftpFileName = \"README.md2\";\n\nSftpUtils.execute(sessionFactory, channel -\u003e {\n    SftpUtils.mkdirp(channel, ftpPath);\n    channel.put(transfer, ftpPath + ftpFileName);\n});\n\nString readmeFile = SftpUtils.execute(sessionFactory, channel -\u003e {\n    ByteArrayOutputStream baos = new ByteArrayOutputStream();\n    channel.get(ftpPath + ftpFileName, baos);\n    byte[] bytes = baos.toByteArray();\n    return new String(bytes);\n});\n```\n\n ### mkdirp  \nCreate directory structure recursively (from mkdir -p command)   \n\n```java\nSftpUtils.mkdirp(sftpChannel, \"/path/to/dir\"); //absolute\nSftpUtils.mkdirp(sftpChannel, \"another/path/to/dir\"); // relative\n```\n### rmr    \nRemoves dirs and files recursivly, starting from a dir name (same as rm -r command)\n\n```java\nSftpUtils.rmr(sftpChannel, \"/existence/path/to/remove\");  // removes all files and dirs inside the \"remove\" directory, including \"remove\" directory it self.\nSftpUtils.rmr(sftpChannel, \"/existence/path/\"); // removes all files and dirs insdie the \"path\" directory, including \"path\" directory it self.\n```\n\n### Usage:\n```xml\n  \u003cdependency\u003e\n    \u003cgroupId\u003ecom.github.mhewedy\u003c/groupId\u003e\n    \u003cartifactId\u003esftp-utils\u003c/artifactId\u003e\n    \u003cversion\u003e2.0.0\u003c/version\u003e\n  \u003c/dependency\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmhewedy%2Fsftp-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmhewedy%2Fsftp-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmhewedy%2Fsftp-utils/lists"}