How to read a directory with using InputStream in eclipse plugin developement - eclipse

I'm developing an eclipse plug-in and I need to traverse a directory and whole content of the directory. I found the method which reads a file in plug-in (bundleresource) as InputStream.
InputStream stream = Activator.class.getResourceAsStream("/dir1/dir2/file.ext");
this method works for files only. I need a way to read directories, list subdirectories and files like File.io.
Thanks.

Do you want to read a resource directory of your plugin? Otherwise you have to traverse a directory and open one stream per file:
String path = "c:\\temp\\";
File directory = new File(path);
if (directory.isDirectory()) {
String[] list = directory.list();
for (String entry : list) {
String absolutePath = path + entry;
System.out.println("processing " + absolutePath);
File file = new File(absolutePath);
if (file.isFile()) {
FileInputStream stream = new FileInputStream(file);
// use stream
stream.close();
}
}
}
If you want to traverse subdirectories as well you should wrap this into a recursive method, check if file is a directory and call the recursive method in this case.

Related

How to list all local mp3 files on flutter2(null safety)?

The code should be working on flutter2, with android and ios(if possible)
Searching for files
Do you want to simplify your life? Use this package:
import 'package:glob/glob.dart';
Stream<File> search(Directory dir) {
return Glob("**.mp3")
.list(root: dir.path)
.where((entity) => entity is File)
.cast<File>();
}
Do you want to avoid adding a new dependency in your project? Manipulate the Directory itself:
Stream<File> search(Directory dir) {
return dir
.list(recursive: true)
.where((entity) => entity is File && entity.path.endsWith(".mp3"))
.cast<File>();
}
Defining the search scope
You'll also need a Directory where you'll search for MP3 files.
Is it the root directory (i.e. search ALL the files in the device)? Use this answer.
Is it another directory? Pick one from this package.
Usage
final Directory dir = /* the directory you obtained in the last section */;
await search(dir).forEach((file) => print(file));

Unzip all file without it's folder using Java

Is it possible to unzip all files from the zip folder without its folder?
Example:
zipfolder.zip has two subfolders called folder1(having files like 1.txt, 2.xlsx, 3.pdf) and folder2(having files like 4.txt, 5.pdf)
Note: The source can any type of archive files like .zip, .rar, .tar, .7-zip etc
This is my code:
String sevenZipLocation = "C:\\Program Files\\7-Zip\\7z.exe";
String src = source filepath (zip file)
String target = output path (output path)
String[] command={sevenZipLocation,"x",src,"-o"+target,"-aou","-y"};
ProcessBuilder p = new ProcessBuilder( command );
Process process = p.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
#SuppressWarnings("unused")
String line;
while ((line = br.readLine()) != null){
System.out.println("line1 "+line);
}
process.waitFor();
When I execute this code the output like
unzip folder ----- folder1(having files like 1.txt, 2.xlsx, 3.pdf) and folder2(having files like 4.txt, 5.pdf)
But I want to extract the only file from all folders and the output like
1.txt, 2.xlsx, 3.pdf, 4.txt, 5.pdf in the output path.
Is there any command for that. Thanks.
All you need to change:
String[] command={sevenZipLocation,"e",src,"-o"+target,"-aou","-y","*.*","-r"};
PS. I don't think Java is the best choice to run OS commands. You'll be wasting a lot of time. But if you insist, don't forget there might be an errorstream too.

How do I get the file/folder path using Apache Commons net FTPS Client

I am using Apache commons ftps client to connect to an ftps server. I have the remote file path which is a directory. This directory contains a tree of sub-directories and files. I want to get the path for each file or folder. Is there any way I can get this property? Or if there is any way I could get the parent folder path, I could concatenate the file name to it.
I am currently using below function to get path and size of all files under a certain directory. It gets all the file in current directory and check if it is a directory or file. If it is a directory call recursively until end of the tree, if it is a file save the path and size. You may not need these "map" things you can edit according to your needs.
Usage:
getServerFiles(ftp,"") // start from root
or
getServerFiles(ftp,"directory_name") // start from given directory
Implementation:
def getServerFiles(ftp: FTPClient, dir: String): Map[String, Double] = {
var fileList = Array[FTPFile]()
var base = ""
if (dir == "") {
fileList = ftp.listFiles
} else {
base = dir + "/"
fileList = ftp.listFiles(dir)
}
fileList.flatMap {
x => if (x.isDirectory) {
getServerFiles(ftp, base + x.getName)
} else {
Map[String, Double](base + x.getName -> x.getSize)
}
}.toMap
}

Get upload filename in eclipse using servlet

I have an application that uploads a file. I need to pass this file into another program but for that I need the file name only. Is there any simple code for that, using only Java or a servlet procedure?
while (files.hasMoreElements())
{
name = (String)files.nextElement();
type = multipartRequest.getContentType(name);
filename = multipartRequest.getFilesystemName(name);
originalFilename = multipartRequest.getOriginalFileName(name);
//extract the file extension - this can be use to reject a
//undesired file extension
extension1 = filename.substring
(filename.length() - 4, filename.length());
extension2 = originalFilename.substring
(originalFilename.length() - 4, originalFilename.length());
//return a File object for the specified uploaded file
File currentFile = multipartRequest.getFile(name);
//InputStream inputStream = new BufferedInputStream
(new FileInputStream(currentFile));
if(currentFile == null) {
out.println("There is no file selected!");
return;
}
There's a method in apache commons-io to get the file's extension. There's also guava Files class, with its getFileExtension method.

Upload Servlet with custom file keys

I have built a Server that you can upload files to and download, using Eclipse, servlet and jsp, it's all very new to me. (more info).
Currently the upload system works with the file's name. I want to programmatically assign each file a random key. And with that key the user can download the file. That means saving the data in a config file or something like : test.txt(file) fdjrke432(filekey). And when the user inputs the filekey the servlet will pass the file for download.
I have tried using a random string generator and renameTo(), for this. But it doesn't work the first time, only when I upload the same file again does it work. And this system is flawed, the user will receive the file "fdjrke432" instead of test.txt, their content is the same but you can see the problem.
Any thoughts, suggestions or solutions for my problem?
Well Sebek, I'm glad you asked!! This is quite an interesting one, there is no MAGIC way to do this. The answer is indeed to rename the file you uploaded. But I suggest adding the random string before the name of the file; like : fdjrke432test.txt.
Try this:
filekey= RenameRandom();
File renamedUploadFile = new File(uploadFolder + File.separator+ filekey+ fileName);
item.write(renamedUploadFile);
//remember to give the user the filekey
with
public String RenameRandom()
{
final int LENGTH = 8;
StringBuffer sb = new StringBuffer();
for (int x = 0; x < LENGTH; x++)
{
sb.append((char)((int)(Math.random()*26)+97));
}
System.out.println(sb.toString());
return sb.toString();
}
To delete or download the file from the server you will need to locate it, the user will input the key, you just need to search the upload folder for a file that begins with that key:
filekey= request.getParameter("filekey");
File f = new File(getServletContext().getRealPath("") + File.separator+"data");
File[] matchingFiles = f.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith(filekey);
}
});
String newfilename = matchingFiles[0].getName();
// now delete or download newfilename