-
Notifications
You must be signed in to change notification settings - Fork 9
GZIP Service
Marc Larue edited this page Mar 19, 2018
·
2 revisions
public static class GZIPService extends Service {
public String path() { return "/some.file"; }
public void filter(Event event) throws Event, Exception {
event.reply().header("Content-Encoding", "gzip");
File to = compress();
FileInputStream in = new FileInputStream(to);
Deploy.pipe(in, event.reply().output(to.length()));
in.close();
}
public synchronized File compress() {
File from = new File("app/content/some.file");
File to = new File("app/content/some.file.gzip");
boolean zip = false;
if(to.exists()) {
if(from.lastModified() > to.lastModified()) {
zip = true;
}
}
else {
to.createNewFile();
zip = true;
}
if(zip) {
FileInputStream in = new FileInputStream(from);
GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(to));
Deploy.pipe(in, out);
out.finish();
out.close();
in.close();
}
return to;
}
}
You could also zip the files before deployment. Actually you should only zip data that either compresses very well or that can be cached, otherwise it's a waste of CPU. In the future rupy might do this automatically for static files.