public static void unzipFile(File zip,String directory) throws Exception
{
try{
java.util.zip.ZipInputStream zis = new java.util.zip.ZipInputStream(new FileInputStream(zip));
java.util.zip.ZipEntry ze = (java.util.zip.ZipEntry) zis.getNextEntry();
File parent = new File(directory);
if (!parent.exists() && !parent.mkdirs()){
throw new Exception("解压 \"" + parent.getAbsolutePath() + "\" 失败");
}
while (ze != null){
String name = ze.getName();
File child = new File(parent, name);
FileOutputStream output = new FileOutputStream(child);
byte[] buffer = new byte[10240];
int bytesRead = 0;
while ((bytesRead = zis.read(buffer)) > 0){
output.write(buffer, 0, bytesRead);
}
output.flush();
output.close();
ze = (java.util.zip.ZipEntry) zis.getNextEntry();
}
zis.close();
}catch (IOException e){
e.printStackTrace();
}
}
如果压缩文件下面有文件夹就报错没有文件夹直接是文件就不报错误。
解决方案用zip4j import net.lingala.zip4j.core.ZipFile;
/** * 大肠癌市局数据导入 * @param mapping * @param form * @param request * @param response * @return * @throws Exception * @author:jinijing@neusoft.com * @throws IOException * @throws IOException * @throws IOException */ public ActionForward doImport(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException { String res = null; // 取得表单对象 IUpload upload = null; try { upload = UploadFactory.createAndExecuteUpLoad( UploadFactory.UPLOAD_TYPE_COMMONLOAD, request, response); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); // 回调 res = "保存出错,请重新保存!"; RiaUtil.writerJsonText2Page(response, "<textarea>{result:'" + res + "'}</textarea>"); return null; } IFile file = upload.getFiles().getFile(0); String fileName = file.getFileName(); //设置文件名称 try { String filePath = request.getSession().getServletContext() .getRealPath("") + "/tempfh"; File fileTemp = new File(filePath); //如果文件夹不存在则创建如果存在删除文件里所有子文件 if (!fileTemp.exists() && !fileTemp.isDirectory()) { //创建文件目录 fileTemp.mkdir(); } else { //删除文件里所有文件及子文件 checkDir(fileTemp); } String df = filePath + "/" + fileName; file.saveAs(df); ZipFile zipFile = new ZipFile(df); zipFile.extractAll(filePath); if (fileTemp.isDirectory()) { String[] children = fileTemp.list(); for (int i = 0; i < children.length; i++) { if (children[i].trim().equalsIgnoreCase("初筛记录.xml")) { //导入初筛记录解析xml traverXmlByIteratorcs(new File(fileTemp + "/初筛记录.xml")); } else if (children[i].trim().equalsIgnoreCase("基本信息.xml")) { //导入初筛记录解析xml traverXmlByIteratorzb(new File(fileTemp + "/基本信息.xml")); } else if (children[i].trim().equalsIgnoreCase("随访记录.xml")) { //导入随访记录解析xml traverXmlByIteratorsf(new File(fileTemp + "/随访记录.xml")); } else if (children[i].trim().equalsIgnoreCase("诊断记录.xml")) { //导入诊断记录解析xml traverXmlByIteratorzd(new File(fileTemp + "/诊断记录.xml")); } else if (children[i].trim().equalsIgnoreCase("report")) { //导入bingli .pdf importBL(new File(filePath + "/report/bingli")); //导入changjing .pdf importCJ(new File(filePath + "/report/changjing")); //导入chuyuan .pdf importCY(new File(filePath + "/report/chuyuan")); } } } } catch (Exception e) { e.printStackTrace(); // 回调 res = "保存文档出错!"; RiaUtil.writerJsonText2Page(response, "<textarea>{result:'" + res + "'}</textarea>"); return null; } // 回调 res = "导入数据成功!"; RiaUtil.writerJsonText2Page(response, "<textarea>{result:'" + res + "'}</textarea>"); return null; }