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());
  }
}

No Comments »

Comment on this entry

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">