/** * * * <pre> * <b>Description:</b> * Function remove folder from ftp * <b>Creation date: </b>02.05.08 * <b>Modification date: </b>02.05.08 * </pre> * * @param String sDirPath � removed directory * @author Vitalii Fedorets * @return boolean � true if success */ public boolean removeFolder(String sDirPath) throws Exception { try { File oDir = new File(sDirPath); FTPClient oFtp = this.initFtpClientLoginChangeWorkingDirectory(sDirPath); oFtp.changeWorkingDirectory(oDir.getParent().replace("\\", "/")); int iReply = oFtp.getReplyCode(); if (!FTPReply.isPositiveCompletion(iReply)) { log.setError("Not change work directory to `" + oDir.getParent().replace("\\", "/") + "`"); oFtp.disconnect(); return false; } if (!oFtp.removeDirectory(sDirPath)) { log.setError("Could not remove directory `" + sDirPath + "`"); oFtp.disconnect(); return false; } return true; } catch (Exception oEx) { throw oEx; } }
/** * Convenience method, so that we don't open a new connection when using this method from within * another method. Otherwise every API invocation incurs the overhead of opening/closing a TCP * connection. */ private boolean delete(FTPClient client, Path file, boolean recursive) throws IOException { Path workDir = new Path(client.printWorkingDirectory()); Path absolute = makeAbsolute(workDir, file); String pathName = absolute.toUri().getPath(); FileStatus fileStat = getFileStatus(client, absolute); if (fileStat.isFile()) { return client.deleteFile(pathName); } FileStatus[] dirEntries = listStatus(client, absolute); if (dirEntries != null && dirEntries.length > 0 && !(recursive)) { throw new IOException("Directory: " + file + " is not empty."); } if (dirEntries != null) { for (int i = 0; i < dirEntries.length; i++) { delete(client, new Path(absolute, dirEntries[i].getPath()), recursive); } } return client.removeDirectory(pathName); }