1:jar包
commons-compress-1.0.jar
commons-io.jar
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.io.IOUtils;
2:代码片段:
ZipArchiveInputStream zais = new ZipArchiveInputStream(zipFileForm.getInputStream(), "GBK", true);
ArchiveEntry archiveEntry = null;
while((archiveEntry = zais.getNextEntry()) != null) {
//获取文件名
String entryFileName = archiveEntry.getName();
//只处理包中的*.xls文件
if(!entryFileName.endsWith(".xls")){
continue;
}
// 把解压后的数据写到内存数组中.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IOUtils.copy(zais, baos);
//把解压后的内存数组与ByteArrayInputStream关联
ByteArrayInputStream oneUnzipedIS = new ByteArrayInputStream(baos.toByteArray());
}
commons-compress-1.0.jar
commons-io.jar
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.io.IOUtils;
2:代码片段:
ZipArchiveInputStream zais = new ZipArchiveInputStream(zipFileForm.getInputStream(), "GBK", true);
ArchiveEntry archiveEntry = null;
while((archiveEntry = zais.getNextEntry()) != null) {
//获取文件名
String entryFileName = archiveEntry.getName();
//只处理包中的*.xls文件
if(!entryFileName.endsWith(".xls")){
continue;
}
// 把解压后的数据写到内存数组中.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IOUtils.copy(zais, baos);
//把解压后的内存数组与ByteArrayInputStream关联
ByteArrayInputStream oneUnzipedIS = new ByteArrayInputStream(baos.toByteArray());
}
本文介绍如何使用Java解压ZIP文件,并仅处理其中的.xls文件。通过使用Apache Commons库,实现从ZIP文件读取数据并将其保存到内存数组中。
2081

被折叠的 条评论
为什么被折叠?



