061007 | Code, Processing / Java
Tags: Code, compression, data, file, hack, java, pde, processing.org, text, zip
Tags: Code, compression, data, file, hack, java, pde, processing.org, text, zip
This is a simple variant to the built-in saveStrings() method in Processing. It allows you to write strings to a GZIP compressed file instead of a plain text file. Very useful when writing text-based data files that add up to a few megabytes.
Source code – saveStringsGZIP.pde
// saveStringsGZIP.pde
// Marius Watz - http://workshop.evolutionzone.com
//
// Code for saving an array of strings to a GZIP compressed file.
// Based on Processing's built-in saveStrings() method.
// Java classes needed for file I/O.
import java.io.*;
import java.util.zip.GZIPOutputStream;
void setup() {
String [] s=new String[2000];
for(int i=0; i< s.length; i++) s[i]=nf(i,50);
saveStringsGZIP("test.txt",s);
}
// Based on code from processing.core.PApplet, by the Processing team.
public void saveStringsGZIP(String filename, String strings[]) {
try {
String location = savePath(filename+".gz");
GZIPOutputStream fos =
new GZIPOutputStream(new FileOutputStream(location));
PrintWriter writer =
new PrintWriter(new OutputStreamWriter(fos));
for (int i = 0; i < strings.length; i++)
if(strings[i]!=null) writer.println(strings[i]);
writer.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("saveStringsGZIP() failed: "
+ e.getMessage());
}
}
Comment on this entry




