文件处理

记录一下在文件处理里面遇到的问题

读excel大文件

依赖添加

git地址

1
2
3
4
5
<dependency>
<groupId>com.monitorjbl</groupId>
<artifactId>xlsx-streamer</artifactId>
<version>2.0.0</version>
</dependency>

示例代码

1
2
3
4
5
6
7
Workbook workbook = StreamingReader.builder().rowCacheSize(1).bufferSize(4096).open(in);
Sheet sheet = workbook.getSheetAt(0);
Row firstRow = sheet.rowIterator().next();
Iterator<Cell> iterator = firstRow.cellIterator();
while (iterator.hasNext()) {
cellList.add(iterator.next().getStringCellValue());
}

对输入流进行压缩

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

private InputStream getZipInputStream(InputStream inputStream, String fileName) {
byte[] buf = new byte[1024];
try {
//ZipOutputStream类:完成文件或文件夹的压缩
// ZipOutputStream out = new ZipOutputStream(new FileOutputStream("/home/jianganwei/下载/demo_LOCAL.zip"));
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
ZipOutputStream out = new ZipOutputStream(byteArrayOutputStream);
int len;
ZipEntry zipEntry = new ZipEntry(fileName);
// zipEntry.setUnixMode(666);

out.putNextEntry(zipEntry);
while ((len = inputStream.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
inputStream.close();
out.close();
logger.info("压缩完成."+fileName);
return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/org.apache.ant/ant -->
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.9.3</version>
</dependency>