I have a bunch of public assets that I would like to zip and provide for download.
Is there an easy way in Play! to create a zip for a list of files/folders?
I suppose you can always use Java libraries. See JavaDocs for details
play war "project_dir" -o "war_file_name" --zip
Note: I'm using Play-1.2.3
You can use Play helper class, Files.zip
In the controller use this:
public class Public extends Controller {
public static void download() {
File dir = VirtualFile.fromRelativePath("/dir-to-zip/").getRealFile();
File zip = VirtualFile.fromRelativePath("/files.zip").getRealFile();
Files.zip(dir, zip);
renderBinary(zip);
}
}
Then add this to your routes file:
* /download Public.download
Note that file paths are from your application root, not your file system root.
Related
I need to run a web-app on Tomcat, but it cannot read the txt files(from a relative paths as below) on Tomcat. However, it does work if I use a full path.
So I am wondering where can I put these txt files so that when Tomcat started, the app can successfully read the txt files from a relative path.
Currently, the project structure is as follows, the txt files is located on the same directory as src file in Project Explorer in Eclipse.
Project_Name
src
java files
EDGES.txt
NODES.txt
The code is as follows, I am appreciated if someone can give me an answer in details, since I am quite new to Java.
The code is as follows:
public class RouteingDao {
NodeJSONReader nodeInput = new NodeJSONReader("NODES.txt");
EdgeJSONReader edgeInput = new EdgeJSONReader("EDGES.txt");
...
}
The NodeJSONReader/EdgeJSONReader class is as follows:
public class EdgeJSONReader {
private EdgeEntity[] edgeEntity;
// constructor
public EdgeJSONReader(String JSON_FILE) {
edgeEntity = readEntityFromFile(JSON_FILE);
}
// load the JSON data from local file
public EdgeEntity[] readEntityFromFile(String JSON_FILE) {
try {
Reader reader = new FileReader(JSON_FILE);
Gson gson = new Gson();
edgeEntity = gson.fromJson(reader, EdgeEntity[].class);
}
...
}
}
If you are using a servlet, then access the servlet context and the getRealPath method.
this.getServletContext().getRealPath("WEB-INF/nodes.txt")
The relative path sent to getRealPath will be expanded to the location of the files for your web app. You can add any path you like, even to a hidden file in WEB-INF.
From a JSP you can use
${pageContext.servletContext.getRealPath("WEB-INF/nodes.txt")}
Be careful, this will be in the build directory, so any changes to nodes.txt will not be saved to the original file.
Here is my problem.
My package setup is:
-src
---- foo
-------- code.java
---- foo.data
-------- files I need to access
Currently I have a jar that contains all of the above files I need to access. I will be running this code on my machine and send File objects initialized on my machine to another machine with the same jar running.
My question is how can I obtain a relative path to the data file so when I send the File objects to the other machine they know to look in the jar?
I need to access the data file so I can perform methods on it. Therefore I need to create a new File(relative/path/to/jar/data/files) with it and then I will be passing the File objects to another computer to compute.
Currently I have been able to use getClass().getResource("data/" + "filename").getPath() this gives me a path like file:/pathtojar!/pathtofileinjar/ which I think will be able to find the jar in the other machine but I am getting null pointer when trying to use the item in the jar. Are files in jar able to be used or is there a workaround for it?
Why not send the relative path of the file in the jar, instead of the File?
The other program can then use that path and read the data from the jar.
Could you try with the absolute path, like so:
package resourcetest;
import java.net.URL;
public class ResourceTest {
public static void main(String[] args) {
new ResourceTest().test();
}
private void test() {
URL res = this.getClass().getResource("/data/data.txt");
System.out.println(res);
}
}
Here my data file that i want to access is data/data.txt
src\resourcetest\ResourceTest.class (this class runs and tries to access data/Data.txt
src\data\Data.txt
I want to use the TrueZip library to append a file to an existing archive (not by
unpacking, adding a file and repacking - the new versions are supposed to have this
feature), but I find it a bit difficult to understand the API.
Can please someone, more knowledgeable than me, suggest how to do this in a few lines?
Google is your friend:
Appending entries to ZIP files with TrueZIP 7.3
class MyApplication extends TApplication {
#Override
protected void setup() {
// This should obtain the global configuration.
TConfig config = TConfig.get();
// Set FsOutputOption.GROW for appending-to rather than reassembling an
// archive file.
config.setOutputPreferences(
config.getOutputPreferences.set(FsOutputOption.GROW));
}
...
}
I am making the library that has the default properties in the file default.properties.
private static String defPropertyPath = "/database.properties";
I want to ask if this file can be replaced by the program that use my library. So the program will define the properties with the same name default.properties that will replace the properties from library. I created the default.properties in the program where i use the library, but the library is still loading the properties from their package.
edit:
I read the properties file via input stream:
InputStream ins = DbProperties.class.getResourceAsStream(defPropertyPath);
if (ins == null) {
logger.error("Can't find properties:" + pathToProperties);
return;
}
Edit: File structure:
DbLibrary.jar
/
/database.properties
/src
MyApplication.jar
/
/database.properties
/src
/lib/DbLibrary.jar
My application use the DbLibrary.jar and wants to force this library to use database.properties from MyApplication and not from the DbLibrary.
May be much clearer if your library exports some API that allows the user of your library to invoke an init method at any time.
public static void init(Properties p) { ... }
I'm not sure there is enough information to answer your question, but I'm going to guess that perhaps you included the "database.properties" file in the jar with your application. If you did that, the application will always read the file from the jar, and not from the file system.
You only have to override the properties file in these projects which include your library.
I have stored non-java files in a package. I want to read files from this package without specifying the absolute path to the file(e.g C:\etc\etc...). How should I do this?
Use getResourceAsStream
For example:
MyClass.class.getResourceAsStream("file.txt");
Will open file.txt if it's in the same package that MyClass
Also:
MyClass.class.getResourceAsStream("/com/foo/bar/file.txt");
Will open file.txt on package com.foo.bar
Good luck! :)
First, ensure that the package in which your files contained is in your app's classpath..
Though your didnt specifying the path of the files, you still have to obtain the files' paths to read them.Your know all your files' names and package name(s)? If so, your could try this to obtain a url of your file:
public class Test {
public static void main(String[] args) throws Exception {
URL f = Test.class.getClassLoader().getResource("resources/Test.txt");
System.out.println(f);
}
}
the code above obtains the url of file 'Test.txt' in another package named 'resources'.