java 自带的zip解压类ZipInputStream解压文件夹报错 解决方案

本文介绍如何使用Java自带的ZipInputStream类解决解压文件夹时遇到的问题,提供了一个unzipFile方法的示例代码,该方法接受zip文件路径和目标解压目录作为参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值