如何将 ZipInputStream 读入 CharArrayReader

发布于 2024-09-30 07:09:43 字数 610 浏览 2 评论 0原文

现在我正在使用 Google App Engine (GAE) 开发一个应用程序。 GAE 不允许我创建临时文件夹来存储我的 zip 文件并从中读取。唯一的方法就是从记忆中读取它。该 zip 文件包含 6 个 CSV 文件,我需要将其读入 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);

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随波逐流 2024-10-07 07:09:43

用 InputStreamReader 包装 ZipInputStream 以将字节转换为字符;然后调用 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);
}

如果你只有一个压缩文件(试图绕过 1MB 限制),那么这将起作用:

InputStreamReader isr = new InputStreamReader(zin);
zip.getNextEntry();
CSVReader reader = new CSVReader(isr, ...);

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, ...);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文