Java Code Examples for jcifs.smb.SmbFile#listFiles()

The following examples show how to use jcifs.smb.SmbFile#listFiles() . 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: Futils.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
public static long folderSize(SmbFile directory) {
    long length = 0;
    try {
        for (SmbFile file:directory.listFiles()) {

            if (file.isFile())
                length += file.length();
            else
                length += folderSize(file);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return length;
}
 
Example 2
Source File: SambaSenderOld.java    From iaf with Apache License 2.0 5 votes vote down vote up
private String listFilesInDirectory(SmbFile directory) throws IOException {
	SmbFile[] files = directory.listFiles();

	int count = (files == null ? 0 : files.length);
	XmlBuilder dirXml = new XmlBuilder("directory");
	dirXml.addAttribute("name", directory.getCanonicalPath());
	dirXml.addAttribute("count", count);
	for (int i = 0; i < count; i++) {
		SmbFile file = files[i];
		dirXml.addSubElement(getFileAsXmlBuilder(file));
	}
	return dirXml.toXML();
}
 
Example 3
Source File: SMBOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 4 votes vote down vote up
private void copyInternal(SmbFile source, SmbFile target, CopyParameters params) throws IOException {
	if (Thread.currentThread().isInterrupted()) {
		throw new IOException(FileOperationMessages.getString("IOperationHandler.interrupted")); //$NON-NLS-1$
	}
	source = toCannonicalFile(source);
	target = toCannonicalFile(target);
	if (source.equals(target)) {
		throw new SameFileException(source, target);
	}
	boolean makeParents = Boolean.TRUE.equals(params.isMakeParents());
	if (source.isDirectory()) {
		if (!params.isRecursive()) {
			throw new IOException(MessageFormat.format(FileOperationMessages.getString("IOperationHandler.cannot_copy_directory"), source)); //$NON-NLS-1$
		}
		if (target.exists() && !target.isDirectory()) {
			throw new IOException(MessageFormat.format(FileOperationMessages.getString("IOperationHandler.cannot_overwrite_not_a_directory"), source, target)); //$NON-NLS-1$
		}
		source = ensureTrailingSlash(source);
		target = ensureTrailingSlash(target);
		if (!target.exists()) {
			try {
				if (makeParents) {
					target.mkdirs();
				} else {
					target.mkdir();
				}
			} catch (SmbException e) {
				throw new IOException(MessageFormat.format(FileOperationMessages.getString("IOperationHandler.create_failed"), target), e); //$NON-NLS-1$
			}
			source.copyTo(target);
		} else {
			for (SmbFile child: source.listFiles()) {
				copyInternal(child, new SmbFile(target, child.getName()), params);
			}
		}
	} else {
		if (target.exists()) {
			if (params.isNoOverwrite()) {
				return;
			}
			if (params.isUpdate() && (source.lastModified() <= target.lastModified())) {
				return;
			}
		} else if (makeParents) {
			mkParentDirs(target);
		}

		source.copyTo(target);
	}
}
 
Example 4
Source File: SMBOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 4 votes vote down vote up
private static List<SmbFile> expand(SmbFile base, String part, boolean directory) throws IOException {
	if (base == null) {
		throw new NullPointerException("base"); //$NON-NLS-1$
	}
	if (!base.isDirectory()) {
		throw new IllegalArgumentException(MessageFormat.format(FileOperationMessages.getString("IOperationHandler.not_a_directory"), base)); //$NON-NLS-1$
	}
	
	base = ensureTrailingSlash(base);
	part = URIUtils.urlDecode(part);
	if (hasWildcards(part)) {
		SmbFile[] children;
		try {
			// Let's use cool SmbFile.listFiles(String) method which resolves exactly our (DOS) wildcards...
			children = base.listFiles(part);
		} catch (SmbException e) {
			// ... but it uses not so cool way of letting know that there are no files matching the pattern 
			if (e.getNtStatus() == NtStatus.NT_STATUS_NO_SUCH_FILE) {
				return Collections.emptyList(); // maybe this could be done on any SmbException
			} else {
				throw e;
			}
		}
		if (!directory) {
			return Arrays.asList(children);
		} else {
			List<SmbFile> dirsOnly = new ArrayList<SmbFile>(children.length);
			for (SmbFile child : children) {
				if (child.isDirectory()) {
					dirsOnly.add(child);
				}
			}
			return dirsOnly;
		}
	} else {
		SmbFile file = new SmbFile(base, part);
		if (file.exists()) {
			return Arrays.asList(file);
		} else {
			return new ArrayList<SmbFile>(0);
		}
	}
}
 
Example 5
Source File: BrowserSmb.java    From Mizuu with Apache License 2.0 4 votes vote down vote up
@Override
public boolean browse(SmbFile folder) {
	if (folder.getName().equals("smb://"))
		return false;

	List<BrowserFileObject> list = new ArrayList<BrowserFileObject>();
	List<SmbFile> orderedArray = new ArrayList<SmbFile>();
	List<SmbFile> temp = new ArrayList<SmbFile>();

	try {
		SmbFile[] listFiles = folder.listFiles();
		if (listFiles == null)
			return false;

           Collections.addAll(orderedArray, listFiles);

		Collections.sort(orderedArray, new Comparator<SmbFile>() {
			@Override
			public int compare(SmbFile f1, SmbFile f2) {
				try {
					if (f1.isDirectory() && !f2.isDirectory()) {  
						// Directory before non-directory  
						return -1;  
					} else if (!f1.isDirectory() && f2.isDirectory()) {  
						// Non-directory after directory  
						return 1;  
					}
				} catch (Exception ignored) {}

				return f1.getName().toLowerCase(Locale.getDefault()).compareTo(f2.getName().toLowerCase(Locale.getDefault()));  
			}
		});

		for (SmbFile f : orderedArray)
			if (MizLib.isValidFilename(f.getName())) {
				list.add(new BrowserFileObject(f.getName(), f.isDirectory(), f.isDirectory() ? 0 : f.length()));
				temp.add(f);
			}

		listFiles = temp.toArray(new SmbFile[temp.size()]);
		temp.clear();
		orderedArray.clear();

		setParentFolder(new SmbFile(folder.getParent()));
		setCurrentFolder(folder);
		setCurrentFiles(listFiles);

		setBrowserFiles(list);
	} catch (Exception e) {
		Log.d("Mizuu", e.toString());
		return false;
	}

	return browseParent();
}