1

現在、Google App Engine (GAE) を使用してアプリケーションを開発しています。GAE では、zip ファイルを保存してそこから読み取るための一時フォルダーを作成できません。唯一の方法は、メモリから読み取ることです。zipfile には、CSVReader に読み込む必要がある 6 つの CSV ファイルが含まれています。

//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);

CSVReader オブジェクトの CharArrayReader を作成するために必要な char[] に ZipInputStream を読み込むにはどうすればよいですか。

CSVReader reader = new CSVReader(CharArrayRead(char[] buf));
4

1 に答える 1

3

ZipInputStreamをInputStreamReaderでラップして、バイトから文字に変換します。次に、inputStreamReader.read(char [] buf、int offset、int length)を呼び出して、次のようにchar[]バッファーを埋めます。

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

CharArrayReadが実際にjava.io.CharArrayReaderである場合は、char []にロードする必要はなく、次のようなコードを使用することをお勧めします。

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

zipファイルが1つしかない場合(1MBの制限を回避しようとしている場合)、これは機能します。

InputStreamReader isr = new InputStreamReader(zin);
zip.getNextEntry();
CSVReader reader = new CSVReader(isr, ...);
于 2010-11-01T16:06:05.297 に答える