How to change icon of a empty folder in JTree? (FTP File) - java

I am working with to show all files and folders of a FTPserver into a JTree. But I got a problem that the empty folders are shown as a file. But how to show them as a folder icon?
Here is my code:
public void buildTree(){
try {
ftpClient.connect("130.229.178.31");
ftpClient.login("admin", "123456");
root = new DefaultMutableTreeNode("Welcome!");
for (int i = 0; i < 1; i++) {
DefaultMutableTreeNode temp = new DefaultMutableTreeNode("FTP-Server");
root.add(temp);
bind(temp,"");
}
} catch (IOException e1) {
e1.printStackTrace();
throw new RuntimeException("Client Error", e1);
}
try {
ftpClient.disconnect();
} catch (IOException e2) {
e2.printStackTrace();
throw new RuntimeException("Error when shutdown", e2);
}
}
// bind nod/subnode to the tree (recursive method)
public void bind(DefaultMutableTreeNode node,String path){
try {
Boolean defaultPath = true;
while (defaultPath)
{
defaultPath = ftpClient.changeToParentDirectory();
}
ftpClient.changeWorkingDirectory(path);
FTPFile[] files = ftpClient.listFiles();
for(int i=0;files!=null && i<files.length;i++){
FTPFile tempFile = files[i];
if(tempFile.isDirectory()){
DefaultMutableTreeNode tempNode = new DefaultMutableTreeNode(tempFile.getName());
node.add(tempNode);
bind(tempNode, path+"/"+tempFile.getName());
}else{
DefaultMutableTreeNode tempNode = new DefaultMutableTreeNode(tempFile.getName());
node.add(tempNode);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The "sad" folder is a empty folder, but shown as a file icon. How to change it?
Thank you very much
PS:
Same of the methods is not working, like
FileSystemView fileSystemView = FileSystemView.getFileSystemView();
setIcont(fileSystemView.getSystemIcon(File file));
Because we are working with FTP files not with files.

You have two choices:
Add a "dummy file" to an empty folder (maybe call it "[empty]") or
Use your own DefaultTreeCellRenderer
I think the second coice is preferable. I also recommend to use your own TreeModel to indicate if the tree node is a file or folder.

Related

Data Extract Tar and 7z

I have a single .tar file with many folders and subfolders in it. Inside these many folders there are .7z files among other files. I'd like to search through these folders/subfolder and locate .7z files, (assign them to an array?) and extract them to their respective location.
I'm using Apache Commons:
1) org.apache.commons.compress.archivers.sevenz
Provides classes for reading and writing archives using the 7z format.
2) org.apache.commons.compress.archivers.tar
Provides stream classes for reading and writing archives using the TAR format.
step I wanna extract the .tar file
step I wanna go through the extracted .tar file folder and its subfolders recursively and locate .7z files.
In the 3. step I wanna feed the array the array of .7z files I found and extract them 1 by 1 to their respective locations.
I'm having problems in the 3. step with array call/assignment :/ Could you please help? Thank you very much :)
/**
* uncompresses .tar file
* #param in
* #param out
* #throws IOException
*/
public static void decompressTar(String in, File out) throws IOException {
try (TarArchiveInputStream tin = new TarArchiveInputStream(new FileInputStream(in))){
TarArchiveEntry entry;
while ((entry = tin.getNextTarEntry()) != null) {
if (entry.isDirectory()) {
continue;
}
File curfile = new File(out, entry.getName());
File parent = curfile.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
IOUtils.copy(tin, new FileOutputStream(curfile));
}
}
}
/**
* uncompresses .7z file
* #param in
* #param destination
* #throws IOException
*/
public static void decompressSevenz(String in, File destination) throws IOException {
//#SuppressWarnings("resource")
SevenZFile sevenZFile = new SevenZFile(new File(in));
SevenZArchiveEntry entry;
while ((entry = sevenZFile.getNextEntry()) != null){
if (entry.isDirectory()){
continue;
}
File curfile = new File(destination, entry.getName());
File parent = curfile.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
FileOutputStream out = new FileOutputStream(curfile);
byte[] content = new byte[(int) entry.getSize()];
sevenZFile.read(content, 0, content.length);
out.write(content);
out.close();
}
sevenZFile.close();
}
public void run()
{
//1) uncompress .tar
try {
JThreadTar.decompressTar(RECURSIVE_DIRECTORY_PATH, new File(RECURSIVE_DIRECTORY));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//2) go through the extracted .tar file directory and look for .7z (recursively?)
File[] files = new File(RECURSIVE_DIRECTORY).listFiles();
for (File file : files) {
if (file.isDirectory()) {
File[] matches = file.listFiles(new FilenameFilter()
{
public boolean accept(File dir, String name)
{
return name.endsWith(".7z");
}
});
for (File element: matches) {
System.out.println(element);
}
}
else {
continue;
}
}
//3) Feed the array above to decompressSevenz method
for (int i = 0; i < matches.length; i++)
{
if (matches[i].isFile())
{
try {
JThreadTar.decompressSevenz(matches[i].toString(), new File(RECURSIVE_DIRECTORY));
}
catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
}
My problem is: I can't refer to []matches in step 3. I'm not using this correctly. I just want to create an array []matches for .7z file matches. Every time a .7z is found, I'd like to add it to this array. and in the 3. step I wanna extract each .7z to its relative location.
I came a bit further:
//1) uncompress .tar
try {
JThreadTar.decompressTar(RECURSIVE_DIRECTORY_PATH, new File(RECURSIVE_DIRECTORY));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//2) go through the extracted .tar file directory and look for .7z (recursively?)
File dir = new File(RECURSIVE_DIRECTORY);
File[] dirFiles = dir.listFiles();
ArrayList<File> matches2 = new ArrayList<File>();
for (File file : dirFiles) {
if (file.isDirectory()) {
File[] matches = dir.listFiles(new FilenameFilter()
{
public boolean accept(File dir, String name)
{
return name.endsWith(".7z");
}
});
matches2.addAll(Arrays.asList(matches));
}
else if (file.isFile()) {
if (file.getName().endsWith(".7z")){
matches2.add(file);
};
}
};
//3) Feed the arraylist above to decompressSevenz method
for (int counter = 0; counter < matches2.size(); counter++) {
if (matches2.get(counter).isFile())
{
try {
JThreadTar.decompressSevenz(matches2.get(counter).toString(), new File(RECURSIVE_DIRECTORY));
}
catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
}
Here is after the final form of step 2 and step 3 from #Joop Eggen
Path topDir = Paths.get(RECURSIVE_DIRECTORY);
try {
Files.walk(topDir)
.filter(path -> path.getFileName().toString().endsWith(".7z"))
.forEach(path -> {
try {
JThreadTar.decompressSevenz(path.toString(), topDir.toFile());
} catch (IOException e2) {
e2.printStackTrace();
}
});
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
step recursively:
Path toptopDir = Paths.get(RECURSIVE_DIRECTORY_PATH);
try {
Files.walk(toptopDir)
.filter(path -> path.getFileName().toString().endsWith(".tar"))
.forEach(path -> {
try {
JThreadTar.decompressTar(RECURSIVE_DIRECTORY_PATH, new File(RECURSIVE_DIRECTORY));
} catch (IOException e2) {
e2.printStackTrace();
}
});
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
I took the opportunity to use the newer Path and Files. Files.listFiles() may return null. And the usage of Arrays.asList and such will cause heavy data.
All that would be simplified to:
Path topDir = Paths.get(RECURSIVE_DIRECTORY);
Files.walk(topDir)
.filter(path -> path.getFileName().toString().endsWith(".7z"))
.forEach(path -> {
try {
JThreadTar.decompressSevenz(path.toString(), topDir.toFile());
} catch (IOException e2) {
e2.printStackTrace();
}
});

Text document is becoming file folder

Here is my class, what I am doing wrong. Why is my text document becoming a file folder. Please explain what is going on and how I can correct it. Thank you
public class InputOutput {
public static void main(String[] args) {
File file = new File("C:/Users/CrypticDev/Desktop/File/Text.txt");
Scanner input = null;
if (file.exists()) {
try {
PrintWriter pw = new PrintWriter(file);
pw.println("Some data that we have stored");
pw.println("Another data that we stored");
pw.close();
} catch(FileNotFoundException e) {
System.out.println("Error " + e.toString());
}
} else {
file.mkdirs();
}
try {
input = new Scanner(file);
while(input.hasNext()) {
System.out.println(input.nextLine());
}
} catch(FileNotFoundException e) {
System.out.println("Error " + e.toString());
} finally {
if (input != null) {
input.close();
}
}
System.out.println(file.exists());
System.out.println(file.length());
System.out.println(file.canRead());
System.out.println(file.canWrite());
System.out.println(file.isFile());
System.out.println(file.isDirectory());
}
}
Thanks. The above is my Java class.
You mistakingly assume Text.txt is not a directory name.
mkdirs() creates a directory (and all directories needed to create it). In your case 'Text.txt'
See here: https://docs.oracle.com/javase/7/docs/api/java/io/File.html#mkdirs().
It is perfectly fine for a directory to have a . in it.
You could use getParentFile() to get the directory you want to create and use mkdirs() on that.
For additional informations. Here is the différence between the two representaions of files and directories:
final File file1 = new File("H:/Test/Text.txt"); // Creates NO File/Directory
file1.mkdirs(); // Creates directory named "Text.txt" and its parent directory "H:/Test" if it doesn't exist (may fail regarding to permissions on folders).
final File file = new File("H:/Test2/Text.txt"); // Creates NO File/Directory
try {
file.createNewFile(); // Creates file named "Text.txt" (if doesn't exist) in the folder "H:/Test2". If parents don't exist, no file is created.
} catch (IOException e) {
e.printStackTrace();
}
Replace your code:
else {
file.mkdirs();
}
with:
else {
if (!file.isFile()&&file.getParentFile().mkdirs()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Only meta/inf file available when exploring jar content

I am trying to read the content of a jar in a folder accessible from the root directory of my project, the jar is correctly found, however, my code only prints the name of the META-INF file, here's what i tried so far:
public static void provideClassList(String jarName) {
List<String> classNames = new ArrayList<String>();
ZipInputStream zip;
try {
zip = new ZipInputStream(new FileInputStream(StaticValues.JARS_PATH.concat(jarName)));
for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) {
System.out.println(entry);//PRINTS META-INF/
if (!entry.isDirectory() && entry.getName().endsWith(".class")) {
String className = entry.getName().replace('/', '.');
classNames.add(className.substring(0, className.length() - ".class".length()));
}
zip.close();
}
// explore content (THIS IS ACTUALLY EMPTY)
for (String className : classNames) {
try {
Class<?> clazz = Class.forName(className);
System.out.println(clazz.getCanonicalName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException ex) {
}
}
I cant see any permissioning issue, also, i have opened the jar file manually from console and the content that i expect to find is all there.
Those are the properties i see from eclipse:
You are calling zip.close(); inside of the for loop, that is propably the reason you only get the first entry in the jar. Move it outside of the for loop, or even better, use a try-with-resources Statement.
try (FileInputStream fis = new FileInputStream(StaticValues.JARS_PATH.concat(jarName);
ZipInputStream zip = new ZipInputStream(fis)) {
// code for iterating goes here
}

save a created propertyfile in specific folder

How can I save a prop file in a specific folder for example,
now it is saved in the root I guess, but it needs to be in the same folder as the class where it is created.
I also want to know how to load it. If it possible to load a properties file easily from the root then it is okay as well to save it in the root.
code creating the file, first 2 lines with // ( = make code work now without using prop file), class name = Providers
public static DataAccessProvider createProvider (URL url) {
//MovieDAOOnline mdaoOn = new MovieDAOOnline();
//mdaoOn.setUrl(url);
Properties prop = new Properties();
OutputStream output = null;
try {
output = new FileOutputStream("config.properties");
// set the properties value
prop.setProperty("uri", url.toString());
prop.store(output, null);
} catch (IOException io) {
io.printStackTrace();
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return new OnlineProvider();
}
code for getting the file, first line in comment needs to be changed to get uri from propertie:
public Movie getMovie(int id) throws DataAccessException{
//StringBuilder builder = new StringBuilder(url.toString());
builder.append("movies.xml");
MovieConfigRead mcr = new MovieConfigRead();
List<Movie> film = null;
try {
film = mcr.geefMovies(builder.toString());
} catch (JAXBException e) {
throw new DataAccessException();
} catch (MalformedURLException e) {
throw new DataAccessException();
}
for (Movie movie : film) {
if (movie.getId() == id) {
return movie;
}
}
return null;
}

Unnecessary file structure is added while "zipping" a directory using java?

well my question is really simple, is about an unexpected behavior (or at least is unexpected to me) while I try to zip a directory, I have the following methods that I've created on my own (I'm quite aware that I'm not handling exceptions and all that stuff, It is because (by now) I'm just doing this to learn how to do it so stability "is not really important"), here is the code:
public static void zipDirectory(File srcDirectory, File zipFile) throws IllegalArgumentException {
if (!srcDirectory.isDirectory()) {
throw new IllegalArgumentException("The first parameter (srcDirectory) MUST be a directory.");
}
int bytesRead;
byte[] dataRead = new byte[1000];
BufferedInputStream in = null;
ZipOutputStream zOut;
try {
zOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
for (File f : srcDirectory.listFiles()) {
if (f.isDirectory()) {
FileUtilities.zipInnerDirectory(f,zOut);
}else {
in = new BufferedInputStream(new FileInputStream(f.getAbsolutePath()), 1000);
zOut.putNextEntry(new ZipEntry(f.getPath()));
while((bytesRead = in.read(dataRead,0,1000)) != -1) {
zOut.write(dataRead, 0, bytesRead);
}
zOut.closeEntry();
}
}
zOut.flush();
zOut.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void zipInnerDirectory(File dir, ZipOutputStream zOut) throws IllegalArgumentException {
if (!dir.isDirectory()) {
throw new IllegalArgumentException("The first parameter (srcDirectory) MUST be a directory.");
}
BufferedInputStream in = null;
int bytesRead;
byte[] dataRead = new byte[1000];
try {
for (File f : dir.listFiles()) {
if (f.isDirectory()) {
FileUtilities.zipInnerDirectory(f,zOut);
}else {
in = new BufferedInputStream(new FileInputStream(f.getAbsolutePath()), 1000);
zOut.putNextEntry(new ZipEntry(f.getPath()));
while((bytesRead = in.read(dataRead,0,1000)) != -1) {
zOut.write(dataRead, 0, bytesRead);
}
zOut.closeEntry();
}
}
zOut.flush();
zOut.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
As I said is not my best coding so please don't judge the code (or at least don't be too strict ;) ), I know it can be so much better; ok the "unexpected behavior" is this, let's say that I have the following directory:
H:\MyDir1\MyDir2\MyDirToZip
when i send as a parameter a file created with that path (new File("H:\\MyDir1\\MyDir2\\MyDirToZip")) everything's work pretty fine the zip is created successfully, the thing is that when I open (unzip) the files inside the zip they have the next structure:
H:\MyDir1\MyDir2\MyDirToZip
when I was expecting to find inside just:
\MyDirToZip
without H: \MyDir1 \MyDir2 which are "unnecessary" (BTW they just contain one to each other in the appropriate order, i mean, the other files that are in them are not compressed, that is why I say they are unnecessary) so the question is, what I'm I doing wrong? how can I specify that I just want to zip the structure down the srcDirectory?
zOut.putNextEntry(new ZipEntry(f.getPath()));
This should be the problem. f.getPath() will return a path that's relative to some root directory (probably your current working dir), but not relative to the directory you are zipping. You need to figure out a way to get the relative path from the zip directory, possibly this will do:
new ZipEntry(f.getAbsolutePath().substring(zipDir.getAbsolutePath().length()))
or, if you want the root directory added:
new ZipEntry(zipDir.getName() + "/"
+ f.getAbsolutePath().substring(zipDir.getAbsolutePath().length()))

Categories