/** * Copies this file to another file. This object is the source file. The destination file is given * as the argument. If the destination file, does not exist a new one will be created. Otherwise * the source file will be appended to the destination file. Directories will be copied * recursively. * * @param file The file to receive the data. * @throws NullPointerException If file is null. * @throws IOException If an IOException occurs. */ public void copyTo(GeneralFile file, boolean forceOverwrite) throws IOException { if (file == null) { throw new NullPointerException(); } if (isDirectory()) { // recursive copy GeneralFile[] fileList = listFiles(); file.mkdir(); if (fileList != null) { for (int i = 0; i < fileList.length; i++) { fileList[i].copyTo( FileFactory.newFile( file.getFileSystem(), file.getAbsolutePath(), fileList[i].getName()), forceOverwrite); } } } else { if (file.isDirectory()) { // change the destination from a directory to a file file = FileFactory.newFile(file, getName()); } try { if (file instanceof LocalFile) { ftpClient.get(getPath(), ((LocalFile) file).getFile()); } else if (file instanceof FTPFile) { ftpClient.transfer( getPath(), ((FTPFile) file).getFTPClient(), file.getPath(), !forceOverwrite, null); } else { super.copyTo(file); } } catch (FTPException e) { IOException io = new IOException(); io.initCause(e); throw io; } } }
/** * Copies this file to another file. This object is the source file. The destination file is given * as the argument. If the destination file, does not exist a new one will be created. Otherwise * the source file will be appended to the destination file. Directories will be copied * recursively. * * @param file The file to receive the data. * @throws NullPointerException If file is null. * @throws IOException If an IOException occurs. */ public void copyFrom(GeneralFile file, boolean forceOverwrite) throws IOException { if (file == null) { throw new NullPointerException(); } if (file.isDirectory()) { // recursive copy GeneralFile[] fileList = file.listFiles(); mkdir(); if (fileList != null) { for (int i = 0; i < fileList.length; i++) { FileFactory.newFile(this, fileList[i].getName()).copyFrom(fileList[i], forceOverwrite); } } } else { if (isDirectory()) { // change the destination from a directory to a file GeneralFile subFile = FileFactory.newFile(this, file.getName()); subFile.copyFrom(file); return; } try { if (file instanceof LocalFile) { ftpClient.put(((LocalFile) file).getFile(), getPath(), !forceOverwrite); } else if (file instanceof FTPFile) { ftpClient.transfer(file.getPath(), ftpClient, getPath(), !forceOverwrite, null); } else { super.copyTo(file); } } catch (FTPException e) { IOException io = new IOException(); io.initCause(e); throw io; } } }
private RemoteFile<FTPFile> asRemoteFile(String absolutePath, FTPFile file, String charset) { RemoteFile<FTPFile> answer = new RemoteFile<FTPFile>(); answer.setCharset(charset); answer.setEndpointPath(endpointPath); answer.setFile(file); answer.setFileNameOnly(file.getName()); answer.setFileLength(file.getSize()); answer.setDirectory(file.isDirectory()); if (file.getTimestamp() != null) { answer.setLastModified(file.getTimestamp().getTimeInMillis()); } answer.setHostname(((RemoteFileConfiguration) endpoint.getConfiguration()).getHost()); // absolute or relative path boolean absolute = FileUtil.hasLeadingSeparator(absolutePath); answer.setAbsolute(absolute); // create a pseudo absolute name String dir = FileUtil.stripTrailingSeparator(absolutePath); String absoluteFileName = FileUtil.stripLeadingSeparator(dir + "/" + file.getName()); // if absolute start with a leading separator otherwise let it be relative if (absolute) { absoluteFileName = "/" + absoluteFileName; } answer.setAbsoluteFilePath(absoluteFileName); // the relative filename, skip the leading endpoint configured path String relativePath = ObjectHelper.after(absoluteFileName, endpointPath); // skip leading / relativePath = FileUtil.stripLeadingSeparator(relativePath); answer.setRelativeFilePath(relativePath); // the file name should be the relative path answer.setFileName(answer.getRelativeFilePath()); return answer; }