首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在CharArrayReader中读取ZipInputStream

如何在CharArrayReader中读取ZipInputStream
EN

Stack Overflow用户
提问于 2010-11-01 23:44:49
回答 1查看 3K关注 0票数 1

现在,我正在使用Google App Engine (GAE)开发一个应用程序。GAE不允许我创建临时文件夹来存储我的zipfile并从中读取。唯一的方法是从内存中读取它。压缩文件包含6个CSV文件,我需要将其读取到CSVReader中。

代码语言:javascript
复制
//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);

如何将ZipInputStream读取到char[]中,这是为我的CSVReader对象创建CharArrayReader所需的。

代码语言:javascript
复制
CSVReader reader = new CSVReader(CharArrayRead(char[] buf));
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-11-02 00:06:05

用InputStreamReader包装ZipInputStream以将字节转换为字符;然后调用inputStreamReader.read(char[] buf,int offset,int length)来填充char[]缓冲区,如下所示:

代码语言:javascript
复制
//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[]中,最好使用下面这样的代码:

代码语言:javascript
复制
InputStreamReader isr = new InputStreamReader(zin);
BufferedReader br = new BufferedReader(isr);
ZipEntry ze;
while ((ze = zin.getNextEntry()) != null) {
    CSVReader reader = new CSVReader(br);
}

如果你只有一个压缩文件(试图绕过1MB的限制),那么这是可行的:

代码语言:javascript
复制
InputStreamReader isr = new InputStreamReader(zin);
zip.getNextEntry();
CSVReader reader = new CSVReader(isr, ...);
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4070446

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档