it.sauronsoftware.ftp4j.FTPFile Java Examples

The following examples show how to use it.sauronsoftware.ftp4j.FTPFile. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FtpRepository.java    From SimpleFTP with MIT License 6 votes vote down vote up
/**
 * Lists the files present at the specified path.
 * @param path The specified path.
 * @return The list of files.
 */
public List<FileEntity> listFiles(FtpServer server, String path) throws FTPException, IOException, FTPIllegalReplyException, FTPAbortedException, FTPDataTransferException, FTPListParseException {
    FTPClient client = this.getConnection(server);

    if(path != null && !path.equals("/")) {
        client.changeDirectory(path);
    }

    FTPFile[] list = client.list();

    List<FileEntity> results = new ArrayList<FileEntity>();

    if(list != null) {
        for (FTPFile file : list) {
            if(!file.getName().equals("..")) {
                results.add(this.ftpFileToEntity(file, path));
            }
        }
    }

    Collections.sort(results);

    return results;
}
 
Example #2
Source File: MLSDListParser.java    From ftp4j with GNU Lesser General Public License v2.1 5 votes vote down vote up
public FTPFile[] parse(String[] lines) throws FTPListParseException {
	ArrayList list = new ArrayList();
	for (int i = 0; i < lines.length; i++) {
		FTPFile file = parseLine(lines[i]);
		if (file != null) {
			list.add(file);
		}
	}
	int size = list.size();
	FTPFile[] ret = new FTPFile[size];
	for (int i = 0; i < size; i++) {
		ret[i] = (FTPFile) list.get(i);
	}
	return ret;
}
 
Example #3
Source File: FtpRepository.java    From SimpleFTP with MIT License 4 votes vote down vote up
/**
 * Convert a FTP file to a file entity.
 * @param file The FTP file to convert.
 * @param currentPath The path where the file has been found.
 * @return The converted file entity.
 */
private FileEntity ftpFileToEntity(FTPFile file, String currentPath)
{
    FileEntity entity = new FileEntity();
    entity.setPath(currentPath.endsWith("/")
            ? currentPath + file.getName()
            : currentPath + "/" + file.getName());

    entity.setName(file.getName());
    entity.setSize(file.getSize());
    entity.setIsDirectory(file.getType() == FTPFile.TYPE_DIRECTORY);
    return entity;
}
 
Example #4
Source File: NetWareListParser.java    From ftp4j with GNU Lesser General Public License v2.1 4 votes vote down vote up
public FTPFile[] parse(String[] lines) throws FTPListParseException {
	int size = lines.length;
	// What's the date today?
	Calendar now = Calendar.getInstance();
	// Ok, starts parsing.
	int currentYear = now.get(Calendar.YEAR);
	FTPFile[] ret = new FTPFile[size];
	for (int i = 0; i < size; i++) {
		Matcher m = PATTERN.matcher(lines[i]);
		if (m.matches()) {
			String typeString = m.group(1);
			String sizeString = m.group(2);
			String monthString = m.group(3);
			String dayString = m.group(4);
			String yearString = m.group(5);
			String hourString = m.group(6);
			String minuteString = m.group(7);
			String nameString = m.group(8);
			// Parse the data.
			ret[i] = new FTPFile();
			if (typeString.equals("-")) {
				ret[i].setType(FTPFile.TYPE_FILE);
			} else if (typeString.equals("d")) {
				ret[i].setType(FTPFile.TYPE_DIRECTORY);
			} else {
				throw new FTPListParseException();
			}
			long fileSize;
			try {
				fileSize = Long.parseLong(sizeString);
			} catch (Throwable t) {
				throw new FTPListParseException();
			}
			ret[i].setSize(fileSize);
			if (dayString.length() == 1) {
				dayString = "0" + dayString;
			}
			StringBuffer mdString = new StringBuffer();
			mdString.append(monthString);
			mdString.append(' ');
			mdString.append(dayString);
			mdString.append(' ');
			boolean checkYear = false;
			if (yearString == null) {
				mdString.append(currentYear);
				checkYear = true;
			} else {
				mdString.append(yearString);
				checkYear = false;
			}
			mdString.append(' ');
			if (hourString != null && minuteString != null) {
				if (hourString.length() == 1) {
					hourString = "0" + hourString;
				}
				if (minuteString.length() == 1) {
					minuteString = "0" + minuteString;
				}
				mdString.append(hourString);
				mdString.append(':');
				mdString.append(minuteString);
			} else {
				mdString.append("00:00");
			}
			Date md;
			try {
				synchronized (DATE_FORMAT) {
					md = DATE_FORMAT.parse(mdString.toString());
				}
			} catch (ParseException e) {
				throw new FTPListParseException();
			}
			if (checkYear) {
				Calendar mc = Calendar.getInstance();
				mc.setTime(md);
				if (mc.after(now) && mc.getTimeInMillis() - now.getTimeInMillis() > 24L * 60L * 60L * 1000L) {
					mc.set(Calendar.YEAR, currentYear - 1);
					md = mc.getTime();
				}
			}
			ret[i].setModifiedDate(md);
			ret[i].setName(nameString);
		} else {
			throw new FTPListParseException();
		}
	}
	return ret;
}
 
Example #5
Source File: DOSListParser.java    From ftp4j with GNU Lesser General Public License v2.1 4 votes vote down vote up
public FTPFile[] parse(String[] lines) throws FTPListParseException {
	int size = lines.length;
	FTPFile[] ret = new FTPFile[size];
	for (int i = 0; i < size; i++) {
		Matcher m = PATTERN.matcher(lines[i]);
		if (m.matches()) {
			String month = m.group(1);
			String day = m.group(2);
			String year = m.group(3);
			String hour = m.group(4);
			String minute = m.group(5);
			String ampm = m.group(6);
			String dirOrSize = m.group(7);
			String name = m.group(8);
			ret[i] = new FTPFile();
			ret[i].setName(name);
			if (dirOrSize.equalsIgnoreCase("<DIR>")) {
				ret[i].setType(FTPFile.TYPE_DIRECTORY);
				ret[i].setSize(0);
			} else {
				long fileSize;
				try {
					fileSize = Long.parseLong(dirOrSize);
				} catch (Throwable t) {
					throw new FTPListParseException();
				}
				ret[i].setType(FTPFile.TYPE_FILE);
				ret[i].setSize(fileSize);
			}
			String mdString = month + "/" + day + "/" + year + " " + hour
					+ ":" + minute + " " + ampm;
			Date md;
			try {
				synchronized (DATE_FORMAT) {
					md = DATE_FORMAT.parse(mdString);
				}
			} catch (ParseException e) {
				throw new FTPListParseException();
			}
			ret[i].setModifiedDate(md);
		} else {
			throw new FTPListParseException();
		}
	}
	return ret;
}
 
Example #6
Source File: EPLFListParserTest.java    From ftp4j with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Test
public void nameIsParsed() throws Exception {
	FTPFile[] ftpFiles = parser.parse(new String[]{line1});
	assertEquals("dev", ftpFiles[0].getName());
}
 
Example #7
Source File: EPLFListParserTest.java    From ftp4j with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Test
public void typeDirectoryIsParsed() throws Exception {
	FTPFile[] ftpFiles = parser.parse(new String[]{line1});
	assertEquals(TYPE_DIRECTORY, ftpFiles[0].getType());
}
 
Example #8
Source File: EPLFListParserTest.java    From ftp4j with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Test
public void typeFileIsParsed() throws Exception {
	FTPFile[] ftpFiles = parser.parse(new String[]{line2});
	assertEquals(TYPE_FILE, ftpFiles[0].getType());
}
 
Example #9
Source File: EPLFListParserTest.java    From ftp4j with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Test
public void sizeIsParsed() throws Exception {
	FTPFile[] ftpFiles = parser.parse(new String[]{line2});
	assertEquals(10376, ftpFiles[0].getSize());
}
 
Example #10
Source File: EPLFListParserTest.java    From ftp4j with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Test
public void multipleLinesAreParsed() throws Exception {
	FTPFile[] ftpFiles = parser.parse(lines);
	assertEquals(2, ftpFiles.length);
}
 
Example #11
Source File: MLSDListParserTest.java    From ftp4j with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Test
public void typeFileIsParsed() throws Exception {
	FTPFile[] ftpFiles = parser.parse(new String[]{line1});
	assertEquals(TYPE_FILE, ftpFiles[0].getType());
}
 
Example #12
Source File: MLSDListParserTest.java    From ftp4j with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Test
public void sizeIsParsed() throws Exception {
	FTPFile[] ftpFiles = parser.parse(new String[]{line1});
	assertEquals(4161, ftpFiles[0].getSize());
}
 
Example #13
Source File: MLSDListParserTest.java    From ftp4j with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Test
public void filenameWithColonIsParsed() throws Exception {
	FTPFile[] ftpFiles = parser.parse(new String[]{line1});
	assertEquals("cap;mux.tar.gz", ftpFiles[0].getName());
}
 
Example #14
Source File: MLSDListParserTest.java    From ftp4j with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Test
public void filenameIsParsed() throws Exception {
	FTPFile[] ftpFiles = parser.parse(new String[]{line2});
	assertEquals("mux.tar.gz", ftpFiles[0].getName());
}