1

Right now i am developing an application using Google App Engine (GAE). GAE doesnt allow me to create temp folder for me to store my zipfile and read from it. The only way is to read it from memory. The zipfile contains 6 CSV files which i need to read it into CSVReader.

//part of the code

MultipartFormDataRequest multiPartRequest = null;

Hashtable files = multiPartRequest.getFiles();

UploadFile userFile = (UploadFile)files.get("bootstrap_file");

InputStream input = userFile.getInpuStream();

ZipInputStream zin = new ZipInputStream(input);

How do i read ZipInputStream into char[] which is needed to create CharArrayReader for my CSVReader object.

CSVReader reader = new CSVReader(CharArrayRead(char[] buf));
cuh
  • 3,723
  • 4
  • 30
  • 47
Wen Jun
  • 522
  • 3
  • 9
  • 19

1 Answers1

3

Wrap the ZipInputStream with an InputStreamReader to convert from bytes to chars; then call inputStreamReader.read(char[] buf, int offset, int length) to fill your char[] buffer, like this:

//part of the code
MultipartFormDataRequest multiPartRequest = null;
Hashtable files = multiPartRequest.getFiles();
UploadFile userFile = (UploadFile)files.get("bootstrap_file");
InputStream input = userFile.getInpuStream();
ZipInputStream zin = new ZipInputStream(input);

// wrap the ZipInputStream with an InputStreamReader    
InputStreamReader isr = new InputStreamReader(zin);
ZipEntry ze;
// ZipEntry ze gives you access to the filename etc of the entry in the zipfile you are currently handling
while ((ze = zin.getNextEntry()) != null) {
    // create a buffer to hold the entire contents of this entry
    char[] buf = new char[(int)ze.getSize()];
    // read the contents into the buffer
    isr.read(buf);
    // feed the char[] to CSVReader
    CSVReader reader = new CSVReader(CharArrayRead(buf));
}

if your CharArrayRead is actually a java.io.CharArrayReader, then there is no need to load it into a char[] and you'd be better off with code like this:

InputStreamReader isr = new InputStreamReader(zin);
BufferedReader br = new BufferedReader(isr);
ZipEntry ze;
while ((ze = zin.getNextEntry()) != null) {
    CSVReader reader = new CSVReader(br);
}

If you just have a single zipped file (trying to get around the 1MB limitation) then this will work:

InputStreamReader isr = new InputStreamReader(zin);
zip.getNextEntry();
CSVReader reader = new CSVReader(isr, ...);
Luke Hutteman
  • 1,921
  • 13
  • 11
  • i have 6 ZipEntry(6 csv file inside the zipinputstream). So how do i exactly point the right ZipEntry to the right CSVReader? And how do i "wrap" the ZipInputStream with an InputStreamReader? – Wen Jun Nov 01 '10 at 16:36
  • 1
    you use ZipInputStream.getNextEntry() to move to the next entry in the zipfile; I'll edit my answer with a code-sample. – Luke Hutteman Nov 01 '10 at 16:56