【发布时间】:2022-02-03 20:15:04
【问题描述】:
我正在尝试压缩子文件夹并收到 Stream Closed 异常
private static void zipSubFolder(ZipOutputStream out, File folder, int basePathLength) throws IOException{
final int BUFFER = 2048;
File[] fileList = folder.listFiles();
BufferedInputStream origin = null;
for (File file : fileList) {
if (file.isDirectory()) {
zipSubFolder(out, file, basePathLength);
} else {
byte[] data = new byte[BUFFER];
String unmodifiedFilePath = file.getPath();
String relativePath = unmodifiedFilePath
.substring(basePathLength);
try (FileInputStream fi = new FileInputStream(unmodifiedFilePath)) {
origin = new BufferedInputStream(fi, BUFFER);
}
ZipEntry entry = new ZipEntry(relativePath);
entry.setTime(file.lastModified()); // to keep modification time after unzipping
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
origin.close();
}
}
}
在这行出现错误: while ((count = origin.read(data, 0, BUFFER)) != -1)
: java.io.IOException: Stream Closed
at java.io.FileInputStream.read(FileInputStream.java:313)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
【问题讨论】:
标签: java android android-studio ioexception fileinputstream