Is there a way to set a destination to unzip for gzip files when a user double clicks on the archive. Looking for a Scala/java solution - scala

Is there a way to get a gzip archive file to unzip to a different destination when a user double clicks on the archive? Currently, my compression code looks something like this in Scala:
val filename = SetFilename.getOrElse {
val path = files.head.getAbsolutePath
val baseUrl = FilenameUtils.getFullPathNoEndSeparator(path)
...
}
val output = new File(filename)
val fos = new FileOutputStream(output)
val gzos = new GZIPOutputStream(new BufferedOutputStream(fos))
try {
files.foreach { input =>
val fis = new FileInputStream(input)
try {
ioStream(fis, gzos)
gzos.flush()
}
finally {
fis.close()
}
}
}
finally {
gzos.close()
fos.close()
}
IS there any way to tell the compressed files to decompress in a different destination when a user double clicks on the archive?

It is not the gzip archive that decides it will be unzipped in the same location, it's something the operating system you're unzipping it on decides.
If you need to unzip into a specific place, you should look for a packaging solution like deb for Ubuntu or Debian systems; or dmg for OSX.

Related

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.

Downloading csv file using Play Framework?

I want to add to my app a simple button that on click will call an Action that will create a csv file from two lists I have and download it to the user computer.
This is my Action:
def createAndDownloadFile = Action {
val file = new File("newFile.csv")
val writer = CSVWriter.open(file)
writer.writeAll(List(listOfHeaders, listOfValues))
writer.close()
Ok.sendFile(file, inline = false, _ => file.getName)
}
but this is now working for me, the file is not getting downloaded from the browser...
im expecting to see the file get downloaded by the browser, i thought Ok.sendFile should do the trick..
thanks!
You can use Enumerators and streams for that. It should work like this:
val enum = Enumerator.fromFile(...)
val source = akka.stream.scaladsl.Source.fromPublisher(play.api.libs.streams.Streams.enumeratorToPublisher(enum))
Result(
header = ResponseHeader(OK, Map(CONTENT_DISPOSITION → "attachment; filename=whatever.csv.gz")),
body = HttpEntity.Streamed(source.via(Compression.gzip), None, None)
)
This will actually pipe the download through gzip. Just remove the .via(Compression.gzip) part if that is not needed.

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
}

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

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

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.