public List<String> getDirContent( ServerDetailsDTO conDetails, String filePath, String... extentions) throws IOException { FileObject fileObject = null; StandardFileSystemManager manager = null; try { manager = new StandardFileSystemManager(); manager.init(); UserAuthenticator auth = new StaticUserAuthenticator(null, conDetails.getUserName(), conDetails.getPassword()); FileSystemOptions opts = new FileSystemOptions(); DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth); SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no"); String fileUri = buildUri(conDetails, filePath); fileObject = manager.resolveFile(fileUri, opts); if (!fileObject.exists()) { return null; } List<String> fileNames = new ArrayList<String>(); FileObject[] children = fileObject.getChildren(); if (children != null) { for (FileObject child : children) { String fileName = child.getName().getBaseName(); if (extentions.length > 0) { for (String extention : extentions) { if (fileName.endsWith(extention)) { fileNames.add(fileName); } } } else { fileNames.add(fileName); } } } return fileNames; } finally { try { if (fileObject != null) { ((SftpFileSystem) fileObject.getFileSystem()).closeCommunicationLink(); FileSystem fs = null; fs = fileObject.getFileSystem(); manager.closeFileSystem(fs); } } finally { } } }
private void changeCurrentNode() { boolean hasMatchSelectedName = false; FileObject[] children = null; try { children = currentFileObject.getChildren(); } catch (Exception e) { e.printStackTrace(); } if (children == null) return; sortFiles(children); String selectedName = historyManager.getSelectedItem(currentFileObject.getName().getPath()); table.removeAll(); TableItem item; for (int i = 0; i < children.length; i++) { FileName fileName = children[i].getName(); if (fileName.getBaseName().equals(selectedName)) { currentRow = i; hasMatchSelectedName = true; } item = new TableItem(table, SWT.NONE); item.setData("fileObject", children[i]); item.setText(fileName.getBaseName()); try { FileType fileType = children[i].getType(); FileContent fileContent = children[i].getContent(); if (fileType.equals(FileType.FOLDER)) { item.setImage(folderImage); item.setText(1, "--"); item.setText(2, StringUtil.formatDate(fileContent.getLastModifiedTime())); } else { item.setImage(fileImage); item.setText(1, StringUtil.formatSize(fileContent.getSize())); item.setText(2, StringUtil.formatDate(fileContent.getLastModifiedTime())); } } catch (Exception e) { e.printStackTrace(); } } if (!hasMatchSelectedName) currentRow = 0; table.setSelection(currentRow); table.setFocus(); }
/** Lists the children of a folder. */ private void listChildren(final FileObject dir, final boolean recursive, final String prefix) throws FileSystemException { final FileObject[] children = dir.getChildren(); for (int i = 0; i < children.length; i++) { final FileObject child = children[i]; System.out.print(prefix); System.out.print(child.getName().getBaseName()); if (child.getType() == FileType.FOLDER) { System.out.println("/"); if (recursive) { listChildren(child, recursive, prefix + " "); } } else { System.out.println(); } } }
private List<FileObject> getFiles(String localfolder) throws KettleFileException { try { List<FileObject> myFileList = new ArrayList<FileObject>(); // Get all the files in the local directory... FileObject localFiles = KettleVFS.getFileObject(localfolder, this); FileObject[] children = localFiles.getChildren(); if (children != null) { for (int i = 0; i < children.length; i++) { // Get filename of file or directory if (children[i].getType().equals(FileType.FILE)) { myFileList.add(children[i]); } } // end for } return myFileList; } catch (IOException e) { throw new KettleFileException(e); } }
public Object execute(final CommandContext context) throws Exception { assert context != null; IO io = context.getIo(); FileObject file = resolveFile(context, path); io.println("URL: {}", file.getURL()); io.println("Name: {}", file.getName()); io.println("BaseName: {}", file.getName().getBaseName()); io.println("Extension: {}", file.getName().getExtension()); io.println("Path: {}", file.getName().getPath()); io.println("Scheme: {}", file.getName().getScheme()); io.println("URI: {}", file.getName().getURI()); io.println("Root URI: {}", file.getName().getRootURI()); io.println("Parent: {}", file.getName().getParent()); io.println("Type: {}", file.getType()); io.println("Exists: {}", file.exists()); io.println("Readable: {}", file.isReadable()); io.println("Writeable: {}", file.isWriteable()); io.println("Root path: {}", file.getFileSystem().getRoot().getName().getPath()); if (file.exists()) { FileContent content = file.getContent(); FileContentInfo contentInfo = content.getContentInfo(); io.println("Content type: {}", contentInfo.getContentType()); io.println("Content encoding: {}", contentInfo.getContentEncoding()); try { // noinspection unchecked Map<String, Object> attrs = content.getAttributes(); if (attrs != null && !attrs.isEmpty()) { io.println("Attributes:"); for (Map.Entry<String, Object> entry : attrs.entrySet()) { io.println(" {}='{}'", entry.getKey(), entry.getValue()); } } } catch (FileSystemException e) { io.println("File attributes are NOT supported"); } try { Certificate[] certs = content.getCertificates(); if (certs != null && certs.length != 0) { io.println("Certificate:"); for (Certificate cert : certs) { io.println(" {}", cert); } } } catch (FileSystemException e) { io.println("File certificates are NOT supported"); } if (file.getType().equals(FileType.FILE)) { io.println("Size: {} bytes", content.getSize()); } else if (file.getType().hasChildren() && file.isReadable()) { FileObject[] children = file.getChildren(); io.println("Directory with {} files", children.length); for (int iterChildren = 0; iterChildren < children.length; iterChildren++) { io.println("#{}:{}", iterChildren, children[iterChildren].getName()); if (iterChildren > 5) { break; } } } io.println( "Last modified: {}", DateFormat.getInstance().format(new Date(content.getLastModifiedTime()))); } else { io.println("The file does not exist"); } FileObjects.close(file); return Result.SUCCESS; }