QQ群: 643369047, 一起交流行业问题
方法
private static File unzip(MultipartFile file) throws IOException {
byte[] buffer = new byte[1024];
int bufferSize = 1024;
File tempFile = null;
ZipInputStream zis = new ZipInputStream(file.getInputStream());
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
tempFile = File.createTempFile(entry.getName(), "tmp");
tempFile.deleteOnExit();
FileOutputStream fos = new FileOutputStream(tempFile);
BufferedOutputStream bos = new BufferedOutputStream(fos, bufferSize);
int count;
while ((count = zis.read(buffer, 0, bufferSize)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
bos.close();
}
zis.close();
return tempFile;
}
Controller类
sbf为解压缩包中的文本信息
public void receiveZip(MultipartFile file) throws IOException {
try {
File unzip = unzip(file);
BufferedReader reader = null;
StringBuffer sbf = new StringBuffer();
try {
reader = new BufferedReader(new FileReader(unzip));
String tempStr;
while ((tempStr = reader.readLine()) != null) {
sbf.append(tempStr);
}
reader.close();
System.out.println(sbf.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
System.out.println("============================");
System.out.println(sbf.toString());
} catch (IOException e) {
e.printStackTrace();
}
}