日期:2014-05-20 浏览次数:20798 次
public static List unzip(FormFile formFile) { if (formFile == null) return null; List list = null; InputStream in = null; ZipInputStream zipInput = null; try { in = formFile.getInputStream(); zipInput = new ZipInputStream(in); ZipEntry zipEntry = null; list = new ArrayList(); while ((zipEntry = zipInput.getNextEntry()) != null) { if (!zipEntry.isDirectory()) { FileBean fb = new FileBean(); int len = new Long(zipEntry.getSize()).intValue(); byte[] data = new byte[len]; int actual = 0; int bytesread = 0; while ((bytesread != len) && (actual != -1)) { actual = zipInput .read(data, bytesread, len - bytesread); bytesread += actual; } fb.setFileSize(len); fb.setFileName(zipEntry.getName()); fb.setFileType(zipEntry.getName().substring( zipEntry.getName().lastIndexOf(".") + 1)); fb.setContents(data); fb.setUploadDate(DateTimeUtil.getToDate()); fb.setUploadTime(DateTimeUtil.getToSecond()); list.add(fb); } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (zipInput != null) { zipInput.close(); } if (in != null) { in.close(); } } catch (Exception ex) { } } return list; }